簡體   English   中英

C#Excellibrary-如何獲取單元格內容?

[英]C# Excellibrary - how to get cell content?

我需要一些快速幫助。 我如何使用網狀結構獲取單元格的內容? 該文檔非常薄。

我沒有真正得到示例代碼:

 // 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];

有沒有類似的方法可以做到這一點? (免責聲明:在這些示例代碼段中可能會出現一些語法錯誤,但出於理論目的它們只是存在。)

編輯:

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

給我一個NullReferenceException(對象引用未設置為對象的實例。)。

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

初始化數組后,此方法工作正常(忘記初始化它帶來了我誤以為是關於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]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM