https://github.com/deadzq/cp-utils-excelreader  <(感谢知名网友的帮助)

https://sargeraswang.com/blog/2018/11/27/excelutil-1-dot-2-1-doc/

https://github.com/SargerasWang/ExcelUtil

轮子好,但是永远比不上自己写,但是项目节奏紧张,只能先用别人写好的轮子.

这个问题主要发生在后台easyui界面的导入导出excel数据.

导出我没有使用大多数博客推荐的poi,而是使用了两个js插件,感觉这样还是比较方便的.(分页上的也同时能导出) ? <还得测试下

导入必须走数据库,也就是说需要将excel中的数据转为List<数据对象bean> 再传入insertObjList(Obj objList) <service层,再去调用dao层的单个insert数据里

首先创建了ExcelController用于接收路径请求;

package com.tansuo365.test1.controller;import com.alibaba.fastjson.JSONObject;
import com.sargeraswang.util.ExcelUtil.ExcelLogs;
import com.sargeraswang.util.ExcelUtil.ExcelUtil;
import com.tansuo365.test1.bean.PetroleumCoke;
import com.tansuo365.test1.service.PetroleumCokeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;@RestController
@RequestMapping("/excel")
public class ExcelController {@Autowiredprivate PetroleumCokeService petroleumCokeService;@RequestMapping("/importExcel")public Integer importExcel(@RequestParam(value = "uploadFile") MultipartFile uploadFile,Model model){JSONObject jsonObject = new JSONObject();InputStream in = null;System.err.println("in Excel Controller importExcel method;");System.out.println("excel:"+uploadFile);//获取前台excel输入流try {in = uploadFile.getInputStream();System.err.println(in);} catch (IOException e) {e.printStackTrace();}//excel的表头与文字对应,获取excel表头.if (uploadFile.getOriginalFilename().isEmpty() || uploadFile.getSize() == 0) {String message="上传失败";model.addAttribute("m",message);}ExcelLogs log = new ExcelLogs();Collection<PetroleumCoke> petroleumCokes = ExcelUtil.importExcel(PetroleumCoke.class, in, "yyyy/MM/dd HH:mm:ss", log, 0);List list = (List) petroleumCokes;Integer message = petroleumCokeService.insertBatchList(list);return message;}
}

在进行转换时因为前端页面使用了转化(将字段转为了对应的中文)

coke.html  line 474 :

 sheetsData.forEach(function (item, index) {content[item.position] = {v: item.value};if (item.value == 'id') {content[item.position] = {v: '序列号'};} else if (item.value == 'grade') {content[item.position] = {v: '品级'};} else if (item.value == 'province') {content[item.position] = {v: '省份'};} else if (item.value == 'company') {content[item.position] = {v: '企业'};} else if (item.value == 's_company') {content[item.position] = {v: '简称'};} else if (item.value == 'sulfur') {content[item.position] = {v: '硫含量%'};} else if (item.value == 'ash') {content[item.position] = {v: '灰分%'};} else if (item.value == 'volatile_matter') {content[item.position] = {v: '挥发分%'};} else if (item.value == 'wdr') {content[item.position] = {v: '扣水率%'};} else if (item.value == 'vanadium') {content[item.position] = {v: '钒含量ppm'};} else if (item.value == 'density') {content[item.position] = {v: '真密度g/cm³'};} else if (item.value == 'coke_content') {content[item.position] = {v: '粉焦量'};} else if (item.value == 'coke_type') {content[item.position] = {v: '类型'};} else if (item.value == 'today_price') {content[item.position] = {v: '今日报价(元)'};} else if (item.value == 'remarks') {content[item.position] = {v: '备注'};} else if (item.value == 'create_time') {content[item.position] = {v: '创建时间'};} else if (item.value == 'update_time') {content[item.position] = {v: '更新时间'};}});

excel:

所以在录入的时候其没法根据字段去录入所以报错:

### SQL: insert into petroleum_coke_tbl (province, company, s_company,     sulfur, ash, volatile_matter,     wdr, vanadium, coke_type,     today_price, remarks, grade,     expand_2, expand_3, create_time,     update_time, density, coke_content     )     values              (?, ?, ?,       ?, ?, ?,       ?, ?, ?,       ?, ?, ?,       ?, ?, ?,       ?, ?, ?       )      ,        (?, ?, ?,       ?, ?, ?,       ?, ?, ?,       ?, ?, ?,       ?, ?, ?,       ?, ?, ?       )
### Cause: java.sql.SQLIntegrityConstraintViolationException: Column 'province' cannot be null

在将上面js注释了转换后导出测试excel数据,再次进行导入测试:

错误依旧.这就奇怪了

那么也就是说传入的list中的province和其它字段是空的.

in Excel Controller importExcel method;
java.io.FileInputStream@7ebe9a94
petroleumCokes.toString()>>[PetroleumCoke(id=null, grade=null, province=null, company=null, s_company=null, sulfur=null, ash=null, volatile_matter=null, wdr=null, vanadium=null, density=null, coke_content=null, coke_type=null, today_price=null, remarks=null, expand_2=null, expand_3=null, create_time=null, update_time=null), PetroleumCoke(id=null, grade=null, province=null, company=null, s_company=null, sulfur=null, ash=null, volatile_matter=null, wdr=null, vanadium=null, density=null, coke_content=null, coke_type=null, today_price=null, remarks=null, expand_2=null, expand_3=null, create_time=null, update_time=null)]
list.get(0)>>PetroleumCoke(id=null, grade=null, province=null, company=null, s_company=null, sulfur=null, ash=null, volatile_matter=null, wdr=null, vanadium=null, density=null, coke_content=null, coke_type=null, today_price=null, remarks=null, expand_2=null, expand_3=null, create_time=null, update_time=null)
list.get(1)>>PetroleumCoke(id=null, grade=null, province=null, company=null, s_company=null, sulfur=null, ash=null, volatile_matter=null, wdr=null, vanadium=null, density=null, coke_content=null, coke_type=null, today_price=null, remarks=null, expand_2=null, expand_3=null, create_time=null, update_time=null)

如我所判断,确实这样,那么是ExcelUtil工具类转换出错么? (这里测试时,将inputStream读取了两次,第二次时就报错了,因为输入流只能读取一次,非要读取两次就应该拷贝一个输入流)

在bean上加上注解后还是出错. 读取不到除了String类型的其它参数:

petroleumCokes.toString()>>[PetroleumCoke(id=null, grade=null, province=山东, company=1, s_company=1, sulfur=null, ash=null, volatile_matter=null, wdr=null, vanadium=null, density=null, coke_content=null, coke_type=null, today_price=null, remarks=1, expand_2=null, expand_3=null, create_time=null, update_time=null), PetroleumCoke(id=null, grade=null, province=山东, company=1, s_company=1, sulfur=null, ash=null, volatile_matter=null, wdr=null, vanadium=null, density=null, coke_content=null, coke_type=null, today_price=null, remarks=null, expand_2=null, expand_3=null, create_time=null, update_time=null)]
list.get(0)>>PetroleumCoke(id=null, grade=null, province=山东, company=1, s_company=1, sulfur=null, ash=null, volatile_matter=null, wdr=null, vanadium=null, density=null, coke_content=null, coke_type=null, today_price=null, remarks=1, expand_2=null, expand_3=null, create_time=null, update_time=null)
list.get(1)>>PetroleumCoke(id=null, grade=null, province=山东, company=1, s_company=1, sulfur=null, ash=null, volatile_matter=null, wdr=null, vanadium=null, density=null, coke_content=null, coke_type=null, today_price=null, remarks=null, expand_2=null, expand_3=null, create_time=null, update_time=null)

这里看一眼importExcel方法

 public static <T> Collection<T> importExcel(Class<T> clazz, InputStream inputStream, String pattern, ExcelLogs logs, Integer... arrayCount) {Workbook workBook;try {workBook = WorkbookFactory.create(inputStream);} catch (Exception var28) {LG.error("load excel file error", var28);return null;}List<T> list = new ArrayList();Sheet sheet = workBook.getSheetAt(0);Iterator rowIterator = sheet.rowIterator();try {List<ExcelLog> logList = new ArrayList();HashMap titleMap = new HashMap();while(true) {Row row;label136:do {while(rowIterator.hasNext()) {row = (Row)rowIterator.next();if (row.getRowNum() == 0) {continue label136;}boolean allRowIsNull = true;Iterator cellIterator = row.cellIterator();while(cellIterator.hasNext()) {Object cellValue = getCellValue((Cell)cellIterator.next());if (cellValue != null) {allRowIsNull = false;break;}}if (allRowIsNull) {LG.warn("Excel row " + row.getRowNum() + " all row value is null!");} else {StringBuilder log = new StringBuilder();if (clazz == Map.class) {Map<String, Object> map = new HashMap();Iterator var36 = titleMap.keySet().iterator();while(var36.hasNext()) {String k = (String)var36.next();Integer index = (Integer)titleMap.get(k);Cell cell = row.getCell(index);if (cell == null) {map.put(k, (Object)null);} else {cell.setCellType(CellType.STRING);String value = cell.getStringCellValue();map.put(k, value);}}list.add(map);} else {T t = clazz.newInstance();int arrayIndex = 0;int cellIndex = 0;List<FieldForSortting> fields = sortFieldByAnno(clazz);Iterator var19 = fields.iterator();while(true) {while(var19.hasNext()) {FieldForSortting ffs = (FieldForSortting)var19.next();Field field = ffs.getField();field.setAccessible(true);if (field.getType().isArray()) {Integer count = arrayCount[arrayIndex];Object value;if (field.getType().equals(String[].class)) {value = new String[count];} else {value = new Double[count];}for(int i = 0; i < count; ++i) {Cell cell = row.getCell(cellIndex);String errMsg = validateCell(cell, field, cellIndex);if (StringUtils.isBlank(errMsg)) {((Object[])value)[i] = getCellValue(cell);} else {log.append(errMsg);log.append(";");logs.setHasError(true);}++cellIndex;}field.set(t, value);++arrayIndex;} else {Cell cell = row.getCell(cellIndex);String errMsg = validateCell(cell, field, cellIndex);if (StringUtils.isBlank(errMsg)) {Object value = null;if (field.getType().equals(Date.class) && cell.getCellTypeEnum() == CellType.STRING) {Object strDate = getCellValue(cell);try {value = (new SimpleDateFormat(pattern)).parse(strDate.toString());} catch (ParseException var27) {errMsg = MessageFormat.format("the cell [{0}] can not be converted to a date ", CellReference.convertNumToColString(cell.getColumnIndex()));}} else {value = getCellValue(cell);ExcelCell annoCell = (ExcelCell)field.getAnnotation(ExcelCell.class);if (value instanceof String && !field.getType().equals(String.class) && StringUtils.isNotBlank(annoCell.defaultValue())) {value = annoCell.defaultValue();}}field.set(t, value);}if (StringUtils.isNotBlank(errMsg)) {log.append(errMsg);log.append(";");logs.setHasError(true);}++cellIndex;}}list.add(t);logList.add(new ExcelLog(t, log.toString(), row.getRowNum() + 1));break;}}}}logs.setLogList(logList);return list;} while(clazz != Map.class);Iterator<Cell> cellIterator = row.cellIterator();for(Integer index = 0; cellIterator.hasNext(); index = index + 1) {String value = ((Cell)cellIterator.next()).getStringCellValue();titleMap.put(value, index);}}} catch (InstantiationException var29) {throw new RuntimeException(MessageFormat.format("can not instance class:{0}", clazz.getSimpleName()), var29);} catch (IllegalAccessException var30) {throw new RuntimeException(MessageFormat.format("can not instance class:{0}", clazz.getSimpleName()), var30);}}

sheet对应着excel表的那个sheel,表示一个工作簿

row表示每行

下面代码判断了是否每个工作簿不是空的,空的就allRowIsNull = true, 如果元素 cellIterator.next(),判断获取的cellValue不是空的话,就将allRowIsNull = false (即有数据了,不要给他true的flag了)

 while(true) {Row row;label136:do {while(rowIterator.hasNext()) {row = (Row)rowIterator.next();if (row.getRowNum() == 0) {continue label136;}boolean allRowIsNull = true;Iterator cellIterator = row.cellIterator();while(cellIterator.hasNext()) {Object cellValue = getCellValue((Cell)cellIterator.next());if (cellValue != null) {allRowIsNull = false;break;}}if (allRowIsNull) {LG.warn("Excel row " + row.getRowNum() + " all row value is null!");} else {StringBuilder log = new StringBuilder();if (clazz == Map.class) {Map<String, Object> map = new HashMap();Iterator var36 = titleMap.keySet().iterator();while(var36.hasNext()) {String k = (String)var36.next();Integer index = (Integer)titleMap.get(k);Cell cell = row.getCell(index);if (cell == null) {map.put(k, (Object)null);} else {cell.setCellType(CellType.STRING);String value = cell.getStringCellValue();map.put(k, value);}}list.add(map);} else {T t = clazz.newInstance();int arrayIndex = 0;int cellIndex = 0;List<FieldForSortting> fields = sortFieldByAnno(clazz);Iterator var19 = fields.iterator();

for(Integer index = 0; cellIterator.hasNext(); index = index + 1) {String value = ((Cell)cellIterator.next()).getStringCellValue();titleMap.put(value, index);}

可以看到上面这些好像都必须cell为string类型.

第二个错误是转型异常

can't ...numberic from string value.原因是设置的时间格式.

Collection<PetroleumCoke> petroleumCokes = ExcelUtil.importExcel(PetroleumCoke.class, in, "yyyy-MM-dd HH:mm:ss", log, 0);

注意这里的yyyy-MM-dd HH:mm:ss

当设置不全时,比如省略了excel中的HH:mm:ss,就会以00:00:00填充到数据中(录入后),所以要把excel表中的时间格式与这里的yyyy-MM-dd...对应起来,后期可以设置为后台设定.

remarks=null, expand_2=null, expand_3=null, create_time=Sun Jan 20 00:00:00 CST 2019, update_time=null)

为了快速开发,暂时把bean中的float改为double, Integer Long 改为了String

PetroleumCoke(id=31, grade=null, province=山东, company=1, s_company=31简称, sulfur=1.2, ash=4.0, volatile_matter=1.2, wdr=1.2, vanadium=123.0, density=1.2, coke_content=12.0, coke_type=海绵焦, today_price=213.0, remarks=备注31, expand_2=null, expand_3=null, create_time=null, update_time=null)
PetroleumCoke(id=32, grade=null, province=山东, company=1, s_company=32简称, sulfur=2.2, ash=12.0, volatile_matter=2.3, wdr=1.2, vanadium=2.0, density=1.3, coke_content=12.0, coke_type=弹丸焦, today_price=3124.0, remarks=备注32, expand_2=null, expand_3=null, create_time=Sun Jan 20 00:00:00 CST 2019, update_time=null)
PetroleumCoke(id=33, grade=null, province=山东, company=的, s_company=的, sulfur=1.2, ash=1.5, volatile_matter=3.0, wdr=1.2, vanadium=1.0, density=1.4, coke_content=11.0, coke_type=海绵焦, today_price=142.0, remarks=备注33, expand_2=null, expand_3=null, create_time=Sun Jan 20 18:16:04 CST 2019, update_time=null)

导入导出ok,不足之处是导出的excel再导入的话会有错误-->非string字段需要双击改善下(即将靠左的numberic改为靠右才判定为numberic,否则string)

导出通过的是html的js,没有调用数据库,而导出的字段可以在js中进行调整: 在上面提到的coke.html  line474

转载于:https://www.cnblogs.com/ukzq/p/10293021.html

[exceltolist] - 一个excel转list的工具相关推荐

  1. excel2json 一个excel转json的工具(开源)

    excel2json 一个excel转json的工具 开源地址:https://github.com/zdhsoft/excel2json 这个工具是基于python 2.7.x(已经增加了3.x的版 ...

  2. spreadjs~~一个Excel在线编辑的工具

    spreadjs的用法 var spreadNS = GC.Spread.Sheets;var spread = new spreadNS.Workbook($("#spreadjsExc& ...

  3. java 操作excel的类_探究下Java操作Excel的几类工具

    引言 java解析.生成Excel比较有名的框架有Apache poi.jxl.但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI ...

  4. 《数据分析实战:基于EXCEL和SPSS系列工具的实践》——3.3 耗时耗力的数据整理过程...

    本节书摘来自华章计算机<数据分析实战:基于EXCEL和SPSS系列工具的实践>一书中的第3章,第3.3节,作者 纪贺元,更多章节内容可以访问云栖社区"华章计算机"公众号 ...

  5. 《数据分析实战 基于EXCEL和SPSS系列工具的实践》一3.4 数据量太大了怎么办

    本节书摘来自华章出版社<数据分析实战 基于EXCEL和SPSS系列工具的实践>一书中的第3章,第3.4节,纪贺元 著,更多章节内容可以访问云栖社区"华章计算机"公众号查 ...

  6. 《数据分析实战 基于EXCEL和SPSS系列工具的实践》一第2章 数据分析的理论、工具、模型...

    本节书摘来自华章出版社<数据分析实战 基于EXCEL和SPSS系列工具的实践>一书中的第2章,第2.1节,纪贺元 著,更多章节内容可以访问云栖社区"华章计算机"公众号查 ...

  7. GridView导出到Excel和开源图表工具

    导出GridView到Excel 把GridView导出到Excel是一个很常用的功能,在网上搜索的解决方案都是一些零碎的代码,并且有很多问题,不是在所有环境下可以通用的,你甚至还有去处理一些这些代码 ...

  8. C#开发的高性能EXCEL导入、导出工具DataPie(支持MSSQL、ORACLE、ACCESS,附源码下载地址)...

    作 为财务数据核算人员,面对大量的业务与财务数据,借助于传统的EXCEL表格,已经力不从心.最近几个月,利用周末及下班的空闲时间,写了一个数据库导入 导出工具,以方便业务逻辑密集型的数据处理.目前,D ...

  9. Excel转换成Json工具

    Excel转换成Json工具: 可执行版本下载:https://github.com/neil3d/excel2json/releases 完整项目源代码下载:https://github.com/n ...

最新文章

  1. linux yum lamp环境,linux centos yum安装LAMP环境
  2. SpringBoot报错 org.apache.catalina.LifecycleException: Protocol handler start failed
  3. 今日恐慌与贪婪指数为78 贪婪程度与昨日持平
  4. JavaSE12:集合简单总结
  5. (IStool)判断系统位数并打包不同的文件
  6. 关于scrollTop
  7. 下载哨兵1精轨数据教程
  8. 注释 护眼色 绿色 RGB
  9. Easy Data Transform for mac (Excel和CSV编程文件转换工具) v1.11.1激活版
  10. R语言实现拟合神经网络; 神经网络包
  11. 腾讯游戏运营总监酒后吹批:运维工程师这些知识点都不会?赶紧找个地埋了吧!
  12. arduino 源码分层浅析
  13. 人口统计、红利、康波
  14. 12.嵌入式控制器EC实战 SMBus概述
  15. 你了解区块链资产吗?新手要如何投资区块链资产?
  16. og属性优化教程,什么是og标签
  17. linux打地鼠课程设计,数字电路课程设计打地鼠.doc
  18. 2020-08-25
  19. 爬取网站时返回的html是乱码问题解决
  20. 计算机考研408每日一题 day76

热门文章

  1. bootmgr快速修复win7_win7电脑系统常见的启动故障
  2. html文本框无填充颜色,将文本框设定为:无填充颜色和无线条颜色
  3. 《中国智慧环保产业发展监测与投资前景研究报告(2022版)》
  4. 关于性 父亲是这样对18岁女儿说的
  5. Linux Curl命令教程
  6. java计算机毕业设计师生教学评价系统源代码+数据库+系统+lw文档
  7. openjudge1.11编程基础之二分查找 04:网线主管
  8. BigData数据可视化软件及工具
  9. 六、改进版(Hit UFO)
  10. MySQL数据库性能优化及自动化运维实践教程!DBA日常工作