使用java读取excel表单的一列

我有一个Excel表。 我想写一个参数作为要读取的列号的方法,并返回包含该列中所有数据的数组。

然后将该列元素放在xml表单中。 我怎么能写一个方法来做到这一点?

使用Apache POI。 你可以在他们的使用页面find例子。

Sheet sheet1 = wb.getSheetAt(0); for (Row row : sheet1) { for (Cell cell : row) { // Here you might have to use the cell number to limit what you want. CellReference cellRef = new CellReference(row.getRowNum(), cell.getCellNum()); System.out.print(cellRef.formatAsString()); System.out.print(" - "); switch(cell.getCellType()) { case Cell.CELL_TYPE_STRING: System.out.println(cell.getRichStringCellValue().getString()); break; case Cell.CELL_TYPE_NUMERIC: if(DateUtil.isCellDateFormatted(cell)) { System.out.println(cell.getDateCellValue()); } else { System.out.println(cell.getNumericCellValue()); } break; case Cell.CELL_TYPE_BOOLEAN: System.out.println(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: System.out.println(cell.getCellFormula()); break; default: System.out.println(); } } } 

您可能会发现这个答案有帮助: 如何使用Apache POI从Excel文件中获取列

这就包含了从一列中提取所有数值的所有代码,无论它们是数字单元格还是包含数字结果的公式单元格。 我怀疑你会需要这样的东西。