package com.esse.projects.ct.until;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.esse.common.core.util.StringUtils;
import java.math.BigDecimal;
/**
* @description:自定义转换器
* @author: yuhx
* @date: 2023/3/30 9:08
*/
public class CustomConvert implements Converter<String> {
/**
* 支持的Java类型
*
* @return
*/
@Override
public Class supportJavaTypeKey() {
return String.class;
}
/**
* 支持的excel类型
*
* @return
*/
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
/**
* 转换Java支持的类型
*
* @param cellData
* @param excelContentProperty
* @param globalConfiguration
* @return
* @throws Exception
*/
@Override
public String convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
return cellData.getStringValue();
}
/**
* 转换为Excel支持的类型
*
* @param s
* @param excelContentProperty
* @param globalConfiguration
* @return
* @throws Exception
*/
@Override
public CellData convertToExcelData(String s, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
if (isBigDecimal(s)) {
BigDecimal bigDecimal = new BigDecimal(s);
return new CellData(bigDecimal);
} else {
return new CellData(s);
}
}
public static boolean isBigDecimal(String str) {
if (str == null || str.trim().length() == 0) {
return false;
}
char[] chars = str.toCharArray();
int sz = chars.length;
int i = (chars[0] == '-') ? 1 : 0;
if (i == sz) return false;
//除了负号,第一位不能为'小数点'
if (chars[i] == '.') return false;
boolean radixPoint = false;
for (; i < sz; i++) {
if (chars[i] == '.') {
if (radixPoint) return false;
radixPoint = true;
} else if (!(chars[i] >= '0' && chars[i] <= '9')) {
return false;
}
}
return true;
}
}
导出excel此单元格中的数字为文本格式,或者其前面有撇号
5星 · 超过95%的资源 需积分: 12 134 浏览量
2023-03-30
16:53:49
上传
评论
收藏 1002B ZIP 举报
YHXSunny123456789
- 粉丝: 1586
- 资源: 3
最新资源
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- Highlight Plus v20.0.1
- 基于MIC+NE555光敏电阻的声光控电路Multisim仿真原理图
- python tkinter-08-盒子模型.ev4.rar
- Doozy UI Manager 2023
- 基于matlab实现夜间车牌识别程序(1).rar
- 基于matlab实现无线传感器网络无需测距定位算法matlab源代码 包括apit,dv-hop,amorphous在内的共7个
- 基于python的yolov5实现的旋转目标检测
- 基于matlab实现无线传感器网络 CAB定位仿真程序 这是无线传感器节点定位CAB算法的仿真程序,由matlab完成.rar
- 基于matlab实现图像处理,本程序使用背景差分法对来往车辆进行检测和跟踪.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页