0

我需要一些快速帮助。如何使用 excellibrary 获取单元格的内容?文档很薄。

我真的没有得到示例代码:

 // traverse cells
 foreach (Pair<Pair<int, int>, Cell> cell in sheet.Cells)
 {
     dgvCells[cell.Left.Right, cell.Left.Left].Value = cell.Right.Value;
 }

 // traverse rows by Index
 for (int rowIndex = sheet.Cells.FirstRowIndex; 
        rowIndex <= sheet.Cells.LastRowIndex; rowIndex++)
 {
     Row row = sheet.Cells.GetRow(rowIndex);
     for (int colIndex = row.FirstColIndex; 
        colIndex <= row.LastColIndex; colIndex++)
     {
         Cell cell = row.GetCell(colIndex);
     }
 }

它似乎过于复杂。我需要做类似的事情:

xlInfo[0,0] = cell 1,1;
xlInfo[0,1] = cell 1,2;
xlInfo[0,2] = cell 1,3;

使用 EPPlus,它看起来像这样:

xlInfo[0,0] = sheet.Cells[1,1];
xlInfo[0,1] = sheet.Cells[1,2];
xlInfo[0,2] = sheet.Cells[1,3];

excellibrary 有类似的方法吗?(免责声明:这些示例片段中可能会出现一些语法错误,但它们只是出于理论目的。)

编辑:

xlInfo[0, 0] = Convert.ToString(sheet.Cells[1, 1]);

给我一个 NullReferenceException (对象引用未设置为对象的实例。)。

我正在使用:http ://code.google.com/p/excellibrary/

4

1 回答 1

1

这在初始化数组后工作正常(忘记初始化它会导致我误认为有关 ExcelLibrary 的错误的错误)。

Workbook book = Workbook.Load(directory + "file.xls");
Worksheet sheet = book.Worksheets[0];
xlInfo = new string[sheet.Cells.LastRowIndex, 3];

xlInfo[0, 0] = Convert.ToString(sheet.Cells[1, 0]);
xlInfo[0, 1] = Convert.ToString(sheet.Cells[1, 1]);
xlInfo[0, 2] = Convert.ToString(sheet.Cells[1, 2]);
于 2013-03-07T09:52:12.550 回答