2017-09-18 3 views
0

質問があります。Javaでapache poiを使用した場合のセルの色が大きい

私はXLSファイルを作成するために、ApacheのPOIを使用したいと、それはほとんどここ を働いているが、私のコードは

public static void writeXLSFile(int L) throws IOException { 

    String excelFileName = "Test.xls";//name of excel file 
    String sheetName = "Sheet1";//name of sheet 


    HSSFWorkbook wb = new HSSFWorkbook(); 
    HSSFSheet sheet = wb.createSheet(sheetName); 

    int i = 0; 
    int l = 0; 
    int c = 0; 
    while (i < L+1) { 
    Row row = sheet.createRow((short) l); 
    Cell cell = row.createCell((short) c); 
    cell.setCellValue(i+1); 
    sheet.addMergedRegion(new CellRangeAddress(l, l + 9, c, c + 2)); 
    cell.setCellStyle(createBorderedStyle(wb)); 
     if (c>=5){ 
      c=0; 
      l=l+10; 
     }else { 
      c=c+3; 
     } 
     i++; 
    } 

    FileOutputStream fileOut = new FileOutputStream(excelFileName); 

    //write this workbook to an Outputstream. 
    wb.write(fileOut); 
    fileOut.flush(); 
    fileOut.close(); 
} 

private static CellStyle createBorderedStyle(Workbook wb2) { 
    CellStyle style = wb2.createCellStyle(); 
    style.setBorderRight(CellStyle.BORDER_THIN); 
    style.setRightBorderColor(IndexedColors.BLACK.getIndex()); 
    style.setBorderBottom(CellStyle.BORDER_THIN); 
    style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); 
    style.setBorderLeft(CellStyle.BORDER_THIN); 
    style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); 
    style.setBorderTop(CellStyle.BORDER_THIN); 
    style.setTopBorderColor(IndexedColors.BLACK.getIndex()); 
    style.setAlignment((short)2); 
    style.setVerticalAlignment((short)0); 

    return style; 
} 

である私が期待しresultatは3列と10行に黒の四角形に1とLの間の数であります最大3カラムの長さ。 私はそのような何かしたい:私はそれが理解できる願っています

https://image.noelshack.com/fichiers/2017/38/2/1505774253-uxbgme7s.png

は、それが あなたはとても値が最後を除いて迷子に同じ行を複数回(作成君たち

答えて

0

ありがとうございました、私の最初の投稿です1行に1つ)。境界は、最初のセルだけでなく、完全な範囲を囲む必要があります。これは、その問題を解決します:アライメントについて

//create the first row 
    Row row = sheet.createRow((short) l); 
    while (i < L+1) { 
     Cell cell = row.createCell((short) c); 
     cell.setCellValue(i+1); 
     sheet.addMergedRegion(new CellRangeAddress(l, l + 9, c, c + 2)); 

     //this adds the border 
     CellRangeAddress cellRangeAddress = new CellRangeAddress(l, l + 9 , c, c + 2); 
     HSSFRegionUtil.setBorderTop(CellStyle.BORDER_THIN, cellRangeAddress, sheet, wb); 
     HSSFRegionUtil.setBorderLeft(CellStyle.BORDER_THIN, cellRangeAddress, sheet, wb); 
     HSSFRegionUtil.setBorderRight(CellStyle.BORDER_THIN, cellRangeAddress, sheet, wb); 
     HSSFRegionUtil.setBorderBottom(CellStyle.BORDER_THIN, cellRangeAddress, sheet, wb); 

     cell.setCellStyle(createBorderedStyle(wb)); 
     if (c>=5){ 
      c=0; 
      l=l+10; 
      //go to the next row here 
      row = sheet.createRow((short) l); 
     }else { 
      c=c+3; 
     } 
     i++; 
    } 

を、これは役立つはず:

style.setAlignment(CellStyle.ALIGN_RIGHT); 
    style.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM); 
関連する問題