使用jxls将excel文件读入java bean

时间:2014-08-24 07:11:44

标签: java jxls

我正在尝试使用 jXLS库读取excel文件,我想使用hashmaps使其动态,但我被困在那里。以下是相关课程。

请帮助使用hashmaps而不是arraylists。

JXLSParser

package com.metricstream.service.xlsparser;

       import java.io.InputStream;
       import java.util.ArrayList;
       import java.util.HashMap;
       import java.util.List;
       import java.util.Map;

       import net.sf.jxls.reader.ReaderBuilder;
       import net.sf.jxls.reader.XLSReadStatus;
       import net.sf.jxls.reader.XLSReader;

       import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
       import org.xml.sax.SAXException;

       public class JXLSParser {
       private static final String CHAR_S = "s";

       public <T> List<T> parseXLS(InputStream xmlStream, InputStream xlsStream,
        Class<T> tClass) throws AppException {
    List<T> tList = new ArrayList<T>();

    XLSReader mainReader = null;

    try {
        mainReader = ReaderBuilder.buildFromXML(xmlStream);
    } catch (IOException ioException) {
        throw new AppException(ioException);
    } catch (SAXException saxException) {
        throw new AppException(saxException);
    }

    Map<String, Object> beans = new HashMap<String, Object>();
    beans.put(tClass.getSimpleName().toLowerCase().concat(CHAR_S), tList);
    XLSReadStatus readStatus = null;

    try {
        readStatus = mainReader.read(xlsStream, beans);
    } catch (InvalidFormatException invalidFormatException) {
        throw new AppException(invalidFormatException);
    } catch (IOException ioException) {
        throw new AppException(ioException);
    }



    if (readStatus.isStatusOK()) {
        System.out.println("Read Status: ok");

    } else {
        throw new AppException("xls cannot be read properly");
    }

    return tList;
}
}

主要

     package com.metricstream.service.xlsparser;

     import java.io.BufferedInputStream;
     import java.io.FileInputStream;
     import java.io.FileNotFoundException;
     import java.io.InputStream;
     import java.util.HashMap;
      import java.util.List;
     import java.util.Map;

     import org.apache.poi.ss.formula.functions.T;

     import com.metricstream.exercise.employee.Employee;

    public class Exercise {
       public static void main(String args[]) throws AppException {
    String xmlConfig = "F:\\workspace/XLSReader/XLSReader/XLSReader/employee.xml";
    String dataXLS = "F:\\workspace/XLSReader/XLSReader/XLSReader/employee.xlsx";
    JXLSParser employeeParser = new JXLSParser();
    InputStream inputXML;
    try {
        inputXML = new BufferedInputStream(new FileInputStream(xmlConfig));
        InputStream inputXLS = new BufferedInputStream(new FileInputStream(
                dataXLS));
        List<Employee> employee = employeeParser.parseXLS(inputXML,
                inputXLS, Employee.class);
    //  Map<String,Object> map = new HashMap<String,Object>(employee.size());
    /*  for( Employee emp: employee)
        {
            map.put(emp.getKey(),emp.getValue());
            System.out.println(emp);
        }*/

        System.out.println(employee);
    } catch (FileNotFoundException e) {
        throw new AppException(e);
    }

}
 }

Excel文件内容

enter image description here

2 个答案:

答案 0 :(得分:0)

<?xml version="1.0" encoding="ISO-8859-1"?>
<workbook>
    <worksheet name="Sheet1">
        <loop startRow="1" endRow="1" items="simples" var="simple"
            varType="java.util.HashMap">

            <section startRow="1" endRow="1">
                <mapping row="2" col="0">simple.firstName</mapping>
                <mapping row="2" col="1">simple.lastName</mapping>
                <mapping row="2" col="2">simple.age</mapping>
                <mapping row="2" col="3">simple.des</mapping>
                <mapping row="2" col="4">simple.salary</mapping> 
            </section>
            <loopbreakcondition>
                <rowcheck offset="0">
                    <cellcheck offset="0" />
                </rowcheck>
            </loopbreakcondition>
        </loop>
    </worksheet>
</workbook>

答案 1 :(得分:0)

尝试设置&#34; varType&#34;属性为&#34; java.util.HashMap&#34;并设置&#34;键入&#34;属性&#34;映射&#34;元素到&#34; java.util.String&#34;或其他。例如:

n_frames

它为我工作。 Jxls Reader版本是2.4.0