java - 在 Apache POI 中设置样式

标签 java excel 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

如何只在标题中设置样式?

最佳答案

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 代码移到 for 循环之外

关于java - 在 Apache POI 中设置样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43467253/

相关文章:

java - 使用 Selenium Apache POI Java webdriver 在 setCellValue() 的数据表 excel 中未打印值

java - 在 OpenFire 中刷新 VCard

excel - VBA 搜索值并从列表中删除(for 循环太慢)

java - 使用 apache poi 读取 .xlsx 文件在 linux 机器上给出 org.apache.poi.POIXMLException

尝试使用 Apache POI 读取 MapReduce 中的 Word 文档时出现 Java.lang.ClassNotFoundException 错误

java - 使用带有前导零的 POI HSSFCell 读取 Excel

java - 执行 JFrame 程序

java - 在 Guice 中使用 Mapbinder 在模块中使用 Inject

java - 通过显式指定类型见证来调用类构造函数

vba - Excel 中的 Munging 时间序列