Jxls use method (file upload read) xlsx file read

Source: Internet
Author: User

1. Official Document: Http://jxls.sourceforge.net/reference/reader.html

2.demo git address: Https://bitbucket.org/leonate/jxls-demo

3.maven add

<dependency>
<groupId>org.jxls</groupId>
<artifactId>jxls-reader</artifactId>
<version>2.0.2</version>
</dependency> 4.xml configuration file (depending on the upload file structure configuration, here is the simplest file configuration)

<?xml version= "1.0" encoding= "Iso-8859-1"?>
<workbook>
<worksheet name= "Sheet1" > (file Pages)
<section startrow= "0" endrow= "0" > (table header start to end line)
</section>
<loop startrow= "1" endrow= "1" items= "list" var= "Soimportdto" vartype= "Com.domain.dto.SoImportDTO" > (start looping The file data, configure the start line, the items map of the list var mapped bean varType classpath)
<section startrow= "1" endrow= "1" > (Loop start line)
<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= "ten" >soImportDTO.shipDate</mapping>
<mapping row= "1" col= "one" >soImportDTO.carNo</mapping>
<mapping row= "1" col= ">soImportDTO.shipFromContact</mapping>"
<mapping row= "1" col= ">soImportDTO.shipFromPhone</mapping>"
<mapping row= "1" col= ">soImportDTO.remark</mapping>" (These are the properties of the bean that corresponds to the configuration of each column)
</section>
<loopbreakcondition> (end condition configuration)
<rowcheck offset= "0" >
<cellcheck offset= "0" >What is the condition of the end of writing here if the empty end of this does not fill</cellcheck>
</rowcheck>
</loopbreakcondition>
</loop>
</worksheet>
</workbook>

Table header Style

Read the file code:

List<soimportdto> soimportdtolist =NewArraylist<>(); Map<String,List<SoImportDTO>> beans =NewHashmap<>(); InputStream Inputxml=NewBufferedinputstream (GetClass (). getResourceAsStream ("/xmlmapper/so-import.xml")); Xlsreader Mainreader=NULL; Try{Mainreader=Readerbuilder.buildfromxml (Inputxml); } Catch(IOException E1) {Log.error ("Read Config file failed" +E1); Throw NewApplicationException (ErrorCode.READ_CONFIG_FAIL.code (), ErrorCode.READ_CONFIG_FAIL.message ()); } Catch(saxexception E1) {Log.error ("Read Config file failed" +E1); Throw NewApplicationException (ErrorCode.READ_CONFIG_FAIL.code (), ErrorCode.READ_CONFIG_FAIL.message ()); } InputStream Inputxls=NULL; Try{Inputxls=File.getinputstream (); } Catch(IOException E1) {Throw NewApplicationException (ErrorCode.READ_ORDER_FAIL.code (), ErrorCode.READ_ORDER_FAIL.message ()); } List<SoImportDTO> Soimportdtos =NewArraylist<>(); Beans.put ("List", Soimportdtos); Convertutils.register (NewDateConverter (), Date.class); Try{xlsreadstatus Readstatus=mainreader.read (Inputxls, beans); } Catch(invalidformatexception E1) {Log.error ("Error mapping data to Bean" +E1); Throw NewApplicationException (ErrorCode.MAPPED_FAIL.code (), ErrorCode.MAPPED_FAIL.message ()); } Catch(IOException E1) {Log.error ("Error mapping data to Bean" +E1); Throw NewApplicationException (ErrorCode.MAPPED_FAIL.code (), ErrorCode.MAPPED_FAIL.message ()); }

After the completion of the above operation, the data has been map into the bean into the beans to get the list OK

Jxls use method (file upload read) xlsx file read

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.