"Your InputStream was neither an OLE2 stream, nor an OOXML stream" 正在读取 .xlsx 文件
"Your InputStream was neither an OLE2 stream, nor an OOXML stream" while reading .xslx File
我想读取 Excel 个文件,我通过 REST 作为 InputStream
传递了这些文件。为此,我有 class ExcelImport
:
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
public class ExcelImport {
public boolean readStream(InputStream stream) {
boolean success;
try {
byte[] bytes = getBytes(stream);
InputStream wrappedStream = new ByteArrayInputStream(bytes);
Workbook workbook = WorkbookFactory.create(wrappedStream);
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
Sheet sheet = workbook.getSheetAt(i);
for (Row row : sheet) {
IterateThroughRow(row);
}
}
success = true;
} catch (FileNotFoundException e) {
success = false;
e.printStackTrace();
} catch (IOException e) {
success = false;
e.printStackTrace();
} catch (InvalidFormatException e) {
success = false;
e.printStackTrace();
}
return success;
}
private void IterateThroughRow(Row row) {
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
//do something with the content...
case Cell.CELL_TYPE_STRING:
cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
cell.getBooleanCellValue();
break;
default:
}
}
}
public static byte[] getBytes(InputStream is) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int len;
byte[] data = new byte[100000];
while ((len = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, len);
}
buffer.flush();
return buffer.toByteArray();
}
}
如果我 运行 这个用这个:
ExcelImport excelImport = new ExcelImport();
InputStream is = new FileInputStream("/path/to/file.xlsx");
excelImport.readStream(is);
它工作正常。但是,如果我在 REST 上将此 class 与来自 PUT
的 InputStream
一起使用,我会得到此异常:
java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream
我包装了流,因为如果我不这样做,我会得到这个异常:Package should contain a content type part
。我从 here.
那里得到了这个
是否可以使用 Apache POI 和来自 REST 的流来读取 Excel 文件?还是我做错了什么?
作为此类问题的一般指南,您应该尝试将服务器接收到的文件保存到磁盘。完成后,检查源文件和接收到的文件大小是否匹配,并具有相同的校验和(例如 md5)。令人惊讶的是,问题经常出在传输上,例如丢失文件的第一位或最后一位,或者在以 ascii 而非二进制格式上传时损坏某些字符,或类似的东西
对于意味着没有数据发送的错误的具体情况,有个好消息。从 r1677562, a more helpful EmptyFileException 开始,将抛出代替针对所有其他无效类型给出的更一般的 Your InputStream was neither an OLE2 stream, nor an OOXML stream
异常。这应该可以让您更容易地在您的代码中发现此类错误的原因。该异常将在 POI 3.12 final(及更高版本。
我遇到了同样的问题,我只是将电子表格重命名并重新保存为 .xlsx,然后就可以了。
我想读取 Excel 个文件,我通过 REST 作为 InputStream
传递了这些文件。为此,我有 class ExcelImport
:
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
public class ExcelImport {
public boolean readStream(InputStream stream) {
boolean success;
try {
byte[] bytes = getBytes(stream);
InputStream wrappedStream = new ByteArrayInputStream(bytes);
Workbook workbook = WorkbookFactory.create(wrappedStream);
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
Sheet sheet = workbook.getSheetAt(i);
for (Row row : sheet) {
IterateThroughRow(row);
}
}
success = true;
} catch (FileNotFoundException e) {
success = false;
e.printStackTrace();
} catch (IOException e) {
success = false;
e.printStackTrace();
} catch (InvalidFormatException e) {
success = false;
e.printStackTrace();
}
return success;
}
private void IterateThroughRow(Row row) {
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
switch (cell.getCellType()) {
//do something with the content...
case Cell.CELL_TYPE_STRING:
cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN:
cell.getBooleanCellValue();
break;
default:
}
}
}
public static byte[] getBytes(InputStream is) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int len;
byte[] data = new byte[100000];
while ((len = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, len);
}
buffer.flush();
return buffer.toByteArray();
}
}
如果我 运行 这个用这个:
ExcelImport excelImport = new ExcelImport();
InputStream is = new FileInputStream("/path/to/file.xlsx");
excelImport.readStream(is);
它工作正常。但是,如果我在 REST 上将此 class 与来自 PUT
的 InputStream
一起使用,我会得到此异常:
java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream
我包装了流,因为如果我不这样做,我会得到这个异常:Package should contain a content type part
。我从 here.
是否可以使用 Apache POI 和来自 REST 的流来读取 Excel 文件?还是我做错了什么?
作为此类问题的一般指南,您应该尝试将服务器接收到的文件保存到磁盘。完成后,检查源文件和接收到的文件大小是否匹配,并具有相同的校验和(例如 md5)。令人惊讶的是,问题经常出在传输上,例如丢失文件的第一位或最后一位,或者在以 ascii 而非二进制格式上传时损坏某些字符,或类似的东西
对于意味着没有数据发送的错误的具体情况,有个好消息。从 r1677562, a more helpful EmptyFileException 开始,将抛出代替针对所有其他无效类型给出的更一般的 Your InputStream was neither an OLE2 stream, nor an OOXML stream
异常。这应该可以让您更容易地在您的代码中发现此类错误的原因。该异常将在 POI 3.12 final(及更高版本。
我遇到了同样的问题,我只是将电子表格重命名并重新保存为 .xlsx,然后就可以了。