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));
}
}
原文地址:https://www.cnblogs.com/celtics/p/12684808.html