1

I have an excel range:

   Excel.Range range = Worksheet.get_Range("A1","A7");

And a string list:

// The list contains the same amount of elements as cells in the range
List<string> AcollumnValues = getList(); 

My code so far:

        Excel.Application App = new Excel.Application();
        Excel.Workbook Workbook = App.Workbooks.Open(sourceASettings["file"]);
        Excel._Worksheet Worksheet = Workbook.Sheets[1];


        Excel.Range range = Worksheet.get_Range("A1","A4") userSupliedRangeArray[1]);
        List<string> valuesToWrite = getValuesToWrite();

How can I write the string list to the excel using the range?

7
  • 1
    Maybe there's a way of doing it directly, but you can iterate through the list and write the values one by one . Commented Oct 3, 2018 at 9:45
  • 2
    If you are able to use EPPlus you might be able to use LoadFromCollection Commented Oct 3, 2018 at 9:48
  • 1
    Found an example of EPPlus LoadFromCollection stackoverflow.com/questions/34882275/… Commented Oct 3, 2018 at 9:53
  • 1
    I second using EPPlus. Makes working with Excel files a lot easier Commented Oct 3, 2018 at 9:53
  • 1
    Ah, @matt link has the right solution for you. Commented Oct 3, 2018 at 9:54

1 Answer 1

2

You'll maybe need to adjust this and fix some minor issues, but it should do the trick (Uses EPPlus):

int counter = 1;
foreach (string str in AcollumnValues) {
   worksheet.Cells["A" + counter].Value = str;
   counter++;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.