读取Excel文件

时间:2015-06-03 20:04:44

标签: c# excel multidimensional-array

我正在尝试使用C#读取excel表并将每行存储到一个数组中。我能够打开文件,但我正在使用的代码读取到" 2D-Object"数组,但我想将信息读入1D字符串数组。

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);
    // 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 :(得分:1)

抱歉,我只想评论。 = X 但无论如何它对你有用吗? LinqToExcel是一个用于处理电子表格的绝佳库! 如果有任何疑问,请告诉我。

=)