Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active February 6, 2023 02:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/1008531e0e9ae8c99b24a405b502f4c8 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/1008531e0e9ae8c99b24a405b502f4c8 to your computer and use it in GitHub Desktop.
Create Excel files in Node.js
var aspose = aspose || {};
aspose.cells = require("aspose.cells");
// create a new workbook
var workbook = new aspose.cells.Workbook(aspose.cells.FileFormatType.XLSX);
// add value in the cell
workbook.getWorksheets().get(0).getCells().get("A1").putValue("Hello World!");
// save as Excel XLSX file
workbook.save("Excel.xlsx");
console.log("done...");
var aspose = aspose || {};
aspose.cells = require("aspose.cells");
// create a new workbook
var workbook = aspose.cells.Workbook("Excel.xlsx");
// Obtaining the reference of the first worksheet
var worksheets = workbook.getWorksheets();
var sheet = worksheets.get(0);
// Adding some sample value to cells
var cells = sheet.getCells();
var cell = cells.get("A1");
cell.setValue(50);
cell = cells.get("A2");
cell.setValue(100);
cell = cells.get("A3");
cell.setValue(150);
cell = cells.get("B1");
cell.setValue(4);
cell = cells.get("B2");
cell.setValue(20);
cell = cells.get("B3");
cell.setValue(50);
// get charts in worksheet
var charts = sheet.getCharts();
// Adding a chart to the worksheet
var chartIndex = charts.add(aspose.cells.ChartType.PYRAMID, 5, 0, 15, 5);
var chart = charts.get(chartIndex);
// Adding NSeries (chart data source) to the chart ranging from "A1"
// cell to "B3"
var serieses = chart.getNSeries();
serieses.add("A1:B3", true);
// Write the Excel file
workbook.save("Excel_with_Chart.xlsx");
var aspose = aspose || {};
aspose.cells = require("aspose.cells");
// create a new workbook
var workbook = aspose.cells.Workbook("Excel.xlsx");
// Get the reference of "A1" cell from the cells of a worksheet
var cell = workbook.getWorksheets().get(0).getCells().get("A1");
// Set the "Hello World!" value into the "A1" cell
cell.setValue("updated cell value.");
// Write the Excel file
workbook.save("Excel.xls", aspose.cells.FileFormatType.EXCEL_97_TO_2003);
// or
//workbook.save("Excel.xlsx");
var aspose = aspose || {};
aspose.cells = require("aspose.cells");
// create a new workbook
var workbook = aspose.cells.Workbook("Excel.xlsx");
// Get the reference of "A1" cell from the cells of a worksheet
var worksheet = workbook.getWorksheets().get(0);
//Insert a string value to a cell
worksheet.getCells().get("C2").setValue("Image");
//Set the 4th row height
worksheet.getCells().setRowHeight(3, 150);
//Set the C column width
worksheet.getCells().setColumnWidth(3,50);
//Add a picture to the D4 cell
var index = worksheet.getPictures().add(3, 3, "aspose-logo.png");
//Get the picture object
var pic = worksheet.getPictures().get(index);
//Set background color of a cell
var cell= worksheet.getCells().get("D4");
var style = cell.getStyle();
style.setBackgroundColor(aspose.cells.Color.getBlack());
style.setPattern(aspose.cells.BackgroundType.VERTICAL_STRIPE);
cell.setStyle(style);
// Write the Excel file
workbook.save("Excel_with_Image.xlsx");
var aspose = aspose || {};
aspose.cells = require("aspose.cells");
// create a new workbook
var workbook = aspose.cells.Workbook("Excel.xlsx");
// obtaining the reference of the newly added worksheet
var sheetIndex = workbook.getWorksheets().add();
var sheet = workbook.getWorksheets().get(sheetIndex);
var cells = sheet.getCells();
// setting the value to the cells
var cell = cells.get("A1");
cell.setValue("Sport");
cell = cells.get("B1");
cell.setValue("Quarter");
cell = cells.get("C1");
cell.setValue("Sales");
cell = cells.get("A2");
cell.setValue("Golf");
cell = cells.get("A3");
cell.setValue("Golf");
cell = cells.get("A4");
cell.setValue("Tennis");
cell = cells.get("A5");
cell.setValue("Tennis");
cell = cells.get("A6");
cell.setValue("Tennis");
cell = cells.get("A7");
cell.setValue("Tennis");
cell = cells.get("A8");
cell.setValue("Golf");
cell = cells.get("B2");
cell.setValue("Qtr3");
cell = cells.get("B3");
cell.setValue("Qtr4");
cell = cells.get("B4");
cell.setValue("Qtr3");
cell = cells.get("B5");
cell.setValue("Qtr4");
cell = cells.get("B6");
cell.setValue("Qtr3");
cell = cells.get("B7");
cell.setValue("Qtr4");
cell = cells.get("B8");
cell.setValue("Qtr3");
cell = cells.get("C2");
cell.setValue(1500);
cell = cells.get("C3");
cell.setValue(2000);
cell = cells.get("C4");
cell.setValue(600);
cell = cells.get("C5");
cell.setValue(1500);
cell = cells.get("C6");
cell.setValue(4070);
cell = cells.get("C7");
cell.setValue(5000);
cell = cells.get("C8");
cell.setValue(6430);
var pivotTables = sheet.getPivotTables();
// adding a PivotTable to the worksheet
var index = pivotTables.add("=A1:C8", "E3", "PivotTable2");
// accessing the instance of the newly added PivotTable
var pivotTable = pivotTables.get(index);
// unshowing grand totals for rows.
pivotTable.setRowGrand(false);
// dragging the first field to the row area.
pivotTable.addFieldToArea(aspose.cells.PivotFieldType.ROW, 0);
// dragging the second field to the column area.
pivotTable.addFieldToArea(aspose.cells.PivotFieldType.COLUMN, 1);
// dragging the third field to the data area.
pivotTable.addFieldToArea(aspose.cells.PivotFieldType.DATA, 2);
// write the Excel file
workbook.save("Excel_with_PivotTable.xlsx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment