Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Column out of Range" Error when populating an Excel Sheet

Tags:

c#

excel

I just started implementing this to populate an Excel sheet with some data:

using OfficeOpenXml;

//..
ExcelWorksheet VerificationSheet_Sheet4 = package.Workbook.Worksheets.Add("SheetTitleHere");
int row = 0, col = 0;
VerificationSheet_Sheet4.Cells[row + 1, col].Value = "AnyStringHere"; // error here

However it pops an error saying column is out of range. Why and how can I fix that?

like image 722
Khalil Khalaf Avatar asked Jun 20 '16 15:06

Khalil Khalaf


Video Answer


2 Answers

Excel worksheets use 1 based indexing rather than zero based. Thus columns and rows both start at 1 and not 0.

like image 100
Steve Avatar answered Sep 30 '22 09:09

Steve


As per the elaboration by @Darren Young Excel uses 1 based indexing thats where the issue is.

  using OfficeOpenXml;

    //..
    ExcelWorksheet VerificationSheet_Sheet4 = package.Workbook.Worksheets.Add("SheetTitleHere");
    int row = 1, col = 1;
    VerificationSheet_Sheet4.Cells[row + 1, col].Value = "AnyStringHere"; 
like image 43
Selaka Nanayakkara Avatar answered Sep 30 '22 07:09

Selaka Nanayakkara