java - 如何在 Apache Poi 上使用子表?

标签 java excel apache-poi

我想知道如何使用 xls 文件的片段以及如何使用片段的元素(单元格、行)进行操作。

我不使用整行。

这里是文档查找:

enter image description here

我只需要使用代码片段进行操作,如下:

enter image description here

以下是与 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/

相关文章:

java - Java中如何从文件名中获取零件数据?

java - 什么是NullPointerException,我该如何解决?

excel - 是否可以使用 VLOOKUP 返回公式?

Java输出流,在从数据库检索所有数据之前触发文件下载

java - 将 lambda 方法从 Java 迁移到 Kotlin 时出现 "Type mismatch"

java - appengine 上的 slim3 分布式事务

excel - Powershell 和 Excel,使用 .replace 区分大小写

vba - excel vba中打印和打印预览事件的区别

java - 跳过 Apache POI 中的空白 Excel 单元格

java - 使用 java 将数据列表写入 Excel 文件中的特定列所需的帮助