2013-03-07 32 views
0

我需要一些快速帮助。 如何使用excellibrary获取单元格的内容?文档非常薄。C#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/

+0

你的意思是Excel Interop? – MoonKnight 2013-03-07 09:21:36

+0

而不是挣扎着,为什么不尝试使用另一个库,http://exceldatareader.codeplex.com/也许? – andri 2013-03-07 09:38:31

+0

我急于完成这段代码,忽略了一个简单的事实。我在我的xlInfo [,]中有一个错误。工作正常。我应该删除这个问题吗? – 2013-03-07 09:40:52

回答

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]); 
相关问题