JAVA:POI设置EXCEL单元格格式

目录

1.Maven引入

2.单元格样式设置

 3.单元格值设置

3.1.设置单元格为文本格式

3.2.设置单元格为日期格式

3.3.设置单元格数值格式

3.4.设置单元格为货币格式

3.5.设置单元格为百分比格式

3.6.设置单元格为中文大写格式

3.7.设置单元格为科学计数法格式


本文将介绍POI Excel for Java的格式设置基本用法,包括:单元格样式设置、值设置(文本、小数、百分比、货币、日期、科学计数法和中文大写等)。

1.Maven引入

<poi.version>3.14</poi.version>
 
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>${poi.version}</version>
        </dependency>

2.单元格样式设置

使用Aspose Excel for Java可以方便地设置Excel文件中的样式。下面是一个简单的设置单元格样式的示例代码:

CellStyle cellStyle=wb.createCellStyle(); // 创建单元格样式
cellStyle.setAlignment(HorizontalAlignment.LEFT);  // 设置单元格水平方向对其方式
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); // 设置单元格垂直方向对其方式

cellStyle.setFillForegroundColor(IndexedColors.BROWN.getIndex());//设置背景颜色cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex()); // 设置前景颜色

cellStyle.setBorderBottom(CellStyle.BORDER_THIN); // 底部边框  cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 底部边框颜色

cellStyle.setBorderLeft(CellStyle.BORDER_THIN); // 左边边框

cellStyle.setLeftBorderColor(IndexedColors.RED.getIndex()); // 左边边框颜色

cellStyle.setBorderRight(CellStyle.BORDER_THIN); // 右边边框

cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex()); // 右边边框颜色

cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); // 上边边框 cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 上边边框颜色

//设置字体
Font font = wb.createFont();

font.setFontName("黑体");

font.setFontHeightInPoints((short) 16);//设置字体大小

Font font2 = wb.createFont();

font2.setFontName("仿宋_GB2312"); font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//粗体显示 font2.setFontHeightInPoints((short) 12);

cellStyle.setFont(font);//选择需要用到的字体格式

cell.setCellStyle(cellStyle); // 设置单元格样式  

 3.单元格值设置

3.1.设置单元格为文本格式

 CellStyle cellStyle=wb.createCellStyle(); // 创建单元格样式    
// 此处设置数据格式
DataFormat df = workbook.createDataFormat();
cellStyle.setDataFormat(df.getFormat("@"));//文本格式

cell.setCellStyle(cellStyle);

cell.setCellValue(data.toString());

3.2.设置单元格为日期格式

CellStyle cellStyle=wb.createCellStyle(); // 创建单元格样式    
// 此处设置数据格式
DataFormat df = workbook.createDataFormat();
cellStyle.setDataFormat(df.getFormat("yyyy-MM-dd"));//日期格式

cell.setCellStyle(cellStyle);

cell.setCellValue(data.toString());

3.3.设置单元格数值格式

CellStyle cellStyle=wb.createCellStyle(); // 创建单元格样式    
// 此处设置数据格式
DataFormat df = workbook.createDataFormat();
cellStyle.setDataFormat(df.getFormat("0"));//数据格式只显示整数"_ "
//cellStyle.setDataFormat(df.getFormat("0.00"));//保留两位小数点

cell.setCellStyle(cellStyle);

cell.setCellValue(data.toString());

3.4.设置单元格为货币格式

CellStyle cellStyle=wb.createCellStyle(); // 创建单元格样式    
// 此处设置数据格式
DataFormat df = workbook.createDataFormat();

cellStyle.setDataFormat(df.getFormat("¥#,##0"));//设置货币格式

cell.setCellStyle(cellStyle);

cell.setCellValue(data.toString());

3.5.设置单元格为百分比格式

CellStyle cellStyle=wb.createCellStyle(); // 创建单元格样式    
// 此处设置数据格式
DataFormat df = workbook.createDataFormat();

cellStyle.setDataFormat(df.getFormat("0.00%"));//%保留两位小数点

cell.setCellStyle(cellStyle);

// 设置单元格内容为double类型,数值需要进行转换计算

 cell.setCellValue(Double.parseDouble(data.toString())/100d);

3.6.设置单元格为中文大写格式

CellStyle cellStyle=wb.createCellStyle(); // 创建单元格样式    
// 此处设置数据格式

DataFormat format= workbook.createDataFormat(); cellStyle.setDataFormat(format.getFormat("[DbNum2][$-804]0"));//设置中文大写 cell.setCellStyle(cellStyle);

cell.setCellValue(data.toString());

3.7.设置单元格为科学计数法格式

CellStyle cellStyle=wb.createCellStyle(); // 创建单元格样式    
// 此处设置数据格式

DataFormat format= workbook.createDataFormat(); cellStyle.setDataFormat(format.getFormat("0.00E+00"));//设置科学计数法 cell.setCellStyle(cellStyle);

cell.setCellValue(data.toString());

猜你喜欢

转载自blog.csdn.net/u012998680/article/details/131480487