使用POI与合并混淆

时间:2016-05-24 13:28:24

标签: 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);

    }

}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

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

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);
    }
}

}

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