在一行中设置每个表格单元格的颜色

时间:2016-01-27 19:58:29

标签: java excel colors apache-poi tablecell

我有一个servlet,它接收一组数据,处理它,然后将它写入excel文件或文本页面,具体取决于表单提交。处理Excel时,所有处理都在使用Apache POI的相应模型中进行。我试图修改它,以便它根据包含的数据对行进行颜色编码,但是,在将颜色应用到行后,当我将工作簿写入fileoutputstream时,颜色不存在。我将数据处理成excel文件:

MCVE

package mcve;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Set;
import org.apache.poi.ss.usermodel.IndexedColors;
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 MCVE {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws FileNotFoundException, IOException {
    LinkedHashMap<Integer, String> testReport = new LinkedHashMap<>();

    testReport.put(0, "CPU");
    testReport.put(1, "App");
    testReport.put(2, "Other");
    testReport.put(3, "Memory");
    testReport.put(4, "Power");
    testReport.put(5, "Disk");

    File file = new File("C:/SplunkTesting/report.xlsx");
    FileOutputStream out = new FileOutputStream(file);
    writeToExcel(testReport).write(out);
    out.flush();
    out.close();
}

private static XSSFWorkbook writeToExcel(LinkedHashMap<Integer, String> formattedReport) {
    try {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet();
        int rowIndex = sheet.getLastRowNum();
        Set<Integer> keySet = formattedReport.keySet();
        for (Integer key : keySet) {
            XSSFRow row = sheet.createRow(rowIndex++);
            String line = formattedReport.get(key);
            String[] strArr = line.split(",");
            int cellCount = 0;
            for (String str : strArr) {
                XSSFCell cell = row.createCell(cellCount++);
                cell.setCellValue(str);
            }

            XSSFCellStyle style = workbook.createCellStyle();
            if (line.contains("App")) {
                style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
            } else if (line.contains("CPU")) {
                style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
            } else if (line.contains("Disk")) {
                style.setFillForegroundColor(IndexedColors.GOLD.getIndex());
            } else if (line.contains("Memory")) {
                style.setFillForegroundColor(IndexedColors.INDIGO.getIndex());
            } else if (line.contains("Network")) {
                style.setFillForegroundColor(IndexedColors.LAVENDER.getIndex());
            } else if (line.contains("Power")) {
                style.setFillForegroundColor(IndexedColors.MAROON.getIndex());
            } else {
                style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
            }
            style.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
            row.setRowStyle(style);
        }

        return workbook;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
}

我仍然获得excel文件,但格式化不存在。我究竟做错了什么?

1 个答案:

答案 0 :(得分:1)

我的解决方案是在单元格创建和值设置循环中设置样式。否则,创建时每个单元格都会覆盖行格式,这就是为什么它失败了。即使您在单元格创建后设置行格式,也是如此。