我想知道如何使用 xls
文件的片段以及如何使用片段的元素(单元格、行)进行操作。
我不使用整行。
这里是文档查找:
我只需要使用代码片段进行操作,如下:
以下是与 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 上使用子表?
一般我想知道如何使用 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
一起使用?
有什么建议吗?
最佳答案
我认为这样的东西适合你:
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
}
}
当然,您可以创建自定义结构来仅访问这部分。
关于java - 如何在 Apache Poi 上使用子表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27606398/