我想上传一个excel文件与角,并通过REST发送到后端。该文件以.xlsx文件的形式在MS中创建。但是,当我试图读取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在这里,有角度的休息呼叫:
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);
}
....有什么帮助吗?
发布于 2022-09-05 11:18:07
请在您的呼叫中尝试以下标题:
this.http.post('/rest/upload', formData)....根据某些环境/浏览器,头似乎可以工作,但是暂时删除它们。
public ResponseEntity<String> uploadExcel(@RequestParam("file") MultipartFile excelFile) {然后创建如下工作簿:
workbook = new XSSFWorkbook(excelFile.getInputStream());你能帮我查一下这个吗?
希望查看完整演示答案的人,可以在这里查看github条目:
https://stackoverflow.com/questions/73583646
复制相似问题