问题描述
在 Angular 中使用 kendo-grid-excel
时如何设置原始文件内容类型?
这是我生成excel的模板:
<kendo-grid-excel fileName="file.xlsx">
<kendo-excelexport-column
*ngFor="let i of header.properties"
[field]="i.name"
[title]="i.name">
</kendo-excelexport-column>
</kendo-grid-excel>
我导出excel,修改一下。然后我将它上传到我的 BE,它是用 C# 编写的并使用 NPOI。在 BE 上,我在尝试读取 excel 时遇到错误:
NPOI.OpenXml4Net.Exceptions.InvalidFormatException: 'Package should contain a content type part [M1.13]'
。
我发现 here 解决我的问题的方法是使用
application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
而不是 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
。
<kendo-upload
[saveUrl]="saveUrl"
(upload)="uploadHandler($event)"
[accept]="'.xlsx'">
import
</kendo-upload>
和处理程序:
uploadHandler(e: UploadEvent) {
e.files[0].rawFile.type = "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
e.data = {
file: e.files[0],fileScructure: this.header.properties.map(x => x.name)
};
}
但我收到一个错误:Cannot assign to 'type' because it is a read-only property.
那么,我的问题是如何更改类型?
更新
看来我必须在excel导出上正确设置类型。所以,我需要以某种方式在 kendo-grid-excel
中提供类型。到目前为止,我无法找到如何做到这一点。
更新 2
这看起来像是 NPOI 方面的问题。因为根据 docs,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
是正确的 .xlsx 类型。所以,我尝试将NPOI更新到最新的1.2.3版本,但没有解决问题。我仍然收到错误:NPOI.OpenXml4Net.Exceptions.InvalidFormatException: 'Package should contain a content type part [M1.13]'
。
更新 3
为了更好地诊断问题,我使用 NPOI 生成了一个 .xlsx 文件,并检查我是否可以读取它。我可以用 NPOI 阅读它。因此,我继续尝试找出两个 .xlsx 文件之间的任何差异。我发现的唯一区别如下。
当我右键单击文件时,转到属性 - 安全 - 高级我可以看到资源属性 > 点击 IMAGELOAD: 1
时显示 kendo-generated.xlsx
的下拉菜单。而对于 npoi-generated.xlsx
,我看到的是 Integrity level: High Mandatory Level
。也许这就是阻止 NPOI 读取 kendo-generated.xlsx
的原因?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)