SheetJS - Basic Example

by marbx

HTML

<script src="https://unpkg.com/xlsx/dist/xlsx.full.min.js"></script>

JavaScript

const data = [
  ["id", "name", "value"],
  [1, "sheetjs", 1234],
  [2, "js-xlsx", 5678]
]

const worksheet = XLSX.utils.aoa_to_sheet(data)
const workbook = XLSX.utils.book_new()
/*set col widths. index of array is index of columns*/
var wscols = [{
    wch: 16
  }, // "characters"
  {
    wpx: 300
  } // "pixels"
];
//	,
//if want to hide columns	{hidden: true} // hide column

worksheet["!cols"] = wscols;
console.log(worksheet["C2"])
//cell C2
worksheet["C2"].s = {
  fill: {
    fgColor: {
      rgb: 'FFA3F4B1'
    }
  },
  font: {

    sz: 12,
    bold: true
  },
  border: {
    bottom: {
      style: 'thin',
      color: 'FF000000'
    }
  }
}
for (const key in worksheet) {
  console.log(key)
}
XLSX.utils.book_append_sheet(workbook, worksheet, "SheetJS - Basic")

const sheet = XLSX.writeFile(workbook, "sheetjs.xlsx")