electron js Tutorial

Electron JS How to create an Excel File

In this article, we will learn How to create an Excel file with Electron JS? In order to create an Excel file we need to download a package for Excel. We have multiple options in npm, but we use ExcelJS for this article. For our demo app we will first download our Electron JS Tutorial First Project. Download and Open Electron JS First Project inside Visual Studio Code.

FileDownload
First Project.zipLogin to Download

Next, we need to download and install ExcelJS package using npm. For this open terminal inside Visual Studio Code and execute "npm install exceljs". The command will download and install ExcelJS package. We have our required package. Next we will create a new Excel File. For this first create a new js file named "CreateExcel.js" with following code.

Code

function createNewExcelFile()
{
    var Excel = require('exceljs');
    // A new Excel Work Book
    var workbook = new Excel.Workbook();

    // Some information about the Excel Work Book.
    workbook.creator = 'Mayank Sanghvi';
    workbook.lastModifiedBy = '';
    workbook.created = new Date(2018, 6, 19);
    workbook.modified = new Date();
    workbook.lastPrinted = new Date(2016, 9, 27);

    // Create a sheet
    var sheet = workbook.addWorksheet('Sheet1');
    // A table header
    sheet.columns = [
        { header: 'Id', key: 'id' },
        { header: 'Course', key: 'course' },
        { header: 'URL.', key: 'url' }
    ]

    // Add rows in the above header
    sheet.addRow({id: 1, course: 'HTML', url:'https://vlemonn.com/tutorial/html' });
    sheet.addRow({id: 2, course: 'Java Script', url: 'https://vlemonn.com/tutorial/java-script'});
    sheet.addRow({id: 3, course: 'Electron JS', url: 'https://vlemonn.com/tutorial/electron-js'});
    sheet.addRow({id: 4, course: 'Node JS', url: 'https://vlemonn.com/tutorial/node-js'});

    // Save Excel on Hard Disk
    workbook.xlsx.writeFile("My First Excel.xlsx")
    .then(function() {
        // Success Message
        alert("File Saved");
    });
}

This file has a function to create a new Excel file with some dummy data. We need to call this function in our main index.html. We will call this function on window load. Following is the final code for index.html file.

Code

<!DOCTYPE html>
  <html>
    <head>
      <meta charset="UTF-8">
      <title>Hello vLemonn!</title>
    </head>
    <body>
      <h1>Hello vLemonn!</h1>
      <p>
      We are using node <script>document.write(process.versions.node)</script>,
      Chrome <script>document.write(process.versions.chrome)</script>,
      and Electron <script>document.write(process.versions.electron)</script>,
      on OS <script>document.write(process.platform)</script>
    </p>
    <p>
        Hello everyone, This is <a href="https://mayanksanghvi.com">Mayank Sanghvi</a> 
        from <a href="https://vlemonn.com">vLemonn.com</a> 
        . This is our Electron JS tutorial Series. You can find complete Elecron JS Tutorial <a href="https://vlemonn.com/tutorial/electon-js">here</a>
    </p>
    </body>
    <script src="CreateExcel.js"></script>
    <script >
        window.onload=function(e){
          createNewExcelFile();
        }
    </script>
  </html>

Finally, run app by executing "npm test" inside the terminal. Following is the output for this example.

...

About The Author

Hello, I am Mayank Sanghvi I am primarily a BI developer with experience in MSBI and Cognos BI Suit. Also having interest in various other technologies such as Android, Java, C#, ASP .NET and other. I enjoy learning new technologies and share my experience with others.

0 Comments
Leave A Comment

Please login to post your valuable comments.

add