Search code examples
c#excellibrary

C# Excellibrary - how to get cell content?


I need some quick help. How do I get the content of a cell using excellibrary? The documentation is very thin.

I don't really get the example code:

 // 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);
     }
 }

It seems overly complex. I need to do something like:

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

With EPPlus it would look something like this:

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

Is there a similar way of doing this with excellibrary? (Disclaimer: some syntax error may occur in these example snippets, but they're just there for theory purposes.)

EDIT:

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

Gives me a NullReferenceException (Object reference not set to an instance of an object.).

I'm using: http://code.google.com/p/excellibrary/


Solution

  • This worked fine after initializing the array (forgetting to initialize it brought about the error which i mistook for an error regarding 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]);