POI 单元格样式CellStyle

x33g5p2x  于2021-12-28 转载在 其他  
字(2.8k)|赞(0)|评价(0)|浏览(429)

1. 单元格样式

CellStyle决定了单元格的显示样式,决定了单元格:

- 单元格各个边框样式及颜色  - 单元格填充图案、前景色及背景色  - 单元格内容水平、垂直对齐方式  - 单元格内容字体Font  - 单元格内容格式化DataFormat  - 单元格内容是否换行  - 单元格内容旋转

2. 单元格样式个数限制

工作簿Workbook中单元格样式个数是有限制的,所以在 程序中应该重复使用相同CellStyle,而不是为每个单元 格创建一个CellStyle

HSSFCellStyle - 4000个
XSSFCellStyle - 64000个

3. 创建单元格样式

1. workbook创建一个单元格样式cellStyle
 2. 设置属性
 3. 单元格设置样式cellStyle
CellStyle style = workbook.createCellStyle();
cell.setCellStyle(style);

4. 实例

package hssf.sheet.color;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExportXSSFColor {

    public static void main(String[] args) throws Exception {
        File file = new File("C:\\Users\\Administrator\\Desktop\\test.xlsx");
        if (file.exists()) {
            file.delete();
        }
        BufferedOutputStream out = null;
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test.xlsx");
            out = new BufferedOutputStream(fileOutputStream);
            exportExcel(out);
        } finally {
            out.close();
        }
    }

    /** * @param out * @throws IOException */
    private static void exportExcel(BufferedOutputStream out) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        sheet.setColumnWidth(0, 20*256);
        Row row = sheet.createRow((short) 0);
        Cell cell = row.createCell((short) 0);
        cell.setCellValue("调色板");

        // 创建一个单元格样式
        XSSFCellStyle style = workbook.createCellStyle();
        cell.setCellStyle(style);

        /*****************************使用默认颜色**************************************************/
        // 填充色
//      style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
        style.setFillForegroundColor(HSSFColorPredefined.BLUE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        // 字体颜色
        Font font = workbook.createFont();
        font.setColor(IndexedColors.WHITE.getIndex());
        style.setFont(font);
        /**************************************************************************************/

        /*****************************自定义颜色**************************************************/
        XSSFColor color = new XSSFColor(new java.awt.Color(255, 0, 0));
        style.setFillForegroundColor(color);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        XSSFColor color1 = new XSSFColor(new java.awt.Color(0, 0, 0));
        // 字体颜色
        XSSFFont font1 = workbook.createFont();
        font1.setColor(color1);
        style.setFont(font1);
        /**************************************************************************************/

        workbook.write(out);
    }
}

相关文章