帮助jxls读者

时间:2011-02-21 10:31:29

标签: apache-poi

HI

我是jxls的新手。我阅读了有关它的文章,但似乎没有那么好的例子。我正在通过java读取xls文件。 请找到简单的附加xls文件。

1)

public class Employee {   

    private String rollno;   
    private String name;   
    private String percentage;

2)

<worksheet name="Sheet1">
  <section endRow="1" startRow="0"/>
  <loop endRow="4" items="employee" startRow="3" var="emp" varType="com.Employee">
    <section endRow="4" startRow="3">
      <mapping col="0" row="3">emp.rollno</mapping>
      <mapping col="1" row="3">emp.name</mapping>
      <mapping col="2" row="3">emp.percentage</mapping>
    </section>
  </loop>
</worksheet>

3)

public class Client {   
    public static void main(String[] args) throws IOException, SAXException {   
          InputStream inputXML = new BufferedInputStream(new FileInputStream("C:\\NewFile1.xml"));     
          XLSReader mainReader = ReaderBuilder.buildFromXML( inputXML );     
          InputStream inputXLS = new BufferedInputStream(new FileInputStream("c:\\Book1.xls"));    

         Employee employee = new Employee();   

         System.out.println("Is Null inputXML:"+inputXML==null);   
         System.out.println("Is Null inputXLS:"+inputXLS==null);   

         Map beans = new HashMap();   
         beans.put("employee", employee);   
         XLSReadStatus readStatus = mainReader.read( inputXLS, beans);   
         readStatus.toString();   
    }   
}  

我得到一个例外

Feb 21, 2011 3:59:17 PM net.sf.jxls.reader.XLSReaderImpl readSheet
INFO: Processing sheet Sheet1
Exception in thread "main" java.lang.NullPointerException
    at net.sf.jxls.reader.XLSForEachBlockReaderImpl.read(XLSForEachBlockReaderImpl.java:45)
    at net.sf.jxls.reader.XLSSheetReaderImpl.read(XLSSheetReaderImpl.java:25)
    at net.sf.jxls.reader.XLSReaderImpl.readSheet(XLSReaderImpl.java:45)
    at net.sf.jxls.reader.XLSReaderImpl.read(XLSReaderImpl.java:31)
    at com.GetTheData.main(GetTheData.java:41)

请帮助解决这个问题。

谢谢

2 个答案:

答案 0 :(得分:0)

您需要定义loopbreakcondition以根据某些条件停止循环迭代。

例如:

<worksheet name="Sheet1">
  <section endRow="1" startRow="0"/>
  <loop endRow="4" items="employee" startRow="3" var="emp" varType="com.Employee">
    <section endRow="4" startRow="3">
      <mapping col="0" row="3">emp.rollno</mapping>
      <mapping col="1" row="3">emp.name</mapping>
      <mapping col="2" row="3">emp.percentage</mapping>
    </section>
    <loopbreakcondition>
        <rowcheck offset="0">
            <cellcheck offset="0">END</cellcheck>
        </rowcheck>
    </loopbreakcondition>    
  </loop>
</worksheet>

请查看user guide了解详情。

答案 1 :(得分:0)

Client.java这一行:beans.put(“employee”,employee); 改成 :  beans.put(“employee”,new ArrayList());

适合我。