Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compression requested.. #156

Open
Zhenyi-Wang opened this issue Mar 13, 2021 · 2 comments
Open

Compression requested.. #156

Zhenyi-Wang opened this issue Mar 13, 2021 · 2 comments

Comments

@Zhenyi-Wang
Copy link

Hi, just want to be able to compress the exported xlsx file.. as the origional SheetJs version could do. Is it diffiluct to merge the function?

@vodnokoki
Copy link

I have the same issue, is it possible to merge with SheetJs?

@Zhenyi-Wang
Copy link
Author

Zhenyi-Wang commented Apr 22, 2021

I have the same issue, is it possible to merge with SheetJs?

Inspired from #128 and the answer posted by @Plotisateur in #87 (comment)_

Manually replace the function write_zip_type in xlsx.js with below code, only tested with xlsx

function write_zip_type(wb, opts) {
        var o = opts || {};
        style_builder = new StyleBuilder(opts);
        var z = write_zip(wb, o);
        var oopts = {};
        if (o.compression) oopts.compression = 'DEFLATE';
        switch (o.type) {
            case "base64":
                return z.generate({ type: "base64", compression: "DEFLATE" });
            case "binary":
                return z.generate({ type: "string", compression: "DEFLATE" });
            case "buffer":
                return z.generate({ type: "nodebuffer", compression: "DEFLATE" });
            case "file":
                return _fs.writeFileSync(o.file, z.generate({ type: "nodebuffer", compression: "DEFLATE" }));
            case "base64":
                oopts.type = "base64";
                break;
            case "binary":
                oopts.type = "string";
                break;
            case "buffer":
            case "file":
                oopts.type = "nodebuffer";
                break;
            default:
                throw new Error("Unrecognized type " + o.type);
        }

        var out = z.generate(oopts);
        if (o.type === "file") return _fs.writeFileSync(o.file, out);
        return out;
    }

and when you call XLSX.write(workbook, wopts), remember to add

compression: true

in the options, for example

var wopts = {
                bookType: 'xlsx',
                bookSST: false,
                type: 'binary',
                compression: true
            };

Worked for me :) Hope it can help in some way..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants