JXLS使用方法(文件上傳讀取)xlsx文件讀取


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM