Export data to excel using html javascript

Asked

Viewed 73 times

0

I would like to know how to use a javascript library called Exceljs in the browser

I’m importing it into html like this

<html>

    <body>

        <script src="exceljs.js"></script>
        <script src="meu.js"></script>
    </body>
</html>

That library I downloaded from here, is in this list

I am using the following function in the file meu.js

      $(()=>{
           exportarParaExcel()
       })
const exportarParaExcel= async () => {
        const workbook = new Excel.Workbook();
        const worksheet = workbook.addWorksheet("My Sheet");

        worksheet.columns = [
        {header: 'Id', key: 'id', width: 10},
        {header: 'Name', key: 'name', width: 32}, 
        {header: 'D.O.B.', key: 'dob', width: 15,}
        ];

        worksheet.addRow({id: 1, name: 'John Doe', dob: new Date(1970, 1, 1)});
        worksheet.addRow({id: 2, name: 'Jane Doe', dob: new Date(1965, 1, 7)});

        // save under export.xlsx
        await workbook.xlsx.writeFile('export.xlsx');

        //load a copy of export.xlsx
        const newWorkbook = new Excel.Workbook();
        await newWorkbook.xlsx.readFile('export.xlsx');

        const newworksheet = newWorkbook.getWorksheet('My Sheet');
        newworksheet.columns = [
        {header: 'Id', key: 'id', width: 10},
        {header: 'Name', key: 'name', width: 32}, 
        {header: 'D.O.B.', key: 'dob', width: 15,}
        ];
        await newworksheet.addRow({id: 3, name: 'New Guy', dob: new Date(2000, 1, 1)});

        await newWorkbook.xlsx.writeFile('export2.xlsx');

        console.log("File is written")
    }

But in this case he makes the following mistake: Uncaught (in Promise) Referenceerror: Excel is not defined

When I declare using

var Excel = require('exceljs');

Makes the mistake:

Uncaught Referenceerror: require is not defined

How do I use this library in the browser?

  • I believe you can use https://requirejs.org/docs/download.html

  • Gave this here Uncaught Error: Module name "exceljs" has not been loaded yet for context: _. Use require([])

  • It is probably something wrong in load, missing files or the like, see if it solves so: <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.26.0/polyfill.js"></script>&#xA;<script src="exceljs.js"></script>

  • Same problem.

  • I don’t know if I’m making a mess, just do it to test: var Excel = require(['exceljs']);

  • Now it’s come to this Uncaught Error: Mismatched anonymous define() module: function () {

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.