使用Apache POI如何读取特定的excel列

我在使用Apache POI时遇到了问题。 我可以跨行读取,但有时候我只是想阅读某一列。

那么是否有可能只阅读任何特定的列,例如只有“A”列或只有“C”列。

我正在使用Java语言。

heikkim是正确的,这里是一些示例代码适应从我有一些代码:

import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Row; ... for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) { row = sheet.getRow(rowIndex); if (row != null) { Cell cell = row.getCell(colIndex); if (cell != null) { // Found column and there is value in the cell. cellValueMaybeNull = cell.getStringCellValue(); // Do something with the cellValueMaybeNull here ... } } } 

对于colCount使用类似row.getPhysicalNumberOfCells()

  Sheet sheet = workBook.getSheetAt(0); // Get Your Sheet. for (Row row : sheet) { // For each Row. Cell cell = row.getCell(0); // Get the Cell at the Index / Column you want. } 

我的解决scheme,更简单的代码明智的。

你可以循环行,并从每一行读取相同的单元格(不包括列?)。

好的,从你的问题,你只是想读一个特定的列。 所以,当遍历一行然后遍历其单元格时,可以简单地检查列的索引。

 Iterator<Row> rowIterator = mySheet.iterator(); // Traversing over each row of XLSX file while (rowIterator.hasNext()) { Row row = rowIterator.next(); // For each row, iterate through each columns Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); println "column index"+cell.getColumnIndex()//You will have your columns fixed in Excel file if(cell.getColumnIndex()==3)//for example of c { print "done" } } } 

我正在使用POI 3.12–'org.apache.poi:poi:3.12'希望它有帮助。 干杯!

这里是按列读取excel数据的代码。

 public ArrayList<String> extractExcelContentByColumnIndex(int columnIndex){ ArrayList<String> columndata = null; try { File f = new File("sample.xlsx") FileInputStream ios = new FileInputStream(f); XSSFWorkbook workbook = new XSSFWorkbook(ios); XSSFSheet sheet = workbook.getSheetAt(0); Iterator<Row> rowIterator = sheet.iterator(); columndata = new ArrayList<>(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); if(row.getRowNum() > 0){ //To filter column headings if(cell.getColumnIndex() == columnIndex){// To match column index switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: columndata.add(cell.getNumericCellValue()+""); break; case Cell.CELL_TYPE_STRING: columndata.add(cell.getStringCellValue()); break; } } } } } ios.close(); System.out.println(columndata); } catch (Exception e) { e.printStackTrace(); } return columndata; } 
 import java.io.*; import org.apache.poi.hssf.util.CellReference; import org.apache.poi.ss.usermodel.*; import java.text.*; public class XSLXReader { static DecimalFormat df = new DecimalFormat("#####0"); public static void main(String[] args) { FileWriter fostream; PrintWriter out = null; String strOutputPath = "H:\\BLR_Team\\Kavitha\\Excel-to-xml\\"; String strFilePrefix = "Master_5.2-B"; try { InputStream inputStream = new FileInputStream(new File("H:\\BLR_Team\\Kavitha\\Excel-to-xml\\Stack-up 20L pure storage 11-0039-01 ISU_USA-A 1-30-17-Rev_exm.xls")); Workbook wb = WorkbookFactory.create(inputStream); // Sheet sheet = wb.getSheet(0); Sheet sheet =null; Integer noOfSheets= wb.getNumberOfSheets(); for(int i=0;i<noOfSheets;i++){ sheet = wb.getSheetAt(i); System.out.println("Sheet : "+i + " " + sheet.getSheetName()); System.out.println("Sheet : "+i + " " + sheet.getFirstRowNum()); System.out.println("Sheet : "+i + " " + sheet.getLastRowNum()); //Column 29 fostream = new FileWriter(strOutputPath + "\\" + strFilePrefix+i+ ".xml"); out = new PrintWriter(new BufferedWriter(fostream)); out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); out.println("<Bin-code>"); boolean firstRow = true; for (Row row : sheet) { if (firstRow == true) { firstRow = false; continue; } out.println("\t<DCT>"); out.println(formatElement("\t\t", "ID", formatCell(row.getCell(0)))); out.println(formatElement("\t\t", "Table_name", formatCell(row.getCell(1)))); out.println(formatElement("\t\t", "isProddaten", formatCell(row.getCell(2)))); out.println(formatElement("\t\t", "isR3P01Data", formatCell(row.getCell(3)))); out.println(formatElement("\t\t", "LayerNo", formatCell(row.getCell(29)))); out.println("\t</DCT>"); } CellReference ref = new CellReference("A13"); Row r = sheet.getRow(ref.getRow()); if (r != null) { Cell c = r.getCell(ref.getCol()); System.out.println(c.getRichStringCellValue().getString()); } for (Row row : sheet) { for (Cell cell : row) { CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex()); 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; case Cell.CELL_TYPE_BLANK: System.out.println(); break; default: System.out.println(); } } } out.write("</Bin-code>"); out.flush(); out.close(); } } catch (Exception e) { e.printStackTrace(); } } private static String formatCell(Cell cell) { if (cell == null) { return ""; } switch(cell.getCellType()) { case Cell.CELL_TYPE_BLANK: return ""; case Cell.CELL_TYPE_BOOLEAN: return Boolean.toString(cell.getBooleanCellValue()); case Cell.CELL_TYPE_ERROR: return "*error*"; case Cell.CELL_TYPE_NUMERIC: return XSLXReader.df.format(cell.getNumericCellValue()); case Cell.CELL_TYPE_STRING: return cell.getStringCellValue(); default: return "<unknown value>"; } } private static String formatElement(String prefix, String tag, String value) { StringBuilder sb = new StringBuilder(prefix); sb.append("<"); sb.append(tag); if (value != null && value.length() > 0) { sb.append(">"); sb.append(value); sb.append("</"); sb.append(tag); sb.append(">"); } else { sb.append("/>"); } return sb.toString(); } } 

这段代码做了三件事情:

  1. Excel到XML文件生成。 工程。 姓名董金
  2. 打印特定单元格的内容:A13
  3. 同时将excel内容打印成普通文本格式。 要导入的jar子:poi-3.9.jar,poi-ooxml-3.9.jar,poi-ooxml-schemas-3.9.jar,xbea n-2.3.0.jar,xmlbeans -xmlpublic-2.4.0.jar ,dom4j的-1.5.jar