EasyExcel数值格式保留小数
时间: 2023-11-22 19:43:18 浏览: 264
您可以使用EasyExcel库中的`style`方法设置单元格的格式,保留小数的方法如下:
```java
// 导入所需的类
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.metadata.Font;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
// 创建样式
WriteCellStyle cellStyle = new WriteCellStyle();
cellStyle.setDataFormat(0x2); // 设置格式为保留两位小数
// 创建字体
WriteFont font = new WriteFont();
font.setFontName("宋体"); // 设置字体为宋体
cellStyle.setWriteFont(font);
// 设置样式策略
HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(cellStyle, cellStyle);
String fileName = "your-file-path.xlsx";
// 写入数据并设置样式策略
EasyExcel.write(fileName)
.registerWriteHandler(styleStrategy)
.sheet("Sheet1")
.doWrite(yourDataList);```
请将`your-file-path.xlsx`替换为您要保存的文件路径,`yourDataList`为您要写入的数据列表。
相关推荐
![mht](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)