【发布时间】:2015-06-03 15:52:31
【问题描述】:
我是编码新手,我正在尝试将每个客户的信息(Id num、name、生日)放入他们自己的数组中。到目前为止,我能够从 excel 中读取数据,我只是不知道如何将其存储在数组中,而且我不确定如何将“valueArray”从对象转换为字符串数组
static void Main(string[] args)
{
// Reference to Excel Application.
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(Path.GetFullPath("excelpractice1.xlsx"));
// Get the first worksheet.
Excel.Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Sheets.get_Item(1);
// Get the range of cells which has data.
Excel.Range xlRange = xlWorksheet.UsedRange;
// Get an object array of all of the cells in the worksheet with their values.
object[,] valueArray = (object[,])xlRange.get_Value(Excel.XlRangeValueDataType.xlRangeValueDefault);
// iterate through each cell and display the contents.
for (int row = 1; row <= xlWorksheet.UsedRange.Rows.Count; ++row)
{
Console.WriteLine(valueArray[row, row].ToString());
}
// Close the Workbook.
xlWorkbook.Close(false);
// Relase COM Object by decrementing the reference count.
Marshal.ReleaseComObject(xlWorkbook);
// Close Excel application.
xlApp.Quit();
// Release COM object.
Marshal.FinalReleaseComObject(xlApp);
Console.ReadLine();
}
【问题讨论】: