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 { @Autowired
private 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

[exceltolist] - 一个excel转list的工具的更多相关文章

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

    转自:http://www.cnblogs.com/yfl8910/archive/2012/05/19/2509194.html 作为财务数据核算人员,面对大量的业务与财务数据,借助于传统的EXCE ...

  2. JEasyPoi 2.1.4 (Jeecg订制) 版本发布,Excel 和 Word 简易工具类

    JEasyPoi 2.1.4 (jeecg订制)版本发布,EasyPoi Excel 和 Word 简易工具类 easypoi 功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 ...

  3. Excel和Word 简易工具类,JEasyPoi 2.1.5 版本发布

    Excel和Word 简易工具类,JEasyPoi 2.1.5 版本发布 摘要: jeasypoi 功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 就可以方便的写出Excel导 ...

  4. JXLS (Excel导入、导出工具使用)

    JXLS (Excel导入.导出工具使用) 1:简介: jxls是一个简单的.轻量级的excel导出库,使用特定的标记在excel模板文件中来定义输出格式和布局.java中成熟的excel导出工具有p ...

  5. 《数据分析实战:基于EXCEL和SPSS系列工具的实践》一1.4 数据分析的流程

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

  6. 读取EXCEL文档解析工具类

    package test;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException ...

  7. 多Excel文件内容查询工具。

    多Excel文件内容查询工具. 告别繁琐重复的体力劳动,一分钟干完一天的活. 码云 github 下载 当需要在多个Excel表格中查询需要的信息是,一个文件一个文件的去查询非常麻烦. 虽然有其他方法 ...

  8. ExcelPatternTool: Excel表格-数据库互导工具

    ExcelPatternTool Excel表格-数据库互导工具 介绍: 指定Pattern文件-一个规则描述的json文档,基于此规则实现Excel表格与数据库之间的导入导出,校验等功能. 特点: ...

  9. 用php生成一个excel文件(原理)

    1.我们用php来生成一个excel文档来讲述其原理: excel2007里面的文档目录组成部分为: 2.我们使用ZipArchive()方法来生成一个简易的excel文件. 使用方法: 3.代码如下 ...

随机推荐

  1. Shell变量相关

    li@ubuntu:~/test$ vi test.sh li@ubuntu:~/test$ cat test.sh #!/bin/bash #shell变量不加引号;加单引号;加双引号都行 url= ...

  2. GUI常用对象介绍3

    %text hf = axes; ht = text(,,'示例'); get(ht); %公式 并且设置位置坐标 (积分符号) text('String','\int_0^x dF(x)','Pos ...

  3. 跨域的根本原因:JavaScript 的同源策略

    摘自:https://blog.csdn.net/liubo2012/article/details/43148705 同源策略限制了一个源(origin)中加载文本或脚本与来自其它源(origin) ...

  4. SpringMVC配置字符编码过滤器CharacterEncodingFilter来解决表单乱码问题

    1.GET请求 针对GET请求,可以配置服务器Tomcat的conf\server.xml文件,在其第一个<Connector>标签中,添加URIEncoding="UTF-8& ...

  5. Hbuilder安装

    ---恢复内容开始--- ---恢复内容结束---

  6. ELK学习笔记之F5 DNS可视化让DNS运维更安全更高效-F5 ELK可视化方案系列(3)

    0x00 概述 此文力求比较详细的解释DNS可视化所能带来的场景意义,无论是运维.还是DNS安全.建议仔细看完下图之后的大篇文字段落,希望能引发您的一些思考. 在“F5利用Elastic stack( ...

  7. Linux学习笔记之Linux运行脚本时 $'\r' 错误

    1.Windows上操作 用notepad++编译器打开脚本,编辑->文档格式转换->转换为UNIX格式,然后保存. 重新上传.运行,问题解决 2.Linux上操作 用vi/vim命令打开 ...

  8. JavaScript修改元素

    案例1 删除元素 如需删除 HTML 元素,需要清楚该元素的父元素 该js函数代码如下 function remove(){ var parent=document.getElementById(&q ...

  9. UnicodeEncodeError: 'utf-8' codec can't encode character '\udce4' in position 1: surrogates not allowed根本解决方法

    下午开发新改的代码在print路径时出现上述问题,如下: Traceback (most recent call last): File "./update.py", line 6 ...

  10. 关于__declspec(dllexport)

    windows下dll动态库函数的导入与导出. __declspec Microsoft Specific __declspec ( extended-attribute ) declarator l ...