JediusX
JediusX

Reputation: 71

Reading Excel with JXL, cell count per row changing between rows

I attempted to find a solution already, but nothing has come up that matches my problem. I'm using JXL to read an excel spreadsheet and convert each row into a specified object. Each cell within a row corresponds to a property of the object I'm creating. My spreadsheet has 41 columns and after reading 375 rows, the number of cells per row changes from 41 to 32. I can't figure out why.

Here's the code where I'm looping through rows and retrieving the cells:

  w = Workbook.getWorkbook(inputWorkbook);
  // Get the first sheet
  Sheet sheet = w.getSheet(0);
  // Loop over first 10 column and lines

  for (int row=1; row < sheet.getRows();row++)
  {
      EventData event = new EventData();
      // we skip first row bc that should be header info
      //now iterate through columns in row
      try
      {
          Cell[] cell = sheet.getRow(row);

          event.Name = cell[0].getContents();
          event.Location = cell[1].getContents();

The rest of the code continues to grab the contents of each cell and assign them accordingly. But when attempting to access cell[32] on row 376, I get an out of bounds exception.

Upvotes: 7

Views: 6953

Answers (2)

Maverick
Maverick

Reputation: 1599

Instead of accessing the cell data as:

Cell[] cell = sheet.getRow(row);
event.Name = cell[0].getContents();
event.Location = cell[1].getContents();

Try to access the data as:

sheet.getCell(column, row);

Complete code would be:

w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(0);
// Loop over first 10 column and lines

for (int row=1; row < sheet.getRows();row++)
{
    EventData event = new EventData();
    // we skip first row bc that should be header info
    //now iterate through columns in row
    try
    {
        event.Name = sheet.getCell(0, row).getContents();
        event.Location = sheet.getCell(1, row).getContents();
    }
}

Upvotes: 0

Niek
Niek

Reputation: 1019

Could it not just be that everything after cell[32] on that row is empty and thus cell[32] (and up) in the array are not created at all? Am now just starting with jxl and I think that's what I'm seeing

Upvotes: 3

Related Questions