Set range value sets only the first value

时间:2016-10-20 19:10:51

标签: c# excel office-addins excel-addins

To set a range of Excel values, I use this code:

int[] values = new int[] {1, 2, 3};
String rangeStart = "A1";
String rangeEnd = "A3";

Microsoft.Office.Interop.Excel.Workbook objBook;
Microsoft.Office.Interop.Excel.Sheets objSheets;
Microsoft.Office.Interop.Excel._Worksheet workSheet;

objBook = (Microsoft.Office.Interop.Excel.Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;
objSheets = objBook.Worksheets;
workSheet = (Microsoft.Office.Interop.Excel.Worksheet)objBook.ActiveSheet;

workSheet.Range[rangeStart, rangeEnd].Value = values;

But the cells contain only the first value of the array. Any ideas how to write every array value into the fields?

1 个答案:

答案 0 :(得分:1)

As @Ron Rosenfield says, Excel ranges are 2-dimenional arrays. So,

int[,] values = new int[,] { { 1 }, { 2 }, { 3 } }; 

will work for you. Always remember - 2d arrays