简单地读取和写入Angular SheepJs不保留XLSM文件的样式和宏

发布于 2025-01-25 21:45:14 字数 1430 浏览 3 评论 0原文

我在资产文件夹中有一个XLSM文件,我需要作为下载文件读取和返回。我能够阅读该文件。

以下代码基本上将斑点作为文件下载,它保留了宏和样式。

this.http.get(..., {responseType: 'blob' as 'json'}).subscribe((response:any)=>{
    let dataType = response.type;
    let binaryData = [];
    binaryData.push(response);
    let downloadLink = document.createElement('a');
    downloadLink.href = window.URL.createObjectURL(new Blob(binaryData, {type: dataType}));
    if (this.fileName)
        downloadLink.setAttribute('download', this.fileName);
    document.body.appendChild(downloadLink);
    downloadLink.click();
});

但是,我需要修改一些数据,并且我使用的是SheetJ(XLSX)对其进行修改,但它没有保留宏和样式。

以下代码使用XLSX简单地将其读取并写入文件,并且它仍在丢失宏和样式,仅保留数据。

this.http.get(..., {responseType: 'blob' as 'json'}).subscribe((response:any)=>{
    const reader: FileReader = new FileReader();
    reader.onload = (e: any) => {
        const bstr: string = e.target.result;
        const wb: XLSX.WorkBook = XLSX.read(bstr, {
            type: 'binary',
            bookVBA :true,
            cellStyles:true,
            cellHTML:true });
        XLSX.writeFile(wb, this.fileName,{
            type: 'binary',
            bookVBA:true,
            bookType: 'xlsm',
            cellStyles:true,
            bookSST:true,
        });
    };
    reader.readAsBinaryString(response);
});

即使设置bookvbacellstyles true也无法解决。

我正在使用

I have an xlsm file in assets folder, which I need to reading and returning as a download file. I am able to read the file.

The following code, which basically downloads the blob as a file, it retains the macros and styling.

this.http.get(..., {responseType: 'blob' as 'json'}).subscribe((response:any)=>{
    let dataType = response.type;
    let binaryData = [];
    binaryData.push(response);
    let downloadLink = document.createElement('a');
    downloadLink.href = window.URL.createObjectURL(new Blob(binaryData, {type: dataType}));
    if (this.fileName)
        downloadLink.setAttribute('download', this.fileName);
    document.body.appendChild(downloadLink);
    downloadLink.click();
});

However, I need to modify some data, and I am using SheetJs(xlsx) to modify it, but it is not retaining the macros and styling.

The following code uses xlsx to simply read and write it to a file and it is still losing macros and styling, only retaining the data.

this.http.get(..., {responseType: 'blob' as 'json'}).subscribe((response:any)=>{
    const reader: FileReader = new FileReader();
    reader.onload = (e: any) => {
        const bstr: string = e.target.result;
        const wb: XLSX.WorkBook = XLSX.read(bstr, {
            type: 'binary',
            bookVBA :true,
            cellStyles:true,
            cellHTML:true });
        XLSX.writeFile(wb, this.fileName,{
            type: 'binary',
            bookVBA:true,
            bookType: 'xlsm',
            cellStyles:true,
            bookSST:true,
        });
    };
    reader.readAsBinaryString(response);
});

even after setting bookVBA and cellStyles to true isn't resolving it.

I am using the latest version of SheetJs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文