java - 使用 POI 进行合并的困惑

标签 java apache-poi

我正在 POI 中编写一个程序来合并单元格,并且我能够合并它们。有一个包含数字内容的列,当我合并此列并打开工作表并选择此列时,令我惊讶的是,它显示了计数和总和,就好像它没有合并一样。

下面是我的 Excel。

enter image description here

此处,count 应为 5sum 应为 530,但这显示为 252650

下面是我的代码。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;

public class RowsMerge {
    // public static void main(String[] args)
    public static void main(String[] args) throws IOException {
        FileInputStream fin = new FileInputStream(
                new File("C:\\D\\Sheets\\Quality Sheet\\Quality_template.xls"));
        HSSFWorkbook workbook = new HSSFWorkbook(fin);
        HSSFSheet sheet = workbook.getSheetAt(0);

        int row = sheet.getPhysicalNumberOfRows();
        String currentLawName, currentCountry, currentAssociate, previousLawName, previousCountry, previousAssociate;
        String currentPages, previousPages;
        int startIndex = 1, finalIndex = 0;

        System.out.println(row);
        for (int i = 2; i < row; i++) {
            currentAssociate = sheet.getRow(i).getCell(1).toString();
            currentLawName = sheet.getRow(i).getCell(3).toString();
            currentCountry = sheet.getRow(i).getCell(4).toString();
            currentPages = sheet.getRow(i).getCell(5).toString();

            previousAssociate = sheet.getRow(i - 1).getCell(1).toString();
            previousLawName = sheet.getRow(i - 1).getCell(3).toString();
            previousCountry = sheet.getRow(i - 1).getCell(4).toString();
            previousPages = sheet.getRow(i - 1).getCell(5).toString();

            if (currentAssociate.equals(previousAssociate) && currentCountry.equals(previousCountry)
                    && currentLawName.equals(previousLawName) && currentPages.equals(previousPages)) {
                finalIndex += 1;
                if (((i + 1) == row)) {
                    System.out.println("yes");
                    finalIndex += 1;
                    sendRangeToMergeCells(startIndex + 1, finalIndex - 1, sheet, workbook);
                }
            } else {
                sendRangeToMergeCells(startIndex + 1, finalIndex, sheet, workbook);
                startIndex = i;
                finalIndex = 0;
            }

        }
        FileOutputStream fileOut = new FileOutputStream("C:\\D\\Sheets\\Quality Sheet\\new.xls");
        workbook.write(fileOut);
        fileOut.close();
    }

    private static void sendRangeToMergeCells(int startIndex, int finalIndex, HSSFSheet sheet, HSSFWorkbook workbook) {
        System.out.println(startIndex + "\t" + (startIndex + finalIndex));

        CellRangeAddress region = CellRangeAddress.valueOf("F" + (startIndex) + ":F" + ((startIndex + finalIndex)));

        sheet.addMergedRegion(region);

    }

}

我该如何解决这个问题?

最佳答案

这可能是错误造成的,但解决方法可能是将行向上移动而不是合并它们。

public class RowsMerge {

// public static void main(String[] args)
public static void main(String[] args) throws IOException {
    FileInputStream fin = new FileInputStream(
            new File("C:\\D\\Sheets\\Quality Sheet\\Quality_template.xls"));
    HSSFWorkbook workbook = new HSSFWorkbook(fin);
    HSSFSheet sheet = workbook.getSheetAt(0);

    int lastRow = sheet.getLastRowNum();
    String currentLawName, currentCountry, currentAssociate, previousLawName, previousCountry, previousAssociate;
    String currentPages, previousPages;
    int rowCount = 0;

    System.out.println(row);
    for (int i = 2; i <= lastRow; i++) {
        currentAssociate = sheet.getRow(i).getCell(1).toString();
        currentLawName = sheet.getRow(i).getCell(3).toString();
        currentCountry = sheet.getRow(i).getCell(4).toString();
        currentPages = sheet.getRow(i).getCell(5).toString();

        previousAssociate = sheet.getRow(i - 1).getCell(1).toString();
        previousLawName = sheet.getRow(i - 1).getCell(3).toString();
        previousCountry = sheet.getRow(i - 1).getCell(4).toString();
        previousPages = sheet.getRow(i - 1).getCell(5).toString();

        if (currentAssociate.equals(previousAssociate) && currentCountry.equals(previousCountry)
                && currentLawName.equals(previousLawName) && currentPages.equals(previousPages)) {
            rowCount += 1;
            }
        } else {
            shiftRowsUp(sheet, i, rowCount);
            i -= rowCount;
            rowCount = 0;
        }
    }
    shiftRowsUp(sheet, i, rowCount);

    FileOutputStream fileOut = new FileOutputStream("C:\\D\\Sheets\\Quality Sheet\\new.xls");
    workbook.write(fileOut);
    fileOut.close();
}

private void shiftRowsUp(Sheet sheet, int startRow, int rowCount) {
    if (rowCount > 0) {
        int lastRow = sheet.getLastRowNum();
        if (lastRow - startRow < rowCount) {
            for (int i = startRow - rowCount; i < startRow: i++) {
                sheet.removeRow(sheet.getRow(i));
            }
        }
        sheet.shiftRows(startRow, lastRow, -rowCount);
    }
}

}

请注意,此循环假设电子表格的行中没有间隙。

关于java - 使用 POI 进行合并的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37415099/

相关文章:

java - 将 Java 对象作为值保存到 Redis

java - REST 身份验证

java - Spring Boot H2数据转换

java - 处理#REF!使用 apache poi 来自 Excel 的单元格

java - 将动态生成的表格导出到 Excel

java - 如何处理程序状态逻辑?

Java套接字通信——提高速度

java - 获取 HSSFWorkbook 的长度

命令行中的 Java 堆空间错误

java - Apache Poi,如何获取链接的工作簿?