JAVA POI设置EXCEL单元格格式用法举例

 更新时间:2023年08月24日 08:54:31   作者:风萧萧1999  
这篇文章主要给大家介绍了关于JAVA POI设置EXCEL单元格格式用法的相关资料,POI中可能会用到一些需要设置EXCEL单元格格式的操作,需要的朋友可以参考下

前言

本文将介绍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());

总结

到此这篇关于JAVA POI设置EXCEL单元格格式的文章就介绍到这了,更多相关POI设置EXCEL单元格格式内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • SpringBoot整合token实现登录认证的示例代码

    SpringBoot整合token实现登录认证的示例代码

    本文主要介绍了SpringBoot整合token实现登录认证的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • java中子类继承父类,程序运行顺序的深入分析

    java中子类继承父类,程序运行顺序的深入分析

    本篇文章是对java中子类继承父类,程序运行顺序进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • Java线程之间的共享与协作详解

    Java线程之间的共享与协作详解

    这篇文章主要介绍了Java线程之间的共享与协作详解,进程是操作系统进行资源分配的最小单位,线程是进程的一个实体,是CPU调度和分派的基本单位,它是比经常更小的、能够独立运行的基本单位
    2022-07-07
  • IntelliJ IDEA 2021.1 首个 Beta 版本发布

    IntelliJ IDEA 2021.1 首个 Beta 版本发布

    这篇文章主要介绍了IntelliJ IDEA 2021.1 首个 Beta 版本发布,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • Java实现的图片上传工具类完整实例

    Java实现的图片上传工具类完整实例

    这篇文章主要介绍了Java实现的图片上传工具类,涉及java针对图片文件的检查、上传、清除等相关操作技巧,需要的朋友可以参考下
    2017-10-10
  • SpringBoot特点之依赖管理和自动装配(实例代码)

    SpringBoot特点之依赖管理和自动装配(实例代码)

    在使用SpringBoot的时候,会自动将Bean装配到IoC容器中,操作也很简单,今天小编给大家介绍下SpringBoot特点之依赖管理和自动装配的知识,感兴趣的朋友一起看看吧
    2022-03-03
  • Spring Boot 使用 Swagger 构建 RestAPI 接口文档

    Spring Boot 使用 Swagger 构建 RestAPI 接口文档

    这篇文章主要介绍了Spring Boot 使用 Swagger 构建 RestAPI 接口文档,帮助大家更好的理解和使用Spring Boot框架,感兴趣的朋友可以了解下
    2020-10-10
  • Spring Cloud 中@FeignClient注解中的contextId属性详解

    Spring Cloud 中@FeignClient注解中的contextId属性详解

    这篇文章主要介绍了Spring Cloud 中@FeignClient注解中的contextId属性详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • m1 Mac设置多jdk版本并动态切换的实现

    m1 Mac设置多jdk版本并动态切换的实现

    本文主要介绍 Mac 下如何安装 JDK 并且多版本如何切换,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • java多线程volatile内存语义解析

    java多线程volatile内存语义解析

    这篇文章主要介绍了java多线程volatile内存语义解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01

最新评论