Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

How to download on client? #354

Closed
mstrop opened this issue Jul 5, 2017 · 22 comments
Closed

How to download on client? #354

mstrop opened this issue Jul 5, 2017 · 22 comments

Comments

@mstrop
Copy link

mstrop commented Jul 5, 2017

Is there a way, how to create a XLSX on a client and download it (send it as download file) without saving it or interacting with a server?

@kirtiesl
Copy link

kirtiesl commented Jul 6, 2017

Hello, looking for the same feature. Please update

@gtskaushik
Copy link

gtskaushik commented Jul 12, 2017

Yes it is possible.

  1. Create the workbook and populate it with data
  res.attachment("test.xlsx")
  workbook.xlsx.write(res)
                .then(function() {
                    res.end()
            });

Here res is the response to the client

@pleger
Copy link

pleger commented Aug 21, 2017

Hi, everyone,
I am a newest in this topic. Thereby, I couldn't understand the following code in the example:
res.attachment("test.xlsx");
What is res on the client? How can I create res?

Thanks, Paul

@Amerzel
Copy link

Amerzel commented Aug 27, 2017

I think the previous commenter might have been confused about what was being asked. res indicates to me that they meant a node response sent back to the client. The original poster was asking for a way to create and download a file without using a server. I'd like this functionality as well and haven't been able to get it to work yet.

@rojasjandro89
Copy link

Same here, I'm using angular, I have all I need in the client, it would be great if I could just download the file right from the client. So far all I've found has node's "res"

@pleger
Copy link

pleger commented Aug 29, 2017 via email

@rojasjandro89
Copy link

It works for me too. Thanks a lot!

@Mafi78
Copy link

Mafi78 commented Aug 29, 2017

Hi,
I've attached one working zip file in this incident: #322 perhaby cou can try that ?
Best regards
Manfred

@Apollo081594
Copy link

@pleger do you example on how to download excel? i already used res.download. but still cant download the file. But it can do write excel

@pleger
Copy link

pleger commented Jan 3, 2018

I hope the following code can help you!

extract from http://pleger.cl/gp

$(document).ready(function () {  
  | var workbook = new ExcelJS.Workbook();
  |  
  | workbook.creator = 'Paul Leger';
  | workbook.lastModifiedBy = 'Paul Leger';
  | workbook.created = new Date();
  | workbook.modified = new Date();
  | workbook.lastPrinted = new Date();
  |  
  | var worksheet = workbook.addWorksheet("Publications");
  | worksheet.views = [
  | {state: 'frozen', xSplit: 0, ySplit: 1}
  | ];
  |  
  | worksheet.autoFilter = {
  | from: 'A1',
  | to: 'M1'
  | };
  |  
  | worksheet.columns = [
  | { header: 'Index', key: 'Index', width: 15 },
  | { header: 'Title', key: 'title', width: 25, style: {alignment: {wrapText: true} } },
  | { header: 'Authors', key: 'authors', width: 20, style: {alignment: {wrapText: true} } },
  | { header: 'Journal/Conference', key: 'jc', width: 25, style: {alignment: {wrapText: true} } },
  | { header: 'Type', key: 'type', width: 12, style: {alignment: {wrapText: true} } },
  | { header: 'Year', key: 'year', width: 12, style: {numFmt: "0000"}},
  | { header: 'Month', key: 'month', width: 12},
  | { header: 'volume', key: 'volume', width: 12},
  | { header: 'number', key: 'number', width: 12},
  | { header: 'Pages', key: 'pages', width: 12},
  | { header: 'Location', key: 'location', width: 20, style: {alignment: {wrapText: true} } },
  | { header: 'doi', key: 'doi', width: 22, style: {alignment: {wrapText: true} } },
  | { header: 'affiliation', key: 'affiliation', width: 20, style: {alignment: {wrapText: true} } }
  | ];
  |  
  | var firstRow = worksheet.getRow(1);
  | firstRow.font = { name: 'New Times Roman', family: 4, size: 10, bold: true, color: {argb:'80EF1C1C'} };
  | firstRow.alignment = { vertical: 'middle', horizontal: 'center'};
  | firstRow.height = 20;
  |  
  |  
  | csv.shift();
  | worksheet.addRows(csv);
  |  
  | var buff = workbook.xlsx.writeBuffer().then(function (data) {
  | var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
  | saveAs(blob, "publications.xlsx");
  | });

@Apollo081594
Copy link

Thanks you so much!

@LiHaoGit
Copy link

It works for me

koa2

export async function download(ctx: koa.Context) {
    const workbook = new Excel.Workbook()
    const worksheet = workbook.addWorksheet("report")
    worksheet.columns = [
      {header: "Date", key: "date", width: 20}]
    ctx.response.attachment("report.xlsx")
    ctx.status = 200
    await workbook.xlsx.write(ctx.res)
    ctx.res.end()
}

@Messilimeng
Copy link

@LiHaoGit 好啊 我正在用这个插件 可是一直下载不了 能不能加个qq 咨询一下

@shikhaBasra
Copy link

xlsx.writeBuffer()
this code gives me error that function not exist

@dlinx
Copy link

dlinx commented Nov 29, 2018

To download on browser.

workbook.xlsx.writeBuffer().then(function (data: Blob) {
     const blob = new Blob([data],
       { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
     const url = window.URL.createObjectURL(blob);
     const anchor = document.createElement('a');
     anchor.href = url;
     anchor.download = 'download.xls';
     anchor.click();
     window.URL.revokeObjectURL(url);
   });

@JarvisQJ
Copy link

JarvisQJ commented Dec 6, 2018

It works for me

koa2

export async function download(ctx: koa.Context) {
    const workbook = new Excel.Workbook()
    const worksheet = workbook.addWorksheet("report")
    worksheet.columns = [
      {header: "Date", key: "date", width: 20}]
    ctx.response.attachment("report.xlsx")
    ctx.status = 200
    await workbook.xlsx.write(ctx.res)
    ctx.res.end()
}

@LiHaoGit hi,how to receive and turn the data to excel file when you send data like this?

@Siemienik
Copy link
Member

#269 #37

@praveendev404
Copy link

praveendev404 commented May 6, 2019

res.status(200);
res.setHeader('Content-Type', 'text/xlsx');
res.setHeader(
'Content-Disposition',
'attachment; filename=test.xlsx'
);
workbook.xlsx.write(res)
.then(function () {
res.end()
});

@chris-canipe
Copy link

To download on browser.

workbook.xlsx.writeBuffer().then(function (data: Blob) {
     const blob = new Blob([data],
       { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
     const url = window.URL.createObjectURL(blob);
     const anchor = document.createElement('a');
     anchor.href = url;
     anchor.download = 'download.xls';
     anchor.click();
     window.URL.revokeObjectURL(url);
   });

I had to change anchor.click(); to anchor.dispatchEvent(new MouseEvent('click')); to get this working in Firefox. Thanks!

@mateo2181
Copy link

Someone could do this sending the buffer from the backend (Node) ?

@MeghaShrivastava
Copy link

MeghaShrivastava commented Apr 21, 2020

I hope the following code can help you!

extract from http://pleger.cl/gp

$(document).ready(function () {  
  | var workbook = new ExcelJS.Workbook();
  |  
  | workbook.creator = 'Paul Leger';
  | workbook.lastModifiedBy = 'Paul Leger';
  | workbook.created = new Date();
  | workbook.modified = new Date();
  | workbook.lastPrinted = new Date();
  |  
  | var worksheet = workbook.addWorksheet("Publications");
  | worksheet.views = [
  | {state: 'frozen', xSplit: 0, ySplit: 1}
  | ];
  |  
  | worksheet.autoFilter = {
  | from: 'A1',
  | to: 'M1'
  | };
  |  
  | worksheet.columns = [
  | { header: 'Index', key: 'Index', width: 15 },
  | { header: 'Title', key: 'title', width: 25, style: {alignment: {wrapText: true} } },
  | { header: 'Authors', key: 'authors', width: 20, style: {alignment: {wrapText: true} } },
  | { header: 'Journal/Conference', key: 'jc', width: 25, style: {alignment: {wrapText: true} } },
  | { header: 'Type', key: 'type', width: 12, style: {alignment: {wrapText: true} } },
  | { header: 'Year', key: 'year', width: 12, style: {numFmt: "0000"}},
  | { header: 'Month', key: 'month', width: 12},
  | { header: 'volume', key: 'volume', width: 12},
  | { header: 'number', key: 'number', width: 12},
  | { header: 'Pages', key: 'pages', width: 12},
  | { header: 'Location', key: 'location', width: 20, style: {alignment: {wrapText: true} } },
  | { header: 'doi', key: 'doi', width: 22, style: {alignment: {wrapText: true} } },
  | { header: 'affiliation', key: 'affiliation', width: 20, style: {alignment: {wrapText: true} } }
  | ];
  |  
  | var firstRow = worksheet.getRow(1);
  | firstRow.font = { name: 'New Times Roman', family: 4, size: 10, bold: true, color: {argb:'80EF1C1C'} };
  | firstRow.alignment = { vertical: 'middle', horizontal: 'center'};
  | firstRow.height = 20;
  |  
  |  
  | csv.shift();
  | worksheet.addRows(csv);
  |  
  | var buff = workbook.xlsx.writeBuffer().then(function (data) {
  | var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
  | saveAs(blob, "publications.xlsx");
  | });

Hello,
My data is coming from API but after downloading the excel the data is not having proper sequence like the data which is in column 1 of my report is now coming in column 4 and so on.

I'm using calling data list and header like this:
generateExcel(){
this._http.generateExcel(this.headerInfo, this.spendclassificationlist);
}

Could you please suggest me how can I map my data with the proper header.

@huy-lv
Copy link

huy-lv commented Oct 30, 2023

It works for me

koa2

export async function download(ctx: koa.Context) {
    const workbook = new Excel.Workbook()
    const worksheet = workbook.addWorksheet("report")
    worksheet.columns = [
      {header: "Date", key: "date", width: 20}]
    ctx.response.attachment("report.xlsx")
    ctx.status = 200
    await workbook.xlsx.write(ctx.res)
    ctx.res.end()
}

What about client side implementation?

@exceljs exceljs locked and limited conversation to collaborators Oct 31, 2023
@Siemienik Siemienik converted this issue into discussion #2576 Oct 31, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests