1.官方文档:http://jxls.sourceforge.net/reference/reader.html
2.demo git地址:https://bitbucket.org/leonate/jxls-demo
3.maven添加
<dependency> <groupId>org.jxls</groupId> <artifactId>jxls-reader</artifactId> <version>2.0.2</version> </dependency> 4.xml配置文件(视上传文件结构配置,这里就最简单的文件作配置)<?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