Java 使用Apache POI从字符串中读取excel文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19911690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read excel file from string using Apache POI
提问by ducky
I am trying to read an excel file from a string using Apache POI 3.9 without any success. I am not too familiar with java.
我正在尝试使用 Apache POI 3.9 从字符串中读取 excel 文件,但没有成功。我对java不太熟悉。
Just to clarify, in my program I already have the excel file as a string and I am mocking that behaviour by using the readFile function.
为了澄清一下,在我的程序中,我已经将 excel 文件作为字符串,并且我正在使用 readFile 函数来模拟该行为。
Program:
程序:
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);
}
}
The error I am getting 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)
Any help would be appreciated.
任何帮助,将不胜感激。
cheers
干杯
回答by isnot2bad
What are you doing? You're reading a binary file into a byte[]
and convert it to a String
using UTF-8. Later you're converting it back to a byte stream using UTF-8 again. What for? Skip all the steps inbetween:
你在干嘛?您正在将二进制文件读入 abyte[]
并String
使用 UTF-8将其转换为 a 。稍后您将再次使用 UTF-8 将其转换回字节流。做什么的?跳过中间的所有步骤:
public static void main(String[] args) throws IOException, InvalidFormatException {
InputStream is = new FileInputStream("data.xlsx");
Workbook book = WorkbookFactory.create(is);
}
回答by rgettman
It looks like you're making this way too complicated. Just follow the Apache POI Quick Guide, which suggests reading the file with a FileInputStream
. There's no need for reading the bytes into a byte array and using a ByteArrayInputStream
.
看起来你让这种方式太复杂了。只需按照Apache POI 快速指南,该指南建议使用FileInputStream
. 无需将字节读入字节数组并使用ByteArrayInputStream
.
Use one of the following, copied from the guide:
使用从指南中复制的以下内容之一:
// 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"));
回答by ducky
So the fix for my problem was
所以我的问题的解决方法是
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);
}
}