使用Apache POI从字符串中读取excel文件

时间:2013-11-11 16:52:50

标签: java excel apache-poi

我正在尝试使用Apache POI 3.9从字符串中读取excel文件,但没有任何成功。我对java不太熟悉。

只是为了澄清一下,在我的程序中我已经将excel文件作为字符串了,我正在通过使用readFile函数来模拟这种行为。

程序:

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class Test {

    static String readFile(String path, Charset encoding) throws IOException 
    {
        byte[] encoded = Files.readAllBytes(Paths.get(path));
        return encoding.decode(ByteBuffer.wrap(encoded)).toString();
    }

    public static void main(String[] args) throws IOException, InvalidFormatException {
        String result = readFile("data.xlsx", StandardCharsets.UTF_8);

        InputStream is = new ByteArrayInputStream(result.getBytes("UTF-8"));

        Workbook book = WorkbookFactory.create(is);
    }

}

我得到的错误是:

Exception in thread "main" java.util.zip.ZipException: invalid block type
    at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:164)
    at java.util.zip.ZipInputStream.read(ZipInputStream.java:193)
    at java.io.FilterInputStream.read(FilterInputStream.java:107)
    at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource$FakeZipEntry.<init>(ZipInputStreamZipEntrySource.java:127)
    at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:55)
    at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:83)
    at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:267)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:73)
    at Test.main(Test.java:28)

任何帮助都将不胜感激。

欢呼声

4 个答案:

答案 0 :(得分:4)

所以解决我的问题是

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class Test {

    public static void main(String[] args) throws IOException, InvalidFormatException {
        byte[] result = Files.readAllBytes(Paths.get("data.xlsx"));     
        InputStream is = new ByteArrayInputStream(result);
        Workbook book = WorkbookFactory.create(is);
    }

}

答案 1 :(得分:3)

看起来你这样做太复杂了。只需按照Apache POI Quick Guide,即建议使用FileInputStream阅读文件。不需要将字节读入字节数组并使用ByteArrayInputStream

使用从指南中复制的以下内容之一:

// Use a file
Workbook wb = WorkbookFactory.create(new File("MyExcel.xls"));

// Use an InputStream, needs more memory
Workbook wb = WorkbookFactory.create(new FileInputStream("MyExcel.xlsx"));

答案 2 :(得分:0)

你正在做什么?您正在将二进制文件读入byte[]并使用UTF-8将其转换为String。稍后您将再次使用UTF-8将其转换回字节流。做什么的?跳过中间的所有步骤:

public static void main(String[] args) throws IOException, InvalidFormatException {
    InputStream is = new FileInputStream("data.xlsx");
    Workbook book = WorkbookFactory.create(is);
}

答案 3 :(得分:0)

这让我困扰了一段时间。没有一个建议的修复对我有用。解决该问题的方法是在 maven-resources-plugin 中添加一个,因此

        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
            <configuration>
              <encoding>UTF-8</encoding>
              <nonFilteredFileExtensions>
                <nonFilteredFileExtension>docx</nonFilteredFileExtension>
                <nonFilteredFileExtension>xls</nonFilteredFileExtension>
                <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
              </nonFilteredFileExtensions>
            </configuration>
        </plugin>