2016-05-24 3 views
0

私はPOIでセルをマージするプログラムを書いています。私はそれらをマージすることができます。数値の列を持つ列があり、この列を結合するときにシートを開き、この列を選択すると驚いたことに、この列が合計されていないかのようにカウントと合計が表示されます。POIを使ったマージとの混同

以下は私のExcelです。ここで

enter image description here

count5sumあるべき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

これはバグかもしれません。 Excelは、マージされた領域の左上のセルにのみ値を保持します。 POIはすべての値を保持しているようです。この問題をhttps://bz.apache.org/bugzilla/buglist.cgi?list_id=146416&product=POIでお知らせください。問題を再現するのに必要な最小限のコードを含めてください。 – jmarkmurphy

答えて

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

} 

メモこのループは、スプレッドシートの行にギャップがないことを前提としています。

関連する問題