[英]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. 我正在使用JXL读取excel电子表格并将每行转换为指定的对象。 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. 我的电子表格有41列,读完375行后,每行的单元格数从41变为32.我无法弄清楚原因。
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. 但是当试图访问第376行的单元格[32]时,我得到一个超出范围的异常。
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? 难道不仅仅是那行上的单元格[32]之后的所有内容都是空的,因此数组中的单元格[32](和向上)根本不会创建吗? Am now just starting with jxl and I think that's what I'm seeing 我现在刚刚开始使用jxl,我认为这就是我所看到的
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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.