ApachePOI写入Excel单元格格式是百分比的方式

2024-02-18 163点热度 0人点赞 0条评论

使用Apache   POI 写入Excel文件,不希望是字符串类型,而希望是数值型,这样不会有一个提示,例如:

解决办法的代码如下:

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(2);//创建单元格
 
CellStyle percentCellStyle = workbook.createCellStyle();//创建单元格格式
percentCellStyle.setDataFormat(workbook.createDataFormat().getFormat("0.0%"));
 
cell.setCellStyle(percentCellStyle);//给单元格设置格式
cell.setCellValue(-0.8);//给单元格设置值

给单元格设置了百分比专用的格式0.0%

效果如图:

 

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论