[英]Angular excel file upload throws NotOfficeXmlFileException
我想用 Angular 上传 excel 文件并通过 REST ZDB974238714CA8DE634A7CE1D8 发送到后端。 该文件在 MS Excel 中创建为 .xlsx 文件。 但是当我尝试读取 excel 文件时,我在后端服务中遇到了以下异常:
org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException: No valid entries or contents found, this is not a valid OOXML (Office Open XML) file
at org.apache.poi.openxml4j.util.ZipArchiveThresholdInputStream.getNextEntry(ZipArchiveThresholdInputStream.java:156)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:94)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:132)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:312)
at org.apache.poi.ooxml.util.PackageHelper.open(PackageHelper.java:59)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:289)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:285)
at
Caused by: java.util.zip.ZipException: Unexpected record signature: 0X2D2D2D2D
at org.apache.commons.compress.archivers.zip.ZipArchiveInputStream.getNextZipEntry(ZipArchiveInputStream.java:296)
at org.apache.poi.openxml4j.util.ZipArchiveThresholdInputStream.getNextEntry(ZipArchiveThresholdInputStream.java:152)
... 86 more
这里 Angular Rest 调用:
onFileSelect(event): void {
const file: File = event.target.files[0];
if (file) {
const formData = new FormData();
formData.append("file", file);
this.http.post('/rest/upload', formData)
.map((response: Response) => {
let body = response.json();
return body || [];
})
.catch((error: Response) => {
return this.handleError(error);
});
}
}
这里是后端代码:
@RequestMapping(value = "/rest/upload", method = RequestMethod.POST, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
@ResponseBody
public ResponseEntity<List<BaselineJson>> uploadExcel(@FormDataParam("file") InputStream excelFile) {
XSSFWorkbook workbook = null;
try {
workbook = new XSSFWorkbook(excelFile);
//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);
} catch (IOException e) {
throw new RuntimeException(e);
}
....
有什么帮助吗?
请在您的通话中尝试以下标题:
let headers = new Headers();
headers.append('Content-Type', 'multipart/form-data');
headers.append('Accept', 'application/json');
let options = new RequestOptions({ headers: headers });
this.http.post('/rest/upload', formData, options)....
这将设置正确的内容类型,因此希望能够修复它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.