import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//学习地址:http://www.yiibai.com/apache_poi/apache_poi_cells.html
//http://www.ithao123.cn/content-9901497.html
//SXSSFWorkbook CellStyle 如何更改背景色:
//http://blog.csdn.net/downkang/article/details/14164811
public class TestColor {
public static void main(String[] args) throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet spreadsheet = workbook.createSheet("Fontstyle");
//数字2的意思是第2行
XSSFRow row = spreadsheet.createRow(2);
//设置字体
XSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 30);
font.setFontName("IMPACT");
font.setItalic(true);
font.setColor(HSSFColor.GREEN.index);
//设置样式
XSSFCellStyle style = workbook.createCellStyle();
style.setFont(font);
//5的意思是第5列
XSSFCell cell = row.createCell(5);
XSSFRichTextString richString = new XSSFRichTextString( "asdfxghijkadfasdfad" );
//对"Hello,"设置redFont字体
richString.applyFont( 4, 8, font );
//设置单元格中的数据
cell.setCellValue(richString);
//将样式给了单元格
cell.setCellStyle(style);
//输出文件
FileOutputStream out = new FileOutputStream(new File("fontstyle.xlsx"));
workbook.write(out);
//关闭流文件
workbook.close();
out.close();
System.out.println("Excel生成成功,点击工程目录中查看...");
}
}
POI 设置字体多种颜色
最新推荐文章于 2024-02-29 17:28:54 发布