使用C#从Excel复制

我有一个Excel文件,我需要从一列复制数据。 但是我不知道有多less行。

using Excel = Microsoft.Office.Interop.Excel; Excel.Application xlApp = new Excel.Application(); Excel.Workbook _workBook = xlApp.Workbooks; Excel.Worksheet _workSheet = (Excel.Worksheet) _workBook.Worksheets[1]; 

我怎样才能复制数据?

 Range range = _workSheet .UsedRange; int rows = range.Rows.Count; int cols = range.Columns.Count; // nested loops to take values from used range of cell one by one for(int r=1; r <= rows; r++) for(int c=1; c <= cols; c++) object cellValue = range.Cells[r,c].Value2; // take values from one row int row = 1; for(int c=1; c <= cols; c++) object cellValue = range.Cells[row,c].Value2; 

由于您没有提供任何有关用于使用Excel的库的信息,因此我将假定您正在使用Interop服务:

 int columns = _workSheet.Columns.Count; int rows = _workSheet.Rows.Count; int cells = columns*rows string cellValue = (string)((Excel.Range)_workSheet.Cells[1, 1]).Value;