JAVA:POI设置EXCEL单元格格式

news/2024/7/21 3:51:56 标签: 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());


http://www.niftyadmin.cn/n/1005934.html

相关文章

FlagPerf中标准case的定义和添加适配原则

FlagPerf发展至今&#xff0c;在前进过程中沉淀下多个“标准”。所谓“不以规矩&#xff0c;不成方圆”&#xff0c;我们期望在各项标准的规范下&#xff0c;FlagPerf成为一个可靠的芯片评测平台。 1. 标准Case定义 如今&#xff0c;业内芯片厂商百花齐放&#xff0c;主流硬件厂…

UE4/5数字人Metahuman与Style3D的使用【二、布料模拟】

目录 鼠标点击布料模拟&#xff1a; 让布料模拟可以跟着动画序列&#xff1a; 有穿模情况&#xff1a; 多件衣服替换&#xff1a; 关卡序列中使用缓存&#xff1a; 效果&#xff1a; UE4/5数字人Metahuman与Style3D的使用【一、Style3DAtelier软件制作smd格式衣服并导入ue】…

Spring Boot中Redis自动配置的介绍、原理和使用

Spring Boot中Redis自动配置的介绍、原理和使用 引言 Redis是一种高性能的键值对存储数据库&#xff0c;它支持多种数据结构和丰富的功能&#xff0c;如缓存、消息队列、计数器、分布式锁等。在Spring Boot中&#xff0c;我们可以通过自动配置的方式来使用Redis。本文将介绍S…

oracle创建pdb

1、使用omf自动管理的情况 create pluggable database pdb1 admin user pdb1admin identified by pdb1admin; 这里由于使用的是OMF,可以不指定新建的PDB数据文件路径,则会将数据文件存放到DB_CREATE_FILE_DEST下的GUID目录下(GUID可以在v$pdbs中查询到). 也可以指定磁盘组 cre…

Python 基本数据类型(七)

文章目录 每日一句正能量Set&#xff08;集合&#xff09;Dictionary&#xff08;字典&#xff09;结语 每日一句正能量 生活里的累&#xff0c;一半源于生活本身&#xff0c;一半源于我们对待生活的态度。当我们感觉疲惫的时候&#xff0c;记得提醒自己保持一种幸福感&#xf…

hive表小文件合并

1. 背景 公司的 hive 表中的数据是通过 flink sql 程序&#xff0c;从 kafka 读取&#xff0c;然后写入 hive 的&#xff0c;为了数据能够被及时可读&#xff0c;我设置了 flink sql 程序的 checkpoint 时间为 1 分钟&#xff0c;因此&#xff0c;在 hive 表对应的 hdfs 上&am…

Laravel 多字段去重count计数

Laravel 多字段去重count计数 背景&#xff1a;需要统计数据列表总条数&#xff08;字段1、字段2去重统计&#xff09; table&#xff1a;policy_view,去重字段admin_id和permission 期望结果&#xff1a;count不含重复统计数据 解决思路&#xff1a; 语法&#xff1a;DISTI…

ubuntu22.04配置双网卡双静态ip不通网段访问服务器的相同服务

ubuntu22.04配置双网卡双静态ip不通网段访问服务器的相同服务 技术博客 http://idea.coderyj.com/ 1.需求 南方电网网段(假如)是 192.168.3.1的网段机器人服务器在隧道ip是 172.16.1.1网段要求这2个网段都能访问到服务器上的服务 2.解决方案 服务器上配置双网卡 双ip 以ubun…