简单的JXLS / Apache POI示例失败

时间:2017-11-09 15:10:36

标签: java apache-poi jxls

我不知道我是否收到此错误。代码来自JXLS自己的示例,并且从那里下载了Jars。

错误代码:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at org.jxls.util.TransformerFactory.loadPoiTransformer(TransformerFactory.java:85)
    at org.jxls.util.TransformerFactory.getTransformerClass(TransformerFactory.java:69)
    at org.jxls.util.TransformerFactory.createTransformer(TransformerFactory.java:27)
    at org.jxls.util.JxlsHelper.createTransformer(JxlsHelper.java:250)
    at org.jxls.util.JxlsHelper.processTemplate(JxlsHelper.java:108)
    at test_excel.ObjectCollectionDemo.main(ObjectCollectionDemo.java:32)

Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 8 more

参考图书馆:

/slf4j-1.7.25/slf4j-api-1.7.25.jar
/slf4j-1.7.25/slf4j-jdk14-1.7.25.jar
/jxls-2.4.1 2 / dist / jxls-reader-2.0.3.jar
/jxls-2.4.1 2 / dist / jxls-poi-1.0.13.jar
/xls-2.4.1 2 / dist / jxls-jexcel-1.0.6.jar
/jxls-2.4.1 2 / dist / jxls-2.4.1.jar

主要代码:

package test_excel;

import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException; 
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;


public class ObjectCollectionDemo {
    private static Logger logger = LoggerFactory.getLogger(ObjectCollectionDemo.class);

    public static void main(String[] args) throws ParseException, IOException {
        logger.info("Running Object Collection demo");
        List<Employee> employees = generateSampleEmployeeData();

        try(InputStream is = ObjectCollectionDemo.class.getResourceAsStream("object_collection_template.xls")) {
            try (OutputStream os = new FileOutputStream("object_collection_output.xls")) {
                Context context = new Context();
                context.putVar("employees", employees);

                logger.info("OK so far...");

                JxlsHelper.getInstance().processTemplate(is, os, context);
            }
        }
    }  

    public static List<Employee> generateSampleEmployeeData() throws ParseException {
        List<Employee> employees = new ArrayList<Employee>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd", Locale.US);

        employees.add( new Employee("Elsa", dateFormat.parse("1970-Jul-10"), 1500, 0.15) );
        employees.add( new Employee("Oleg", dateFormat.parse("1973-Apr-30"), 2300, 0.25) );
        employees.add( new Employee("Neil", dateFormat.parse("1975-Oct-05"), 2500, 0.00) );
        employees.add( new Employee("Maria", dateFormat.parse("1978-Jan-07"), 1700, 0.15) );
        employees.add( new Employee("John", dateFormat.parse("1969-May-30"), 2800, 0.20) );

        return employees;
    }
}

1 个答案:

答案 0 :(得分:2)

您的班级路径中似乎需要poi-3.XX.jar - 请参阅here