使用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%
效果如图:



文章评论