JXLS使用方法(文件上传读取)xlsx文件读取
Posted Ambitious耗子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JXLS使用方法(文件上传读取)xlsx文件读取相关的知识,希望对你有一定的参考价值。
1.官方文档:http://jxls.sourceforge.net/reference/reader.html
2.demo git地址:https://bitbucket.org/leonate/jxls-demo
3.maven添加
<groupId>org.jxls</groupId>
<artifactId>jxls-reader</artifactId>
<version>2.0.2</version>
</dependency>
<?xml version="1.0" encoding="ISO-8859-1"?>
<workbook>
<worksheet name="Sheet1">(文件页数)
<section startRow="0" endRow="0">(表头开始至结束行)
</section>
<loop startRow="1" endRow="1" items="list" var="soImportDTO" varType="com.domain.dto.SoImportDTO">(开始循环读取文件数据,配置开始行,items映射的list var映射的bean varType 类路径)
<section startRow="1" endRow="1">(循环开始行)
<mapping row="1" col="0">soImportDTO.extno</mapping>
<mapping row="1" col="1">soImportDTO.whcode</mapping>
<mapping row="1" col="2">soImportDTO.type</mapping>
<mapping row="1" col="3">soImportDTO.soLineNo</mapping>
<mapping row="1" col="4">soImportDTO.sku</mapping>
<mapping row="1" col="5">soImportDTO.qty</mapping>
<mapping row="1" col="6">soImportDTO.supplierCode</mapping>
<mapping row="1" col="7">soImportDTO.supplierName</mapping>
<mapping row="1" col="8">soImportDTO.carrierCode</mapping>
<mapping row="1" col="9">soImportDTO.carrierName</mapping>
<mapping row="1" col="10">soImportDTO.shipDate</mapping>
<mapping row="1" col="11">soImportDTO.carNo</mapping>
<mapping row="1" col="12">soImportDTO.shipFromContact</mapping>
<mapping row="1" col="13">soImportDTO.shipFromPhone</mapping>
<mapping row="1" col="14">soImportDTO.remark</mapping>(以上是配置每列对应的bean的属性)
</section>
<loopbreakcondition>(结束条件配置)
<rowcheck offset="0">
<cellcheck offset="0">以什么条件结束写在这里 若是为空结束这不填</cellcheck>
</rowcheck>
</loopbreakcondition>
</loop>
</worksheet>
</workbook>
表头样式
读取文件代码:
List<SoImportDTO> soImportDTOList = new ArrayList<>(); Map<String,List<SoImportDTO>> beans = new HashMap<>(); InputStream inputXML = new BufferedInputStream(getClass().getResourceAsStream("/xmlMapper/so-import.xml")); XLSReader mainReader = null; try { mainReader = ReaderBuilder.buildFromXML( inputXML ); } catch (IOException e1) { log.error("读取配置文件失败"+e1); throw new ApplicationException(ErrorCode.READ_CONFIG_FAIL.code(), ErrorCode.READ_CONFIG_FAIL.message()); } catch (SAXException e1) { log.error("读取配置文件失败"+e1); throw new ApplicationException(ErrorCode.READ_CONFIG_FAIL.code(), ErrorCode.READ_CONFIG_FAIL.message()); } InputStream inputXLS = null; try { inputXLS = file.getInputStream(); } catch (IOException e1) { throw new ApplicationException(ErrorCode.READ_ORDER_FAIL.code(), ErrorCode.READ_ORDER_FAIL.message()); } List<SoImportDTO> soImportDTOs = new ArrayList<>(); beans.put("list", soImportDTOs); ConvertUtils.register(new DateConverter(), Date.class); try { XLSReadStatus readStatus = mainReader.read(inputXLS, beans); } catch (InvalidFormatException e1) { log.error("将数据映射到bean时出错"+e1); throw new ApplicationException(ErrorCode.MAPPED_FAIL.code(), ErrorCode.MAPPED_FAIL.message()); } catch (IOException e1) { log.error("将数据映射到bean时出错"+e1); throw new ApplicationException(ErrorCode.MAPPED_FAIL.code(), ErrorCode.MAPPED_FAIL.message()); }
完成上述之后操作之后 数据就已经map进bean里 到beans里面去拿list就ok
以上是关于JXLS使用方法(文件上传读取)xlsx文件读取的主要内容,如果未能解决你的问题,请参考以下文章
为啥java读不了.xlsx的excel文件只能读.xls的,难道是因为我的jxl.jar包太久了?
java jxls操作excel,怎么把富文本框的内容转换成excel格式的内容