简体   繁体   English

在Java中使用apache poi获得更大的单元格颜色

[英]bigger cell color using apache poi in java

I got a question for you. 我有一个问题要问你。

I want to use apache poi to create a xls file and it's almost working Here is my code 我想使用apache poi创建一个xls文件,它几乎可以正常工作这是我的代码

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

The resultat I expect is number between 1 and L in black rectangle with 3 column and 10 row with 3 column long max. 我期望的结果是黑色矩形中1到L之间的数字,最大3列,最大10行,最大3列。 I want something like that : 我想要这样的东西:

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

I hope it's understandable, it's my first post Thank you guys 我希望这是可以理解的,这是我的第一篇文章谢谢大家

You create the same row multiple times (so the values get lost except for the last one in a row). 您多次创建同一行(因此,除了最后一行中的值之外,其他所有值均会丢失)。 The borders should also be drawn around the complete range, not just the first cell. 还应围绕整个范围绘制边框,而不仅仅是第一个单元格。 This would fix that problem: 这将解决该问题:

    //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++;
    }

About the alignment, this should help: 关于对齐,这应该会有所帮助:

    style.setAlignment(CellStyle.ALIGN_RIGHT);
    style.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM