light-excel-writer
v0.1.0
Published
Excel writer Open XML format
Downloads
14
Readme
Excel
import { Spreadsheet, Color } from "light-excel-writer";
function downloadURL(data: string, fileName: string) {
let a = document.createElement("a");
a.href = data;
a.download = fileName;
document.body.appendChild(a);
a.style.display = "none";
a.click();
a.remove();
}
{
let excel = new Spreadsheet();
let noBorder = excel.styleSheet.addBorder(null, null, null, null);
let borderf = excel.styleSheet.addBorder(
{ style: "medium", color: new Color(null, "FF00FF00") },
null,
null,
null
);
let numFmtDate = excel.styleSheet.addNumFmt("d/m/yyyy\\ hh:mm:ss");
let fill = excel.styleSheet.addFill("lightGrid");
let blackFont = excel.styleSheet.addFont(
10,
new Color(null, "FF000000"),
"Arial",
"minor"
);
let greenFont = excel.styleSheet.addFont(
10,
new Color(null, "FF00FF00"),
"Arial",
"minor"
);
let blueFont = excel.styleSheet.addFont(
10,
new Color(null, "FF0000FF"),
"Arial",
"minor"
);
excel.styleSheet.addCellXfs(blackFont, fill, noBorder);
let greenCell = excel.styleSheet.addCellXfs(greenFont, fill, borderf);
let blueCell = excel.styleSheet.addCellXfs(blueFont, fill, noBorder);
let greenDateCell = excel.styleSheet.addCellXfs(
greenFont,
fill,
noBorder,
numFmtDate
);
let sheet1 = excel.addWorksheet("Sheet1");
sheet1.cols.push({ min: 2, max: 2, width: 22 });
let row = sheet1.addRow();
sheet1.addNumber(row, 5.2, blueCell);
sheet1.addNumber(row, 7.2);
row = sheet1.addRow();
sheet1.addNumber(row, 15.2);
sheet1.addNumber(row, 17.3);
row = sheet1.addRow(4);
sheet1.addFormaula(row, "SUM(A1:A2)", "20.4", null, 1);
sheet1.addDate(row, new Date(), greenDateCell, 2);
sheet1.addSharedString(row, excel.addOrGetSharedString("Magni"), null, 3);
sheet1.addSharedString(
row,
excel.addOrGetSharedString("Aðalsteinn"),
greenCell,
4
);
sheet1.addSharedString(row, excel.addOrGetSharedString("Magni"), null, 5);
let sheet2 = excel.addWorksheet("Sheet2");
let anchor = document.getElementById("Test2") as HTMLAnchorElement;
excel.toData().then((data) => {
const blob = new Blob([data], {
type: Spreadsheet.MineType,
});
const url = window.URL.createObjectURL(blob);
anchor.href = url;
anchor.download = "test1.xlsx";
});
}