jxls导入Excel的问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jxls导入Excel的问题相关的知识,希望对你有一定的参考价值。
导入的excel有一单元格值为空,该值对应的类属性类型为Date,解析excel的时候总报错,因为需要把一个null值转化成Date,这种问题怎么解决
参考技术A 不知道时间是来及,但还是写个可能性解决方案吧,在<mapping>标签中加入属性nullAllowed="true"就可以了 参考技术B Workbook workbook = Workbook.getWorkbook(new File(filePath));Sheet sheet = workbook.getSheet(0);
int col = sheet.getColumns();
int row = sheet.getRows();
String result[][] = new String[row][col];
Cell cell;
// i列 j行
System.out.println("col:"+col+" row: "+row);
for (int i = 0; i < row; i++)
if(i!=0)
SubjectEntity sub=new SubjectEntity();
for (int j = 0; j < col; j++)
cell = sheet.getCell(j, i);
result[i][j] = cell.getContents();
if(j==0)
sub.setQuestion(result[i][j]);
jxls实现导入Excel数据
SSM项目导入Excel,话不多说,直接干货
1:添加maven相关依赖
<dependency> <groupId>org.jxls</groupId> <artifactId>jxls-reader</artifactId> <version>2.0.2</version> </dependency> <dependency> <groupId>org.jxls</groupId> <artifactId>jxls</artifactId> <version>2.4.6</version> </dependency>
2:ExcelHead类:存放Excel表的表头信息,导入时要完全匹配表头信息才可导入
1 public class ExcelHead { 2 //读取表头 3 private String head; 4 //节点 这里要导入的Excel的表头必须和这儿完全匹配 5 private String point = "节点编号*,节点名称*,节点描述,节点类别,仓库编号*,仓库名称*,是否自动接收*,备注"; 6 public String getHead() { 7 return head; 8 } 9 10 public void setHead(String head) { 11 if (StringUtil.isEmpty(this.head)) { 12 this.head = head.trim(); 13 } else { 14 this.head = String.format("%s,%s", this.head, head.trim()); 15 } 16 } 17 18 public String getPoint() { 19 return point; 20 } 21 22 public void setPoint(String point) { 23 this.point = point; 24 } 25 26 }
point-body.xml文件.这里存放的是表体信息
1 <?xml version="1.0" encoding="UTF-8"?> 2 <workbook> 3 <worksheet idx="0"> 4 <!-- 表头开始至结束行 --> 5 <section startRow="0" endRow="0"/> 6 <!-- 开始循环读取文件数据,配置开始行,items映射的list var映射的bean varType 类路径 7 startRow:开始循环的行数 8 endRow-startRow:循环体的大小,0代表一行,依次论推 9 每循环一次,判断是否结束,不结束继续循环,直至结束 10 --> 11 <loop startRow="1" endRow="1" items="pointList" var="sku" 12 varType="com.eglsc.ams.domain.vo.PointVo"> 13 <!-- 循环开始行 --> 14 <section startRow="1" endRow="1"> 15 <!-- 节点编号 --> 16 <mapping row="1" col="0" nullAllowed="true"> 17 sku.pointNo 18 </mapping> 19 <!-- 节点名称 --> 20 <mapping row="1" col="1" nullAllowed="true"> 21 sku.pointName 22 </mapping> 23 <!-- 节点描述 --> 24 <mapping row="1" col="2" nullAllowed="true"> 25 sku.pointDescription 26 </mapping> 27 <!-- 节点类别 --> 28 <mapping row="1" col="3" nullAllowed="true"> 29 sku.pointCategory 30 </mapping> 31 <!-- 仓库编号 --> 32 <mapping row="1" col="4" nullAllowed="true"> 33 sku.siteNo 34 </mapping> 35 <!-- 仓库名称 --> 36 <mapping row="1" col="5" nullAllowed="true"> 37 sku.siteName 38 </mapping> 39 <!-- 是否自动接收 --> 40 <mapping row="1" col="6" nullAllowed="true"> 41 sku.automaticFlag 42 </mapping> 43 <!-- 备注 --> 44 <mapping row="1" col="7" nullAllowed="true"> 45 sku.remark 46 </mapping> 47 </section> 48 <!-- 结束条件配置 --> 49 <loopbreakcondition> 50 <rowcheck offset="0"> 51 <!-- 空白结束不填 --> 52 <cellcheck offset="0"></cellcheck> 53 </rowcheck> 54 </loopbreakcondition> 55 </loop> 56 </worksheet> 57 </workbook>
point-head.xml文件.这里配置的是表头信息
1 <?xml version="1.0" encoding="UTF-8"?> 2 <workbook> 3 <worksheet idx="0"> 4 <!-- 表头开始至结束行 --> 5 <section startRow="0" endRow="0"> 6 <!-- 节点编号 --> 7 <mapping row="0" col="0" nullAllowed="true"> 8 excelHead.head 9 </mapping> 10 <!-- 节点名称 --> 11 <mapping row="0" col="1" nullAllowed="true"> 12 excelHead.head 13 </mapping> 14 <!-- 节点描述 --> 15 <mapping row="0" col="2" nullAllowed="true"> 16 excelHead.head 17 </mapping> 18 <!-- 节点类别 --> 19 <mapping row="0" col="3" nullAllowed="true"> 20 excelHead.head 21 </mapping> 22 <!-- 仓库编号 --> 23 <mapping row="0" col="4" nullAllowed="true"> 24 excelHead.head 25 </mapping> 26 <!-- 仓库名称 --> 27 <mapping row="0" col="5" nullAllowed="true"> 28 excelHead.head 29 </mapping> 30 <!-- 是否自动接收 --> 31 <mapping row="0" col="6" nullAllowed="true"> 32 excelHead.head 33 </mapping> 34 <!--备注--> 35 <mapping row="0" col="7" nullAllowed="true"> 36 excelHead.head 37 </mapping> 38 </section> 39 </worksheet> 40 </workbook>
导入代码:
2 @RequestMapping(value = "/import", method = RequestMethod.POST)
@Response 3 @Transactional 4 public Response importExcel(@RequestParam(required = true) MultipartFile file, HttpServletRequest request) { 5 String fileName = file.getOriginalFilename().toLowerCase(); 6 if(!fileName.endsWith(".xls") && !fileName.endsWith(".xlsx")){ 7 throw new ApplicationException("请上传Excel文件"); 8 } 9 //获取文件 10 CommonsMultipartFile commonsMultipartFile = (CommonsMultipartFile)file; 11 //获取xml文件头配置(我的xml文件是放在/template/import/point目录下的) 12 String xmlConfigPath = String.format("%s%s", request.getServletContext().getRealPath("/"), "/template/import/point/"); 13 String xmlConfigName = String.format("%s%s", xmlConfigPath, "point-head.xml"); 14 Map<String,Object> beans = new HashMap<>(); 15 ExcelHead excelHead = new ExcelHead(); 16 beans.put("excelHead",excelHead); 17 //解析文件头 18 this.parseExcelService.parseExcel(xmlConfigName,commonsMultipartFile,beans); 19 if(!excelHead.getPoint().equals(excelHead.getHead())){ 20 throw new RuntimeException("模板错误"); 21 } 22 //获取文件体信息 23 xmlConfigName = String.format("%s%s", xmlConfigPath, "point-body.xml"); 24 List<PointVo> pointVoList = new ArrayList<>(); 25 beans.clear(); 26 beans.put("pointList",pointVoList); 27 //解析文件体 28 parseExcelService.parseExcel(xmlConfigName,commonsMultipartFile,beans); 29 this.pointService.importExcel(pointVoList); 30 return Response.success("导入成功"); 31 }
Response类携带着操作结束后的信息,供前端人员调用
执行到this.pointService.importExcel(pointVoList)这段代码意味着要导入的Excel中的数据已经放到pointList中了,执行pointService中的方法把数据批量导入进数据库中就完成了.
解析Excel工具类:ParseExcelService
public class ParseExcelService { /** * 解析Excel * * @param xmlConfigName xml配置(字段和Excel单元格的映射) * @param commonsMultipartFile 上传的文件 * @param beans 解析后的数据 */ public void parseExcel(String xmlConfigName, CommonsMultipartFile commonsMultipartFile, Map<String, Object> beans) { InputStream inputStream = null; FileInputStream xmlConfig = null; InputStream inputXML = null; InputStream inputXLS = null; try { //上传的文件流 inputStream = commonsMultipartFile.getInputStream(); //xml配置的文件流 xmlConfig = new FileInputStream(xmlConfigName); //执行解析 inputXML = new BufferedInputStream(xmlConfig); XLSReader mainReader = ReaderBuilder.buildFromXML(inputXML); inputXLS = new BufferedInputStream(inputStream); mainReader.read(inputXLS, beans); } catch (XLSDataReadException e) { logger.error("数据转换异常:", e); throw new ApplicationException("数据转换异常"); } catch (InvalidFormatException e) { logger.error("无效格式异常:", e); throw new ApplicationException("无效格式异常"); } catch (FileNotFoundException e) { logger.error("文件未找到:", e); throw new ApplicationException("文件未找到"); } catch (SAXException e) { logger.error("SAX解析异常:", e); throw new ApplicationException("SAX解析异常"); } catch (IOException e) { logger.error("IO流异常:", e); throw new ApplicationException("IO流异常"); } finally { try { if (inputXLS != null) inputXLS.close(); if (inputXML != null) inputXML.close(); if (xmlConfig != null) xmlConfig.close(); if (inputStream != null) inputStream.close(); } catch (IOException e) { logger.error("parse excel error", e); } } } }
以上是关于jxls导入Excel的问题的主要内容,如果未能解决你的问题,请参考以下文章
java jxls导入excel 的多个sheet 代码该如何写 配置文件怎么写? 还有jxls 能读入07 excel吗?