在使用Excel工作簿例外 [英] exception while using excel workbook

查看:205
本文介绍了在使用Excel工作簿例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的code让这些异常而我M在Excel工作簿中使用POI jar文件写一些数据:

异常线程mainjava.lang.NoClassDefFoundError的:组织/阿帕奇/ POI / UnsupportedFileFormatException
    在java.lang.ClassLoader.defineClass1(本机方法)
    在需要java.lang.ClassLoader.defineClass(来源不明)
    在java.security.SecureClassLoader.defineClass(来源不明)
    在java.net.URLClassLoader.defineClass(来源不明)
    在java.net.URLClassLoader.access $ 100(来源不明)
    在java.net.URLClassLoader的$ 1.run(来源不明)
    在java.net.URLClassLoader的$ 1.run(来源不明)
    在java.security.AccessController.doPrivileged(本机方法)
    在java.net.URLClassLoader.findClass(来源不明)
    在java.lang.ClassLoader.loadClass(来源不明)
    在sun.misc.Launcher $ AppClassLoader.loadClass(来源不明)
    在java.lang.ClassLoader.loadClass(来源不明)
    在WorkBookDemo.main(WorkBookDemo.java:27)
抛出java.lang.ClassNotFoundException:引起org.apache.poi.UnsupportedFileFormatException
    在java.net.URLClassLoader的$ 1.run(来源不明)
    在java.net.URLClassLoader的$ 1.run(来源不明)
    在java.security.AccessController.doPrivileged(本机方法)
    在java.net.URLClassLoader.findClass(来源不明)
    在java.lang.ClassLoader.loadClass(来源不明)
    在sun.misc.Launcher $ AppClassLoader.loadClass(来源不明)
    在java.lang.ClassLoader.loadClass(来源不明)
    ... 13个
我添加以下jar文件:
1)的xmlbeans-2.4.0
2)POI-OOXML-架构 - 3.11
3)POI-3.11
4)的commons-logging-1.1
5)dom4j的-1.6.1
6)的log4j-1.2.17

 进口的java.io.File;
    进口java.io.FileOutputStream中;    进口的java.util.Map;
    进口java.util.Set中;
    进口java.util.TreeMap中;    进口org.apache.poi.ss.usermodel.Cell;
    进口org.apache.poi.ss.usermodel.Row;
    进口org.apache.poi.xssf.usermodel.XSSFSheet;
    进口org.apache.poi.xssf.usermodel.XSSFWorkbook;    公共类WorkBookDemo {       公共静态无效的主要(字串[] args)
       {
            //空白工作簿
            XSSFWorkbook工作簿=新XSSFWorkbook();            //创建一个空白页
            XSSFSheet表= workbook.createSheet(员工数据);            //该数据需要被写入(对象[])
            地图<弦乐,对象[]>数据=新TreeMap的<弦乐,对象[]>();
            data.put(1,新的对象[] {ID,名,姓氏});
            data.put(2,新的对象[] {1,阿密特,舒克拉});
            data.put(3,新的对象[] {2,LOKESH,古普塔});
            data.put(4,新的对象[] {3,约翰,Adwards});
            data.put(5,新的对象[] {4,布莱恩,舒尔茨});           //迭代数据并写入片
            SET<串GT;键集= data.keySet();
            INT ROWNUM = 0;
            对于(字符串键:键集)
            {
                鳞次栉比= sheet.createRow(ROWNUM ++);
                [对象] objArr = data.get(键);
                INT cellnum = 0;
                对于(obj对象:objArr)
                {
                    电池单元= row.createCell(cellnum ++);
                    如果(OBJ的instanceof字符串)
                        cell.setCellValue((字符串)目标文件);
                    否则,如果(OBJ的instanceof整数)
                        cell.setCellValue((整数)OBJ);
                }
            }
            尝试
            {
            //写在文件系统中的工作簿
                FileOutputStream中出=新的FileOutputStream(新文件(exps.xlsx));
                workbook.write(出);
                out.close();
                的System.out.println(exps.xlsx在磁盘上写入成功。);
            }
            赶上(例外五)
            {
                e.printStackTrace();
            }
        }    }


解决方案

 的FileOutputStream出=新的FileOutputStream(新文件(exps.xlsx));

文件输出流是写入输出流数据文件或FileDescriptor。是否一个文件是可或可被创建取决于底层平台。一些平台,特别是允许一个文件只由一个的FileOutputStream(或其他文件写入对象)在同一时间内开进行写入。在这种情况下,在这个类的构造函数,如果所涉及的文件已经打开,就会失败。

由于要明确创建一个文件对象,并通过同为的FileOutputStream 构造函数。它假定文件exps.xlsx已创建。 [参考]

柜面,实在不行,你只需在的FileOutputStream 构造函数传递文件的名称。

 的FileOutputStream出=新的FileOutputStream(exps.xlsx);

这将自动创建文件,柜面已经没有创建它。

此外,我测试你定的code和使用以下的Maven 依赖和它的工作。

 <&依赖性GT;
    <&的groupId GT; org.apache.poi< /的groupId>
    <&的artifactId GT; POI< / artifactId的>
    <&版GT; 3.10决赛< /版本>
< /依赖性>
<&依赖性GT;
    <&的groupId GT; org.apache.poi< /的groupId>
    <&的artifactId GT; POI-OOXML< / artifactId的>
    <&版GT; 3.10 LT; /版本>
< /依赖性>

您不使用行家项目柜面,你可以明确地添加上述罐子上述版本。

I am getting these exceptions in my code while i m writing some data in excel workbook using poi jars:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.defineClass(Unknown Source) at java.net.URLClassLoader.access$100(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at WorkBookDemo.main(WorkBookDemo.java:27) Caused by: java.lang.ClassNotFoundException: org.apache.poi.UnsupportedFileFormatException at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 13 more I added following jars: 1)xmlbeans-2.4.0 2)poi-ooxml-schemas-3.11 3)poi-3.11 4)commons-logging-1.1 5)dom4j-1.6.1 6)log4j-1.2.17

    import java.io.File;
    import java.io.FileOutputStream;

    import java.util.Map;
    import java.util.Set;
    import java.util.TreeMap;

    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;

    public class WorkBookDemo {

       public static void main(String[] args)
       {
            //Blank workbook
            XSSFWorkbook workbook = new XSSFWorkbook();

            //Create a blank sheet
            XSSFSheet sheet = workbook.createSheet("Employee Data");

            //This data needs to be written (Object[])
            Map<String, Object[]> data = new TreeMap<String, Object[]>();
            data.put("1", new Object[] {"ID", "NAME", "LASTNAME"});
            data.put("2", new Object[] {1, "Amit", "Shukla"});
            data.put("3", new Object[] {2, "Lokesh", "Gupta"});
            data.put("4", new Object[] {3, "John", "Adwards"});
            data.put("5", new Object[] {4, "Brian", "Schultz"});

           //Iterate over data and write to sheet
            Set<String> keyset = data.keySet();
            int rownum = 0;
            for (String key : keyset)
            {
                Row row = sheet.createRow(rownum++);
                Object [] objArr = data.get(key);
                int cellnum = 0;
                for (Object obj : objArr)
                {
                    Cell cell = row.createCell(cellnum++);
                    if(obj instanceof String)
                        cell.setCellValue((String)obj);
                    else if(obj instanceof Integer)
                        cell.setCellValue((Integer)obj);
                }
            }
            try
            {
            //Write the workbook in file system
                FileOutputStream out = new FileOutputStream(new File("exps.xlsx"));
                workbook.write(out);
                out.close();
                System.out.println("exps.xlsx written successfully on disk.");
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

    }

解决方案

FileOutputStream out = new FileOutputStream(new File("exps.xlsx"));

A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.

Since you are explicitly creating a File object, and passing the same to FileOutputStream constructor. It assumes that the file "exps.xlsx" is already created. [Reference.]

Incase, it is not, you simply pass the name of the file in FileOutputStream constructor.

FileOutputStream out = new FileOutputStream("exps.xlsx");

This will automatically create the file, incase it is already not created.

Moreover, I tested your given code and used the following Maven dependency and it worked.

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.10-FINAL</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.10</version>
</dependency>

Incase you are not using a maven project, you can explicitly add the aforementioned jars of the aforementioned versions.

这篇关于在使用Excel工作簿例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆