Convert jxl code to poi to read XLSX file
Recently I'm using jxl jar to read XLS excel file. But as I have to read XLSX excel file, I'm using poi jar. Here different classes are available while using poi jar. I have to read each cell of all available rows from sheet. While using jxl, I've buid below code...
private static class SRAccess implements RowAccess {
final Cell[] cells;
SRAccess(Cell[] cells) {
this.cells = cells;
}
public String[] get() {
String[] colValues = new String[cells.length];
for (int j = 0; j < cells.length; j++) {
colValues[j] = cells[j].getContents().trim();
}
return colValues;
}
public void set(String[] values) {
for (int i = 0; i < cells.length; ++i) {
if (i >= values.length)
break;
}
}
public void set(int colIx, String value) {
// cells[colIx].setContents(value);
}
}
public static int access(Sheet sheet, RowHandler rowHandler) {
int count = 0;
for (int i = 0; i < sheet.getRows(); i++) {
Cell cells[] = sheet.getRow(i);
boolean rc = rowHandler.handleRow(new SRAccess(cells), i);
if (!rc)
break;
++count;
}
return count;
}
What I'm facing here is, while using jxl, I'm able to read each row and thus all cells in that row by counting number of rows available in sheet (as shown in access() method). But while using poi, there is no such method available which gives row count directly. We have to use iterator for that. But I have to use these two functions, I want to know how can I know the Cell[] type and pass it to SRAcces() method?
链接地址: http://www.djcxy.com/p/37978.html上一篇: 1,同时阅读Java下载的xlsx文件