将客户数据从excel读入数组

时间:2015-06-03 15:52:32

标签: c# arrays excel vsto

我是编码的新手,我正在尝试将每个客户的信息(身份证号码,姓名,生日)收集到他们自己的数组中。到目前为止,我能够从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();
}

1 个答案:

答案 0 :(得分:0)

这样的事情:

String[,] arr = new String[xlWorksheet.UsedRange.Rows.Count,xlWorksheet.UsedRange.Columns.Count];
for (int row = 1; row <= xlWorksheet.UsedRange.Rows.Count; ++row)
{
      for (int col = 1; col <= xlWorksheet.UsedRange.Columns.Count; ++col)
      {
        arr[row,col] = valueArray[row, col].ToString();
      } 
}

您也可以Array.Copy使用xlRange

顺便说一下。不确定,但我认为您还需要发布ComboBox