使用excel工作簿时出现异常 [英] exception while using excel workbook

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

问题描述

当我使用 poi jar 在 excel 工作簿中写入一些数据时,我的代码中出现了这些异常:

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

我添加了以下 jars:

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

  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"));

文件输出流是用于写入的输出流数据到文件或文件描述符.文件是否可用或是否可以创建取决于底层平台.特别是某些平台,一次只允许一个 FileOutputStream(或其他文件写入对象)打开一个文件进行写入.在这种情况下,如果所涉及的文件已经打开,则此类中的构造函数将失败.

因为您显式创建了一个 File 对象,并将其传递给 FileOutputStream 构造函数.它假定文件 "exps.xlsx" 已经创建.[参考]

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.]

如果不是,您只需在 FileOutputStream 构造函数中传递文件名.

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.

此外,我测试了您给定的代码并使用了以下 Maven 依赖项并且它起作用了.

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>

如果您没有使用 maven 项目,您可以明确添加上述版本的上述 jars.

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

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

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