POI 设置字体多种颜色

来源:互联网 发布:粒子群优化算法 编辑:程序博客网 时间:2024/05/22 13:50
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/14164811public 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生成成功,点击工程目录中查看...");}}

0 0
原创粉丝点击