Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

convert json to excel in javascript

//HTML
<button onclick="exportToCsv()">export to CSV</button>

// Javascript
var Results = [
  ["Col1", "Col2", "Col3", "Col4"],
  ["Data", 50, 100, 500],
  ["Data", -100, 20, 100],
];

exportToCsv = function() {
  var CsvString = "";
  Results.forEach(function(RowItem, RowIndex) {
    RowItem.forEach(function(ColItem, ColIndex) {
      CsvString += ColItem + ',';
    });
    CsvString += "
";
  });
  CsvString = "data:application/csv," + encodeURIComponent(CsvString);
 var x = document.createElement("A");
 x.setAttribute("href", CsvString );
 x.setAttribute("download","somedata.csv");
 document.body.appendChild(x);
 x.click();
}
Comment

javascript json to excel

import * as ExcelJS from 'exceljs';

function bufferToFile(
    buffer,
    filename,
    type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  ) {
    let blob = new Blob([buffer], { type });
    const anchor = document.createElement('a');
    const url = URL.createObjectURL(blob);
    anchor.href = url;
    anchor.download = filename;
    document.body.appendChild(anchor);
    anchor.click();
    document.body.removeChild(anchor);
    URL.revokeObjectURL(url);
  }

async function exportToExcel($event) {
    const workbook = new ExcelJS.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: 10, outlineLevel: 1 },
    ];
    worksheet.getColumn(2).alignment = {
      vertical: 'middle',
      horizontal: 'center',
      wrapText: true,
    };
    worksheet.addRow([3, 'Sam
Sung', new Date()]);
    worksheet.addRow({
      id: 1,
      name: 'John Doe',
      DOB: new Date(1994, 10, 10),
    });
    const filename = 'Export.xlsx';
    const buffer = await workbook.xlsx.writeBuffer();
    this.bufferToFile(buffer, filename);
  }
Comment

PREVIOUS NEXT
Code Example
Javascript :: add object in array state react 
Javascript :: threejs perspectivecamera 
Javascript :: ordenar numeros array javascript 
Javascript :: how to map through an object javascript 
Javascript :: afficher une variable dans la console javascript 
Javascript :: firebase get last document 
Javascript :: convert timestamp to date js 
Javascript :: address format 
Javascript :: add two floating point numbers jquery 
Javascript :: next js get gurrent page params 
Javascript :: mongodb mapreduce 
Javascript :: deep copy javascript 
Javascript :: javascript fetch api 
Javascript :: javascript check negative number 
Javascript :: how i do button when click open a new tab in react 
Javascript :: how to clear array in javascript 
Javascript :: sequelize findall 
Javascript :: javascript isempty 
Javascript :: filter function using recursion 
Javascript :: how to update state.item[1] in state using setState? React 
Javascript :: how to check provided value is in array in javascript 
Javascript :: jquery xpath 
Javascript :: javascript loop last index 
Javascript :: datepicker toltip 
Javascript :: how to get form all filed with properties in jquery 
Javascript :: react extends component Increment data 
Javascript :: javascript foreach table 
Javascript :: how to transform object in string with scaped 
Javascript :: react scripts for browser 
Javascript :: localstorage nextjs 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =