如何在Apache Poi上使用子表格?

我想知道如何使用xls文件的片段,并使用代码片段(单元格,行)的元素进行操作。

我不用整行工作。

这是文件看:

在这里输入图像说明

我只需要使用snippet来操作,如下所示:

在这里输入图像说明

这里是与JXL一起使用的代码片段:

 public class AncillariesWithPoi { private Workbook workbook; private EntryParser shortParser; private EntryParser longParser; private Sheet sheet; private Cell lowShortStart; private Cell lowShortEnd; private Cell highShortStart; private Cell highShortEnd; private Cell lowLongStart; private Cell lowLongEnd; private Cell highLongStart; private Cell highLongEnd; public void setDefault() { setRowKey(new CellReference("C30"), new CellReference("AR30")); setLowSeasonShort("D30", "U40"); setHighSeasonShort("W30", "AN40"); setLowSeasonLong("AR30", "BI42"); setHighSeasonLong("BK30", "CC42"); } public void setRowKey(CellReference firstRef, CellReference lastRef) { shortParser.rowKey = firstRef.getCol(); longParser.rowKey = lastRef.getCol(); } public void setLowSeasonShort(String startCell, String endCell) { this.lowShortStart = sheet.getCell(startCell); this.lowShortEnd = sheet.getCell(endCell); } public void setHighSeasonShort(String startCell, String endCell) { this.highShortStart = sheet.getCell(startCell); this.highShortEnd = sheet.getCell(endCell); // ommited rest of 

如何在apachte poi上使用子表格?

Generall我想知道如何用apache poi API实现下一个方法:

  public void setLowSeasonShort(String startCell, String endCell) { this.lowShortStart = sheet.getCell(startCell); this.lowShortEnd = sheet.getCell(endCell); } 

我在poi发现的一个可能性:

 for (Row row : sheet1) { for (Cell cell : row) { CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex()); System.out.print(cellRef.formatAsString()); System.out.print(" - "); switch (cell.getCellType()) { // work with cell content } } } 

它正在从表格的每一行迭代单元格。

如何重写setLowSeasonShort(String startCell, String endCell)来使用poi api

有什么build议么?

我觉得像这样的东西会为你工作:

 CellRangeAddress range = CellRangeAddress.valueOf(startCell + ":" + endCell); for (int row = range.getFirstRow(); row <= range.getLastRow(); ++row) { for (int col = range.getFirstColumn(); col <= range.getLastColumn(); ++col) { Cell cell = sheet.getRow(row).getCell(col); //do something with cell } } 

自然你可以创build一个自定义的结构来访问这个部分。