poi设置excel字体颜色

2025-11-06 17:15:50

1、第一步:新建一个javaweb项目。

打开myeclipse file --》new--》web project 

具体操作如下图所示:

poi设置excel字体颜色

poi设置excel字体颜色

poi设置excel字体颜色

poi设置excel字体颜色

2、第二步:编写导出代码。

1、在项目中引入poi的依赖包poi-3.17.jar。

2、编写导出的代码

public static void main(String[] args) throws Exception{

//1.创建Excel对象

        Workbook wb =new HSSFWorkbook();

        //2.创建表格的sheet页

        Sheet sheet = wb.createSheet();

        //3.创建行

        Row row = sheet.createRow(3);

        //4.1创建列

        Cell cell = row.createCell(3);

        //4.2设置列宽便于展示

        sheet.setColumnWidth(3, 10000);

        //5设置内容

        cell.setCellValue("有困难找度娘");

        //6.设置样式

        //6.1创建字体

        Font font = wb.createFont();

        font.setFontHeightInPoints((short) 36);

        font.setFontName("华文琥珀");

        font.setColor(Font.COLOR_RED);

        //6.2创建单元格格式CellStyle

        CellStyle cellStyle = wb.createCellStyle();

        cellStyle.setFont(font);

        //6.3字体作用单元格

        cell.setCellStyle(cellStyle);

        //7.写入到硬盘

        //7.1创建流

        FileOutputStream os = new FileOutputStream(new File("E:/file/export.xls"));

        //7.2将wb的内容写入字符流中

        wb.write(os);

        //7.3关流

        os.flush();

        os.close();

    }

poi设置excel字体颜色

poi设置excel字体颜色

poi设置excel字体颜色

poi设置excel字体颜色

3、第三步:测试功能。

在测试类中右键执行run命令。如下图所示:

poi设置excel字体颜色

poi设置excel字体颜色

poi设置excel字体颜色

4、补充:font.setColor(Font.COLOR_RED);

1、poi文档提供了几个变量设置字体颜色。

2、自定义数据可以设置更多的颜色,不过如下

font.setColor((short)5); 黄色

font.setColor((short)7); 粉色

poi设置excel字体颜色

poi设置excel字体颜色

poi设置excel字体颜色

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢