1 回答

TA貢獻(xiàn)1794條經(jīng)驗 獲得超8個贊
單元格內(nèi)部使用圖案填充。填充背景色是圖案后面的顏色。填充前景色是圖案的顏色。
要使用純色填充單元格,您需要使用填充前景色和純色圖案。
...
cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
...
具有單元格填充和單元格內(nèi)容的完整示例:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateExcelCellFillColor {
?public static void main(String[] args) throws Exception {
? Workbook workbook = new XSSFWorkbook();
? //Workbook workbook = new HSSFWorkbook();
? CellStyle cellStyle = workbook.createCellStyle();
? cellStyle.setAlignment(HorizontalAlignment.CENTER);
? cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
? cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
? cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
? Sheet sheet = workbook.createSheet();
? Row row = sheet.createRow(0);
? Cell cell = row.createCell(0);
? cell.setCellValue("cell value");
? cell.setCellStyle(cellStyle);
? row.setHeightInPoints(50);
? sheet.setColumnWidth(0, 50 * 256);
? FileOutputStream out = null;
? if (workbook instanceof HSSFWorkbook) {
? ?out = new FileOutputStream("CreateExcelCellFillColor.xls");
? } else if (workbook instanceof XSSFWorkbook) {
? ?out = new FileOutputStream("CreateExcelCellFillColor.xlsx");
? }
? workbook.write(out);
? out.close();
? workbook.close();
?}
}
結(jié)果:
添加回答
舉報