2016-05-24 9 views
2

マージする行があるコードを書いています。 POIを使用して、私はこれを達成しました。しかし問題は、それらを中心にするために必要な行をマージした後です。verticalマージされた行のコンテンツの整列

以下は私のコードです。

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); 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; 
       if (((i + 1) == row)) { 
        System.out.println("yes"); 
        finalIndex += 1; 
        sendRangeToMergeCells(startIndex + 1, finalIndex - 1, sheet); 
       } 
      } else { 
       sendRangeToMergeCells(startIndex + 1, 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("B:" + startIndex + "\tB:" + (finalIndex + startIndex) + "\t else"); 
     CellRangeAddress region = CellRangeAddress.valueOf("D" + (startIndex) + ":D" + ((finalIndex + startIndex))); 
     sheet.addMergedRegion(region); 
    } 

コンテンツを垂直方向に中央に配置する方法を教えてください。

答えて

3

アライメント(およびその他のプロパティ)をセルに追加するには、CellStyleを設定する必要があります。

これを行うにはいくつかの方法がありますが、あなたの場合は既にコンテンツがあるセル(マージされた領域の左上隅)があるので、CellUtil.setProperty()を使用できます。一度に複数のプロパティを設定する必要がある場合

CellUtil.setCellProperty(cell, CellUtil.VERTICAL_ALIGNMENT, CellStyle.VERTICAL_CENTER); 

、あなたが使用する必要がありますCellUtil.setCellProperties()

Map<String, Object> properties = new HashMap<String, Object>(); 

// border around a cell 
properties.put(CellUtil.BORDER_TOP, CellStyle.BORDER_MEDIUM); 
properties.put(CellUtil.BORDER_BOTTOM, CellStyle.BORDER_MEDIUM); 
properties.put(CellUtil.BORDER_LEFT, CellStyle.BORDER_MEDIUM); 
properties.put(CellUtil.BORDER_RIGHT, CellStyle.BORDER_MEDIUM); 

// Give it a color (RED) 
properties.put(CellUtil.TOP_BORDER_COLOR, IndexedColors.RED.getIndex()); 
properties.put(CellUtil.BOTTOM_BORDER_COLOR, IndexedColors.RED.getIndex()); 
properties.put(CellUtil.LEFT_BORDER_COLOR, IndexedColors.RED.getIndex()); 
properties.put(CellUtil.RIGHT_BORDER_COLOR, IndexedColors.RED.getIndex()); 

// Apply it to a cell  
CellUtil.setCellStyleProperties(cell, properties); 

以上のApache POIクイックガイドで読む:https://poi.apache.org/spreadsheet/quick-guide.html

関連する問題