码迷,mamicode.com
首页 > 其他好文 > 详细

POI 设置导出表中单元格颜色

时间:2020-04-12 14:17:41      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:tput   ram   put   public   exception   color   stat   tst   reg   

 

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 设置导出表中单元格颜色

标签:tput   ram   put   public   exception   color   stat   tst   reg   

原文地址:https://www.cnblogs.com/celtics/p/12684808.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!