翻譯|使用教程|編輯:李顯亮|2020-08-10 09:47:20.757|閱讀 530 次
概述:電子表格使以行,列和工作表的形式存儲(chǔ)大量數(shù)據(jù)成為可能。在本文中,將展示如何以編程方式實(shí)現(xiàn)Excel自動(dòng)化并在Node.js應(yīng)用程序中創(chuàng)建Excel文件。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷售中 >>
Aspose.Cells for Node.js via Java是功能強(qiáng)大的電子表格處理API,可讓您在Node.js應(yīng)用程序內(nèi)創(chuàng)建,編輯或轉(zhuǎn)換Excel文件。該API易于使用的方法使您可以在幾行代碼中無(wú)縫執(zhí)行Excel自動(dòng)化功能。
電子表格使以行,列和工作表的形式存儲(chǔ)大量數(shù)據(jù)成為可能。 此外,您可以對(duì)數(shù)據(jù)執(zhí)行各種操作,例如排序,應(yīng)用數(shù)學(xué)公式,生成圖表和圖形等等。 在當(dāng)今的數(shù)字世界時(shí)代,自動(dòng)化已取代了手工工作。 因此,Excel自動(dòng)化也已成為一種無(wú)需任何第三方軟件即可生成電子表格文檔的工具。
在本文中,將展示如何以編程方式實(shí)現(xiàn)Excel自動(dòng)化并在Node.js應(yīng)用程序中創(chuàng)建Excel文件。
以下是通過(guò)Java使用Aspose.Cells for Node.js創(chuàng)建簡(jiǎn)單Excel文件的步驟:
以下代碼示例顯示了如何在Node.js中創(chuàng)建Excel文件。
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...");
可以加載現(xiàn)有的Excel文件以更新其內(nèi)容。唯一要做的就是在Workbook構(gòu)造函數(shù)中提供Excel文件的路徑。下面的代碼示例演示如何更新Node.js中現(xiàn)有Excel文件的內(nèi)容。
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");
以下是在Node.js的Excel工作表中插入圖像的步驟。
下面的代碼示例演示如何在Node.js的Excel工作表中插入圖像。
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");
圖表是用于直觀表示數(shù)據(jù)的Excel文件的重要組成部分。通過(guò)Java for Node.js的Aspose.Cells,您可以繪制和處理各種圖表,包括朝陽(yáng)圖,樹(shù)圖,直方圖,金字塔等。以下是在Excel工作表中繪制圖表的簡(jiǎn)單步驟。
以下代碼示例顯示如何在Node.js中的Excel中繪制圖表。
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");
Excel文件還允許您基于一系列單元格創(chuàng)建數(shù)據(jù)透視表,以匯總大量數(shù)據(jù)。以下是在Excel工作表中創(chuàng)建數(shù)據(jù)透視表的步驟。
下面的代碼示例演示如何在Node.js中的Excel中創(chuàng)建數(shù)據(jù)透視表。
var aspose = aspose || {}; aspose.cells = require("aspose.cells"); var license = aspose.cells.License(); license.setLicense("D:\\Licenses\\Aspose.Total.Product.Family.lic"); // 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");
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn