easypoi导出--深度解析为什么,数字类型列的单元格不能合并不能为空
问题一:数字类型列的单元格不能合并
easypoi导出时,给数字格式的列做纵向合并,前期发现:数字格式进行合并会报错,想要进行单元格合并的话单元格必须是字符串格式。
经过对源码的分析最终找到:PoiMergeCellUtil 这个类里的mergeCells 这个方法,
在这个方法中有一句代码:
String text = row.getCell(index).getStringCellValue();
这个不可以获取数值类型的单元格的数据,所以报错。继续观察发现:在“ getStringCellValue”这方法所在的接口中还有一个方法:
double getNumericCellValue();
我个人觉得可以在源码中加一个判断,从而使用“getNumericCellValue”这个方法,以达到数值单元格合并的效果。至于为什么easypoi没有这么做,因为没有更多文档以及个人理解问题的原因,我就不得而知了
mergeCells 方法的源码:
public static void mergeCells(Sheet sheet, Map<Integer, int[]> mergeMap, int startRow, int endRow) {
Map<Integer, MergeEntity> mergeDataMap = new HashMap();
if (mergeMap.size() != 0) {
Set<Integer> sets = mergeMap.keySet();
label55:
for(int i = startRow; i <= endRow; ++i) {
Row row = sheet.getRow(i);
Iterator var9 = sets.iterator();
while(true) {
while(true) {
if (!var9.hasNext()) {
continue label55;
}
Integer index = (Integer)var9.next();
if (row != null && row.getCell(index) != null) {
/**
* 这个getStringCellValue 方法不能获取数值类型的单元格的数据。所以会报错
*/
String text = row.getCell(index).getStringCellValue();
if (StringUtils.isNotEmpty(text)) {
hanlderMergeCells(index, i, text, mergeDataMap, sheet, row.getCell(index), (int[])mergeMap

文章详细探讨了在使用Easypoi导出Excel时遇到的两个问题:一是数字类型列的单元格无法合并,原因在于PoiMergeCellUtil的mergeCells方法中getStringCellValue()不适用于数值单元格;二是数字列的空单元格会被默认赋值为0。作者建议在源码中添加判断,使用getNumericCellValue()来支持数值单元格的合并,并指出了空值处理的逻辑导致的默认赋值问题。
最低0.47元/天 解锁文章
3556

被折叠的 条评论
为什么被折叠?



