首页 > 解决方案 > 在 Angular 中使用“kendo-grid-excel”时如何设置原始文件内容类型?

问题描述

kendo-grid-excel在 Angular 中使用时如何设置原始文件内容类型?

这是我生成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,修改它。然后我将它上传到我用 C# 编写并使用NPOI的 BE 。在 BE 上,我在尝试读取 excel 时遇到错误: NPOI.OpenXml4Net.Exceptions.InvalidFormatException: 'Package should contain a content type part [M1.13]'.

我在这里发现解决我的问题的方法是使用 .application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet而不是application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.

以下是我将文件上传回 BE 的方式:

<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方面的一个问题。因为根据docsapplication/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

在此处输入图像描述

标签: angularexcelkendo-uicontent-typenpoi

解决方案


推荐阅读