JXLS初次使用总结:Jxls是能通过xml设置把excel表的某一列映射到java对象的某一个属性当中去
使用思路及步骤如下图:
A.
<?xml version="1.0" encoding="UTF-8"?> <workbook> <worksheet idx="0"> <section startRow="0" endRow="0" /> <!--这里可以理解为忽略前几行,下标从0开始--> <loop startRow="1" endRow="1" items="persons" var="person" varType="pojo.PersonPojo"><!--从第二行开始读取--> <!-- 有些单元格可赋值给多个属性的 --> <section startRow="1" endRow="1"> <mapping cell="A2">person.name</mapping><!--映射A2往下数据--> <mapping cell="B2">person.mobile</mapping><!--映射B2往下数据--> </section> <loopbreakcondition><!--结束循环条件--> <rowcheck offset="0"> <cellcheck offset="0" /> </rowcheck> </loopbreakcondition> </loop> </worksheet> </workbook>
B.
package pojo; public class PersonPojo { private String name; private String mobile; public PersonPojo(String name,String mobile){ this.name = name; this.mobile = mobile; } public String getName() { return name; } public String getMobile() { return mobile; } public void setName(String name) { this.name = name; } public void setMobile(String mobile) { this.mobile = mobile; } }
C.
FileInputStream xmlFin = new FileInputStream(new File("D:\\开发DEMO\\使用jxls读取excell\\Project\\src\\main\\java\\xml\\PersonXML.xml")); FileInputStream dataFin = new FileInputStream(new File("D:\\测试目录\\123.xlsx")); InputStream inputXML = new BufferedInputStream(xmlFin); InputStream inputXLS = new BufferedInputStream(dataFin); List<PersonPojo> persons = new ArrayList<PersonPojo>(); Map<String,Object> beans = new HashMap<String,Object>(); beans.put("persons",persons); XLSReader mainReader = ReaderBuilder.buildFromXML(inputXML); XLSReadStatus readStatus = mainReader.read(inputXLS,beans); for(PersonPojo item:persons){ System.out.println(item.getName()+":"+item.getMobile()); }