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());

  • 8
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用POI的CellValueFormatter类来获取Excel单元格的数值格式。具体的操作如下: 1. 首先,使用POI读取Excel文件并获取一个单元格对象; 2. 然后,使用这个单元格对象的getCellType方法获取该单元格的数据类型; 3. 倘若该单元格的数据类型为数值型的话,可以使用POI的DateUtil类来判断该单元格的数值格式,并进行相应的处理。 例如,以下是一个使用POI获取Excel单元格数值格式的示例代码: ```java // 导入POI相关的库 import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.ss.usermodel.CellValue; import org.apache.poi.ss.usermodel.CellValue.CellTypeEnum; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DataFormatter; public class Example { public static void main(String[] args) throws Exception { // 读取Excel文件 Workbook workbook = WorkbookFactory.create(new File("example.xlsx")); Sheet sheet = workbook.getSheetAt(0); // 获取某个单元格 Cell cell = sheet.getRow(0).getCell(0); // 判断单元格的数据类型 if (cell.getCellType() == CellType.NUMERIC) { // 判断单元格的数值格式 if (DateUtil.isCellDateFormatted(cell)) { // 处理日期型数据 Date date = cell.getDateCellValue(); String formattedDate = new SimpleDateFormat("yyyy/MM/dd").format(date); System.out.println(formattedDate); } else { // 处理普通数值型数据 Double value = cell.getNumericCellValue(); System.out.println(value); } } else { // 处理其他类型的数据 DataFormatter formatter = new DataFormatter(); String text = formatter.formatCellValue(cell); System.out.println(text); } } } ``` 以上代码使用POI读取了一个Excel文件中的单元格,并根据该单元格的数据类型和数值格式进行相应的处理。具体可以根据实际需要进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值