2016-05-24 9 views
2

Ich schreibe einen Code, in dem einige Zeilen zusammengeführt werden sollen. Mit POI habe ich das erreicht. Aber das Problem ist nach dem Zusammenführen der Zeilen, die ich brauche, um sie in die Mitte zu bringen.vertikal Inhalt in fusionierten Zeilen ausrichten

Unten ist mein Code.

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

Bitte lassen Sie mich wissen, wie kann ich den Inhalt vertikal zentrieren.

Antwort

3

Um einer Zelle Ausrichtung (und andere Eigenschaften) hinzuzufügen, müssen Sie CellStyle einstellen.

Es gibt mehrere Möglichkeiten, dies zu tun, aber in Ihrem Fall haben Sie bereits eine Zelle mit Inhalt (das wäre die obere linke Ecke der verschmolzenen Region), so dass Sie CellUtil.setProperty() verwenden können.

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

Wenn Sie mehrere Eigenschaften festlegen müssen, sollten Sie 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); 

Lesen Sie mehr in der Apache POI Kurzanleitung verwenden: https://poi.apache.org/spreadsheet/quick-guide.html