我需要一些快速帮助。 如何使用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(对象引用未设置为对象的实例。)。
答案 0 :(得分: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]);