easyexcel导出时保留空格
时间: 2023-08-19 14:04:22 浏览: 122
在 EasyExcel 中,导出时保留空格可以通过设置文本格式来实现。你可以使用 `StyleStrategy` 类的 `setUseSpaceAsDefaultPlaceholder` 方法来设置默认的占位符为空格。这样,在导出时,如果单元格中包含空格,它们将被保留。
以下是一个示例代码:
```java
// 创建导出数据的列表
List<List<String>> data = new ArrayList<>();
data.add(Arrays.asList("Hello", "World", "EasyExcel"));
data.add(Arrays.asList("This", "is", "a test"));
// 创建 ExcelWriter 对象
ExcelWriter excelWriter = EasyExcel.write("output.xlsx").build();
// 创建 Sheet 对象
WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
// 设置文本格式,保留空格
StyleStrategy styleStrategy = new StyleStrategy();
styleStrategy.setUseSpaceAsDefaultPlaceholder(true);
// 写入数据到 Excel 文件
excelWriter.write(data, writeSheet, styleStrategy);
// 关闭 ExcelWriter
excelWriter.finish();
```
在上述示例中,我们通过 `StyleStrategy` 类的 `setUseSpaceAsDefaultPlaceholder` 方法将默认的占位符设置为使用空格。然后,我们使用 `excelWriter.write` 方法将数据写入 Excel 文件。
这样,在导出时,单元格中的空格将被保留。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)