我面临的问题是我必须从第2行开始阅读第7列到第35列,并将相应的值映射到文档类。
无法确切知道代码应该如何?
List sheetData = new ArrayList();
InputStream excelFile = new BufferedInputStream(new FileInputStream("D:\\Excel file\\data.xlsx"));
Workbook workBook = new XSSFWorkbook(excelFile); // Creates Workbook
XSSFSheet sheet = (XSSFSheet) workBook.getSheet("Daily");
DataFormatter formatter = new DataFormatter();
for (int i = 7; i <= 35; i++) {
XSSFRow row = sheet.getRow(i);
Cell cell = row.getCell(i);
String val = formatter.formatCellValue(cell);
sheetData.add(val);
}
答案 0 :(得分:0)
你可以在文档中使用迭代器进行迭代,但是还有一个函数“getRow()和getCell()”
set CLASSPATH=""
答案 1 :(得分:0)
假设我已正确理解您的问题,我相信您希望处理从第2行开始到文件末尾的每一行,并且对于每一行,请考虑第7列到第35列中的单元格。我相信你也可能需要处理这些值,但你还没有说过,所以对于这个例子我只是将它们填入字符串列表并希望最好...
这是基于Apache POI documentation for iterating over rows and cells
File excelFile = new File("D:\\Excel file\\data.xlsx");
Workbook workBook = WorkbookFactory.create(excelFile);
Sheet sheet = workBook.getSheet("Daily");
DataFormatter formatter = new DataFormatter();
// Start from the 2nd row, processing all to the end
// Note - Rows and Columns in Apache POI are 0-based not 1-based
for (int rn=1; rn<=sheet.getLastRowNum(); rn++) {
Row row = sheet.getRow(rn);
if (row == null) {
// Whole row is empty. Handle as required here
continue;
}
List<String> values = new ArrayList<String>();
for (int cn=6; cn<35; cn++) {
Cell cell = row.getCell(cn);
String val = null;
if (cell != null) { val = formatter.formatCellValue(cell); }
if (val == null || val.isEmpty()) {
// Cell is empty. Handle as required here
}
// Save the value to list. Save to an object instead if required
values.append(val);
}
}
workBook.close();
根据您的业务需求,输入用于处理空白行和单元格的逻辑。然后,根据您的业务需求再次根据您找到的值执行任何操作!