使用 java excel sheet 中的列名称着色

coloring of column name in excel sheet using java

我正在使用 java 在 excel sheet 中存储一些文件。我需要给列名上色。我该如何处理这个。我想用不同的颜色为不同的列着色。

resultMap.put("Question", question);
resultMap.put("Option a", options.get(0));
resultMap.put("Option b", options.get(1));
resultMap.put("Option c", options.get(2));
resultMap.put("Option d", options.get(3));

问题,a选项,b选项,c选项,d选项是我存的五列。 excel sheet 为 .xlsx 格式

XSSFWorkbook myWorkBook = new XSSFWorkbook ();

            // Return first sheet from the XLSX workbook
XSSFSheet mySheet = myWorkBook.createSheet(sheetName);
XSSFRow row = mySheet.createRow(0);
row.createCell(7).setCellValue("Question");
row.createCell(8).setCellValue("Option a");
row.createCell(9).setCellValue("Option b");
row.createCell(10).setCellValue("Option c");
row.createCell(11).setCellValue("Option d");
FileOutputStream fileOut = new FileOutputStream(outputFilePath);
myWorkBook.write(fileOut);
fileOut.close();
myWorkBook.close();

假设您使用的是 Apache POI,您可以执行如下操作,或查看此页面以获取更多信息和示例:http://poi.apache.org/spreadsheet/quick-guide.html#FillsAndFrills

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExCell {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {

    Workbook myWorkBook = new XSSFWorkbook();

    Sheet mySheet = myWorkBook.createSheet("Main");
    Row row = mySheet.createRow(0);

    Font font = myWorkBook.createFont();
    font.setColor(IndexedColors.BLUE.getIndex());
    CellStyle style = myWorkBook.createCellStyle();
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
    style.setFont(font);

    Cell newCell = row.createCell(7);
    newCell.setCellValue("Question");
    newCell.setCellStyle(style);

    row.createCell(8).setCellValue("Option a");
    row.createCell(9).setCellValue("Option b");
    row.createCell(10).setCellValue("Option c");
    row.createCell(11).setCellValue("Option d");

    FileOutputStream fileOut = new FileOutputStream("Sandeep.xlsx");

    myWorkBook.write(fileOut);
    fileOut.close();
    myWorkBook.close();

    System.out.println("Done");
}

}