无法理解如何在POI中合并行

时间:2016-05-24 07:26:45

标签: java apache-poi

我正在编写一个程序,我需要在Excel工作表中合并行。目前,我能够合并起始行,但是当结束时,我无法知道它出错的地方。以下是我的代码。

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) throws IOException {
        FileInputStream fin = new FileInputStream(
                new File("C:\\D\\Sheets\\Sample Sheets\\dummy.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, tempNum = 0;
        System.out.println(row);
        for (int i = 2; i < (row - 1); i++) {
            currentAssociate = sheet.getRow(i).getCell(0).toString();
            currentLawName = sheet.getRow(i).getCell(1).toString();
            currentCountry = sheet.getRow(i).getCell(2).toString();
            currentPages = sheet.getRow(i).getCell(3).toString();

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

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

            }

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

    private static void sendRangeToMergeCells(int startIndex, int finalIndex, HSSFSheet sheet) {
        System.out.println(startIndex + "\t" + (startIndex + finalIndex));
        CellRangeAddress region = CellRangeAddress
                .valueOf("D" + (startIndex + 1) + ":D" + ((startIndex + finalIndex) + 1));
        sheet.addMergedRegion(region);
    }

}

以下是我的Excel表格

SourceExcel:

enter image description here

当前输出:

enter image description here

预期输出:

enter image description here

1 个答案:

答案 0 :(得分:3)

实际上你的合并逻辑很好,它是你的中断逻辑(决定何时合并行的逻辑),但是它有点缺失。

如果电子表格中的最后一行与上一行匹配,则不会执行合并,因为执行将跟随if (currentAssociate.equals(previousAssociate) && ...的第一个分支并且循环结束。必要时,必须在for循环完成后最后一次测试并执行合并逻辑。

for循环后添加以下内容:

if (finalIndex > 0) {
    sendRangeToMergeCells(startIndex, finalIndex, sheet);
}

如果需要,这将合并最后一行。