POI--各种样式的XSSFCellStyle的生成

    //背景色、フォント色、枠線より各種XSSFCellStyleの作成して、cellStyleMapに保存する
    private HashMap<String, XSSFCellStyle> createXssfCellStyle() {
        HashMap<String, XSSFCellStyle> cellStyleMap = new HashMap<>();

        XSSFCellStyle xssfCellStyle;
        String mapKey = "";
        XSSFColor[] xssfColorArr = new XSSFColor[bgColorArr.length];
        XSSFFont[] xssfFontArr = new XSSFFont[fontColorArr.length];
        for(int bgColorIndex=0; bgColorIndex<bgColorArr.length; bgColorIndex++) {
            xssfColorArr[bgColorIndex] = createXssfColor(bgColorArr[bgColorIndex]);
        }
        for(int fontColorIndex=0; fontColorIndex<fontColorArr.length; fontColorIndex++) {
            xssfFontArr[fontColorIndex] = createXssfFont(fontColorArr[fontColorIndex]);
        }

        for (int bgColorIndex=0; bgColorIndex<bgColorArr.length; bgColorIndex++) {
            for(int fontColorIndex=0; fontColorIndex<fontColorArr.length; fontColorIndex++) {
                for(int rightBorderNameIndex=0; rightBorderNameIndex<borderNameArr.length; rightBorderNameIndex++ ) {
                    for (int bottomBorderNameIndex=0; bottomBorderNameIndex<borderNameArr.length; bottomBorderNameIndex++ ) {
                        for (int dataFormatIndex=0; dataFormatIndex<dataFormatArr.length; dataFormatIndex++) {
                            for (int wrapTextIndex=0; wrapTextIndex < wrapTextArr.length; wrapTextIndex++) {
                                xssfCellStyle = wb.createCellStyle();
                                xssfCellStyle.setFillForegroundColor(xssfColorArr[bgColorIndex]);
                                xssfCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                                xssfCellStyle.setFont(xssfFontArr[fontColorIndex]);
                                xssfCellStyle.setBorderTop(BorderStyle.HAIR);
                                xssfCellStyle.setBorderLeft(BorderStyle.HAIR);
                                xssfCellStyle.setBorderRight(BorderStyle.valueOf(borderNameArr[rightBorderNameIndex]));
                                xssfCellStyle.setBorderBottom(BorderStyle.valueOf(borderNameArr[bottomBorderNameIndex]));
                                xssfCellStyle.setDataFormat(dataFormatArr[dataFormatIndex]);
                                xssfCellStyle.setWrapText(wrapTextArr[wrapTextIndex]);

                                //right border name(MEDIUMやHAIR) + bottom border name(MEDIUMやHAIR) + font color(000000など) + background color (ffffffなど)
                                mapKey = borderNameArr[rightBorderNameIndex]
                                        + borderNameArr[bottomBorderNameIndex]
                                        + fontColorArr[fontColorIndex]
                                        + bgColorArr[bgColorIndex]
                                        + dataFormatArr[dataFormatIndex]
                                        + wrapTextKeyArr[wrapTextIndex];
                                cellStyleMap.put(mapKey, xssfCellStyle);
                            }

                        }

                    }
                }
            }
        }
        return cellStyleMap;
    }

    //右枠線のNAME、下枠線のNAME、フォント色、背景色より、合っているXSSFCellStyleを取得
    private XSSFCellStyle getXssfCellStyle(String rightBorderName, String bottomBorderName, String fontColor, String bgColor, int dataFormat, boolean isWrapped ) {
        String wrapTextKey = WRAP_TEXT_KEY_FALSE;
        if (isWrapped) {
            wrapTextKey = WRAP_TEXT_KEY_TRUE;
        }
        String mapKey = rightBorderName + bottomBorderName + fontColor + bgColor + dataFormat + wrapTextKey;
        if (!xssfCellStyleMap.containsKey(mapKey)) {
        }
        return xssfCellStyleMap.get(mapKey);
    }

  

转载于:https://www.cnblogs.com/gaoBlog/p/10724989.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java调用POI生成XLSX表格并写入本地的示例代码: ```java import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelWriter { public static void main(String[] args) throws IOException { // 创建工作簿 XSSFWorkbook workbook = new XSSFWorkbook(); // 创建工作表 XSSFSheet sheet = workbook.createSheet("Sheet1"); // 创建标题行 XSSFRow titleRow = sheet.createRow(0); // 设置标题行样式 XSSFCellStyle titleCellStyle = workbook.createCellStyle(); titleCellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 创建标题单元格并设置样式和值 XSSFCell titleCell = titleRow.createCell(0); titleCell.setCellStyle(titleCellStyle); titleCell.setCellValue("姓名"); titleCell = titleRow.createCell(1); titleCell.setCellStyle(titleCellStyle); titleCell.setCellValue("年龄"); titleCell = titleRow.createCell(2); titleCell.setCellStyle(titleCellStyle); titleCell.setCellValue("性别"); // 创建数据行 for (int i = 1; i <= 3; i++) { XSSFRow dataRow = sheet.createRow(i); // 创建数据单元格并设置值 XSSFCell dataCell = dataRow.createCell(0); dataCell.setCellValue("张三" + i); dataCell = dataRow.createCell(1); dataCell.setCellValue(20 + i); dataCell = dataRow.createCell(2); dataCell.setCellValue("男"); } // 保存工作簿到本地文件 FileOutputStream fos = new FileOutputStream("test.xlsx"); workbook.write(fos); fos.close(); // 释放工作簿资源 workbook.close(); } } ``` 该示例代码生成了一个包含标题行和3行数据的XLSX表格,并保存到本地文件test.xlsx中。可以根据需要修改表格的标题、数据和样式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值