locked
Download and Save as Xlsx and PDF in Blazor RRS feed

  • Question

  • User2041008840 posted

    Hello, 
    How to I download and save into xlsx and pdf from html table. 
    I fetchdata from db and show it into html table. So I want it to download into excel and pdf. 

    Any solution ?

    Friday, August 21, 2020 4:12 AM

Answers

  • User-821857111 posted

    Add a standard hyperlink to your page that points to a Web API controller method. The method should return a FileResult that delivers the file. You will need to create that on the server. You should use an Office Open XML library for generating the Excel file, such as EPPlus (https://www.nuget.org/packages/EPPlus/). Note that version 5 uses a less permissive licence. There are a number of options for generating PDF files in .NET, many of them commercial. You should do some research to find one that suits your needs. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 21, 2020 5:49 AM
  • User-474980206 posted

    to "download" a file form a SPA, you usually create an <a>, set the href to a dataurl then call click.

    function FileSaveAs(filename, fileContent) {
        var link = document.createElement('a');
        link.download = filename;
        link.href = "data:text/plain;charset=utf-8," + encodeURIComponent(fileContent);
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    }
    

    you can call with blazor interop.

    to convert html to pdf, use the javascript library jsPdf

        https://github.com/MrRio/jsPDF

    it has the save feature (it uses document.location rather than an anchor)

    to create excel worksheets use the javascript SheetJS

       https://github.com/SheetJS/sheetjs

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 21, 2020 9:59 PM

All replies

  • User-821857111 posted

    Add a standard hyperlink to your page that points to a Web API controller method. The method should return a FileResult that delivers the file. You will need to create that on the server. You should use an Office Open XML library for generating the Excel file, such as EPPlus (https://www.nuget.org/packages/EPPlus/). Note that version 5 uses a less permissive licence. There are a number of options for generating PDF files in .NET, many of them commercial. You should do some research to find one that suits your needs. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 21, 2020 5:49 AM
  • User-474980206 posted

    to "download" a file form a SPA, you usually create an <a>, set the href to a dataurl then call click.

    function FileSaveAs(filename, fileContent) {
        var link = document.createElement('a');
        link.download = filename;
        link.href = "data:text/plain;charset=utf-8," + encodeURIComponent(fileContent);
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    }
    

    you can call with blazor interop.

    to convert html to pdf, use the javascript library jsPdf

        https://github.com/MrRio/jsPDF

    it has the save feature (it uses document.location rather than an anchor)

    to create excel worksheets use the javascript SheetJS

       https://github.com/SheetJS/sheetjs

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 21, 2020 9:59 PM