【问题标题】:Reading customer data from excel into an array将客户数据从excel读取到数组中
【发布时间】: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();
}

【问题讨论】:

    标签: c# arrays excel vsto


    【解决方案1】:

    类似这样的:

    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();
          } 
    }
    

    你也可以像here一样使用Array.Copy

    顺便说一句。不确定,但我认为您还应该发布xlRange

    【讨论】:

      猜你喜欢
      • 2013-07-16
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      相关资源
      最近更新 更多