POI 设置导出表中单元格颜色
Posted celtics
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POI 设置导出表中单元格颜色相关的知识,希望对你有一定的参考价值。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class TestXls {
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
XSSFWorkbook book = new XSSFWorkbook();
XSSFSheet st = book.createSheet();
XSSFCellStyle style = book.createCellStyle();
java.awt.Color clr_red = new java.awt.Color(255,0,0);
XSSFColor xssClr = new XSSFColor(clr_red);
style.setFillForegroundColor(xssClr);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
/*style.setFillForegroundColor(IndexedColors.RED1.getIndex()); //网上的这种设置法完全没起效果
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);*/
XSSFRow row = st.createRow(0);
XSSFCell cell = row.createCell(0);
cell.setCellStyle(style);
cell.setCellValue("1");
XSSFCell cell1 = row.createCell(1);
XSSFCellStyle style_gr = book.createCellStyle();
java.awt.Color clr_green = new java.awt.Color(0,255,0);
XSSFColor xssClr_gr = new XSSFColor(clr_green);// 主要用到XSSFColor
style_gr.setFillForegroundColor(xssClr_gr);
style_gr.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell1.setCellStyle(style_gr);
cell1.setCellValue("2");
File file = new File("c:\a.xlsx");
book.write(new FileOutputStream(file));
}
}
以上是关于POI 设置导出表中单元格颜色的主要内容,如果未能解决你的问题,请参考以下文章
POI导出excel:设置字体颜色行高自适应列宽自适应锁住单元格合并单元格...
POI 导出 Excel:字体颜色行列自适应锁住合并单元格一文搞定……
JAVA POI 导出 EXCEL模板 怎么设置单元格格式为 文字
JAVA POI 导出 EXCEL模板 怎么设置单元格格式为 文字