2017-04-18 246 views
0

我正在設置標題的樣式,我最後一列中的一個也獲得了爲標題設置的樣式。這是我設置設置風格的代碼。在Apache POI中設置樣式

 XSSFWorkbook workbook = new XSSFWorkbook(); 
    XSSFSheet sheet = workbook.createSheet(sheetName); 

    for (int i = 0; i < numberOfMergedRow; i++) { 
     sheet.addMergedRegion(new CellRangeAddress(i, i, 0, numberOfColumns - 1)); 
     sheet.autoSizeColumn(i); 
    } 

    XSSFRow row = sheet.createRow(0); 
    int l = row.getLastCellNum() + 1; 
    XSSFCell cell = row.createCell((short) l); 
    XSSFCellStyle cellStyle = workbook.createCellStyle(); 
    Font headerFont = workbook.createFont(); 

    Set<Integer> keyset = data.keySet(); 
    int rownum = 0; 
    for (Integer key : keyset) { 
     row = sheet.createRow(rownum++); 
     Object[] objArr = data.get(key); 
     int cellnum = 0; 
     for (Object obj : objArr) { 
      cell = row.createCell(cellnum++); 
      if (obj instanceof String) 
       cell.setCellValue((String) obj); 
      else if (obj instanceof Integer) 
       cell.setCellValue((Integer) obj); 
      else if (obj instanceof Double) 
       cell.setCellValue((Double) obj); 
      else if (obj instanceof Number) 
       cell.setCellValue((Double) obj); 
     } 

     cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); 
     cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); 

     /* adding heading style */ 
     cellStyle.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex()); 
     cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); 
     headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD); 

     headerFont.setFontHeightInPoints((short) 12); 
     cellStyle.setFont(headerFont); 

     cell.setCellStyle(cellStyle); 

my output

我怎麼只能在頭設置的樣式?

+0

使用兩種風格,一個是頭,一個用於休息嗎? – Gagravarr

+0

@ Gagravarr,我應該只使用風格的標題,我只是設置標題的風格,事情是我在數據部分列如何? – pralad

回答

1
XSSFWorkbook workbook = new XSSFWorkbook(); 
XSSFSheet sheet = workbook.createSheet(sheetName); 
for (int i = 0; i < numberOfMergedRow; i++) { 
    sheet.addMergedRegion(new CellRangeAddress(i, i, 0, numberOfColumns - 1)); 
    sheet.autoSizeColumn(i); 
} 

XSSFRow row = sheet.createRow(0); 
int l = row.getLastCellNum() + 1; 
XSSFCell cell = row.createCell((short) l); 
XSSFCellStyle cellStyle = workbook.createCellStyle(); 
Font headerFont = workbook.createFont(); 

cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); 
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); 

/* adding heading style */ 
cellStyle.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex()); 
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); 
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD); 

headerFont.setFontHeightInPoints((short) 12); 
cellStyle.setFont(headerFont); 

cell.setCellStyle(cellStyle); 

Set<Integer> keyset = data.keySet(); 
int rownum = 0; 
for (Integer key : keyset) { 
    row = sheet.createRow(rownum++); 
    Object[] objArr = data.get(key); 
    int cellnum = 0; 
    for (Object obj : objArr) { 
     cell = row.createCell(cellnum++); 
     if (obj instanceof String) 
      cell.setCellValue((String) obj); 
     else if (obj instanceof Integer) 
      cell.setCellValue((Integer) obj); 
     else if (obj instanceof Double) 
      cell.setCellValue((Double) obj); 
     else if (obj instanceof Number) 
      cell.setCellValue((Double) obj); 
    } 

移動之外的cellStyle代碼迴路

+0

我根本沒有風格,不在標題部分。 – pralad