
Een draaitabel maken in Excel met Node.js
- [Excel JavaScript API-installatie][3]
- [Een draaitabel maken in Excel met Node.js][4]
- [Een draaigrafiek maken in Excel programmatisch][5]
Excel JavaScript API-installatie
Om deze krachtige bibliotheek te installeren [download][6] je het API-pakket of installeer je het door de volgende opdrachten uit te voeren:
npm install java
npm install aspose.cells
Een draaitabel maken in Excel met Node.js
In deze sectie zullen we de volgende stappen en het codefragment schrijven dat programmatisch een draaitabel in Excel maakt. U kunt de volgende stappen en het codefragment volgen om Excel naar DataTable in C# te converteren:
- Maak een instantie van de klasse [Workbook][7].
- Haal de referentie van het eerste werkblad op door de methode [get(index)][8] aan te roepen.
- Stel de naam van het werkblad in door de methode [setName][9] aan te roepen.
- Roep de methode [getCells][10] aan om de verzameling Cells op te halen.
- [Get][11] het celelement op de opgegeven celnaam.
- Roep deze methode [setValue][12] aan om de waarde van het bereik in te stellen.
- Haal het Cell-object binnen het bereik door de methode [get][13] aan te roepen.
- Een nieuw blad toevoegen door de methode [add][14] aan te roepen.
- [haal][15] het werkbladelement op de opgegeven index.
- Geef het blad een naam door de methode [setName][9] aan te roepen.
- Voeg een draaitabel toe aan het werkblad door de methode [add][16] aan te roepen.
- Geef de eindtotalen weer door de waarde van de methode [setRowGrand][17] in te stellen.
- Stel de waarde in van de methode [setColumnGrand][18] die aangeeft of het draaitabelrapport eindtotalen voor kolommen weergeeft.
- Stel nu de waarde in van de methode [setAutoFormat][19] die aangeeft of het draaitabelrapport automatisch wordt opgemaakt.
- Stel het type Draaitabel [autoformat][20] in door de methode setAutoFormatType aan te roepen.
- Roep de methode [addFieldToArea][21] aan om het eerste, tweede, derde, vierde en vijfde veld naar het rijgebied te slepen.
- Stel de getalnotatie van het eerste gegevensveld in door de methode [getDataFields][22] aan te roepen.
- Sla het Excel-bestand op door de methode [save][23] aan te roepen.
Kopieer en plak de volgende code in uw hoofdbestand:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
var aspose = aspose || {}; aspose.cells = require("aspose.cells"); // Instantiate an instant of the Workbook class var workbook = new aspose.cells.Workbook(); // Get the reference of the first worksheet by calling the get(index) method var sheet = workbook.getWorksheets().get(0); // set the name of the worksheet by invoking the setName method. sheet.setName("Data"); // Call the getCells method to get the Cells collection. var cells = sheet.getCells(); // Get the Cell element at the specified cell name. var cell = cells.get("A1"); // Call this method to set the value of the range. cell.setValue("Employee"); // Get Cell object in the range by calling the get method . cell = cells.get("B1"); cell.setValue("Quarter"); cell = cells.get("C1"); cell.setValue("Product"); cell = cells.get("D1"); cell.setValue("Continent"); cell = cells.get("E1"); cell.setValue("Country"); cell = cells.get("F1"); cell.setValue("Sale"); cell = cells.get("A2"); cell.setValue("David"); cell = cells.get("A3"); cell.setValue("David"); cell = cells.get("A4"); cell.setValue("David"); cell = cells.get("A5"); cell.setValue("David"); cell = cells.get("A6"); cell.setValue("James"); cell = cells.get("A7"); cell.setValue("James"); cell = cells.get("A8"); cell.setValue("James"); cell = cells.get("A9"); cell.setValue("James"); cell = cells.get("A10"); cell.setValue("James"); cell = cells.get("A11"); cell.setValue("Miya"); cell = cells.get("A12"); cell.setValue("Miya"); cell = cells.get("A13"); cell.setValue("Miya"); cell = cells.get("A14"); cell.setValue("Miya"); cell = cells.get("A15"); cell.setValue("Miya"); cell = cells.get("A16"); cell.setValue("Miya"); cell = cells.get("A17"); cell.setValue("Miya"); cell = cells.get("A18"); cell.setValue("Elvis"); cell = cells.get("A19"); cell.setValue("Elvis"); cell = cells.get("A20"); cell.setValue("Elvis"); cell = cells.get("A21"); cell.setValue("Elvis"); cell = cells.get("A22"); cell.setValue("Elvis"); cell = cells.get("A23"); cell.setValue("Elvis"); cell = cells.get("A24"); cell.setValue("Elvis"); cell = cells.get("A25"); cell.setValue("Jean"); cell = cells.get("A26"); cell.setValue("Jean"); cell = cells.get("A27"); cell.setValue("Jean"); cell = cells.get("A28"); cell.setValue("Ada"); cell = cells.get("A29"); cell.setValue("Ada"); cell = cells.get("A30"); cell.setValue("Ada"); cell = cells.get("B2"); cell.setValue("1"); cell = cells.get("B3"); cell.setValue("2"); cell = cells.get("B4"); cell.setValue("3"); cell = cells.get("B5"); cell.setValue("4"); cell = cells.get("B6"); cell.setValue("1"); cell = cells.get("B7"); cell.setValue("2"); cell = cells.get("B8"); cell.setValue("3"); cell = cells.get("B9"); cell.setValue("4"); cell = cells.get("B10"); cell.setValue("4"); cell = cells.get("B11"); cell.setValue("1"); cell = cells.get("B12"); cell.setValue("1"); cell = cells.get("B13"); cell.setValue("2"); cell = cells.get("B14"); cell.setValue("2"); cell = cells.get("B15"); cell.setValue("3"); cell = cells.get("B16"); cell.setValue("4"); cell = cells.get("B17"); cell.setValue("4"); cell = cells.get("B18"); cell.setValue("1"); cell = cells.get("B19"); cell.setValue("1"); cell = cells.get("B20"); cell.setValue("2"); cell = cells.get("B21"); cell.setValue("3"); cell = cells.get("B22"); cell.setValue("3"); cell = cells.get("B23"); cell.setValue("4"); cell = cells.get("B24"); cell.setValue("4"); cell = cells.get("B25"); cell.setValue("1"); cell = cells.get("B26"); cell.setValue("2"); cell = cells.get("B27"); cell.setValue("3"); cell = cells.get("B28"); cell.setValue("1"); cell = cells.get("B29"); cell.setValue("2"); cell = cells.get("B30"); cell.setValue("3"); cell = cells.get("C2"); cell.setValue("Maxilaku"); cell = cells.get("C3"); cell.setValue("Maxilaku"); cell = cells.get("C4"); cell.setValue("Chai"); cell = cells.get("C5"); cell.setValue("Maxilaku"); cell = cells.get("C6"); cell.setValue("Chang"); cell = cells.get("C7"); cell.setValue("Chang"); cell = cells.get("C8"); cell.setValue("Chang"); cell = cells.get("C9"); cell.setValue("Chang"); cell = cells.get("C10"); cell.setValue("Chang"); cell = cells.get("C11"); cell.setValue("Geitost"); cell = cells.get("C12"); cell.setValue("Chai"); cell = cells.get("C13"); cell.setValue("Geitost"); cell = cells.get("C14"); cell.setValue("Geitost"); cell = cells.get("C15"); cell.setValue("Maxilaku"); cell = cells.get("C16"); cell.setValue("Geitost"); cell = cells.get("C17"); cell.setValue("Geitost"); cell = cells.get("C18"); cell.setValue("Ikuru"); cell = cells.get("C19"); cell.setValue("Ikuru"); cell = cells.get("C20"); cell.setValue("Ikuru"); cell = cells.get("C21"); cell.setValue("Ikuru"); cell = cells.get("C22"); cell.setValue("Ipoh Coffee"); cell = cells.get("C23"); cell.setValue("Ipoh Coffee"); cell = cells.get("C24"); cell.setValue("Ipoh Coffee"); cell = cells.get("C25"); cell.setValue("Chocolade"); cell = cells.get("C26"); cell.setValue("Chocolade"); cell = cells.get("C27"); cell.setValue("Chocolade"); cell = cells.get("C28"); cell.setValue("Chocolade"); cell = cells.get("C29"); cell.setValue("Chocolade"); cell = cells.get("C30"); cell.setValue("Chocolade"); cell = cells.get("D2"); cell.setValue("Asia"); cell = cells.get("D3"); cell.setValue("Asia"); cell = cells.get("D4"); cell.setValue("Asia"); cell = cells.get("D5"); cell.setValue("Asia"); cell = cells.get("D6"); cell.setValue("Europe"); cell = cells.get("D7"); cell.setValue("Europe"); cell = cells.get("D8"); cell.setValue("Europe"); cell = cells.get("D9"); cell.setValue("Europe"); cell = cells.get("D10"); cell.setValue("Europe"); cell = cells.get("D11"); cell.setValue("America"); cell = cells.get("D12"); cell.setValue("America"); cell = cells.get("D13"); cell.setValue("America"); cell = cells.get("D14"); cell.setValue("America"); cell = cells.get("D15"); cell.setValue("America"); cell = cells.get("D16"); cell.setValue("America"); cell = cells.get("D17"); cell.setValue("America"); cell = cells.get("D18"); cell.setValue("Europe"); cell = cells.get("D19"); cell.setValue("Europe"); cell = cells.get("D20"); cell.setValue("Europe"); cell = cells.get("D21"); cell.setValue("Oceania"); cell = cells.get("D22"); cell.setValue("Oceania"); cell = cells.get("D23"); cell.setValue("Oceania"); cell = cells.get("D24"); cell.setValue("Oceania"); cell = cells.get("D25"); cell.setValue("Africa"); cell = cells.get("D26"); cell.setValue("Africa"); cell = cells.get("D27"); cell.setValue("Africa"); cell = cells.get("D28"); cell.setValue("Africa"); cell = cells.get("D29"); cell.setValue("Africa"); cell = cells.get("D30"); cell.setValue("Africa"); cell = cells.get("E2"); cell.setValue("China"); cell = cells.get("E3"); cell.setValue("India"); cell = cells.get("E4"); cell.setValue("Korea"); cell = cells.get("E5"); cell.setValue("India"); cell = cells.get("E6"); cell.setValue("France"); cell = cells.get("E7"); cell.setValue("France"); cell = cells.get("E8"); cell.setValue("Germany"); cell = cells.get("E9"); cell.setValue("Italy"); cell = cells.get("E10"); cell.setValue("France"); cell = cells.get("E11"); cell.setValue("U.S."); cell = cells.get("E12"); cell.setValue("U.S."); cell = cells.get("E13"); cell.setValue("Brazil"); cell = cells.get("E14"); cell.setValue("U.S."); cell = cells.get("E15"); cell.setValue("U.S."); cell = cells.get("E16"); cell.setValue("Canada"); cell = cells.get("E17"); cell.setValue("U.S."); cell = cells.get("E18"); cell.setValue("Italy"); cell = cells.get("E19"); cell.setValue("France"); cell = cells.get("E20"); cell.setValue("Italy"); cell = cells.get("E21"); cell.setValue("New Zealand"); cell = cells.get("E22"); cell.setValue("Australia"); cell = cells.get("E23"); cell.setValue("Australia"); cell = cells.get("E24"); cell.setValue("New Zealand"); cell = cells.get("E25"); cell.setValue("S.Africa"); cell = cells.get("E26"); cell.setValue("S.Africa"); cell = cells.get("E27"); cell.setValue("S.Africa"); cell = cells.get("E28"); cell.setValue("Egypt"); cell = cells.get("E29"); cell.setValue("Egypt"); cell = cells.get("E30"); cell.setValue("Egypt"); cell = cells.get("F2"); cell.setValue(2000); cell = cells.get("F3"); cell.setValue(500); cell = cells.get("F4"); cell.setValue(1200); cell = cells.get("F5"); cell.setValue(1500); cell = cells.get("F6"); cell.setValue(500); cell = cells.get("F7"); cell.setValue(1500); cell = cells.get("F8"); cell.setValue(800); cell = cells.get("F9"); cell.setValue(900); cell = cells.get("F10"); cell.setValue(500); cell = cells.get("F11"); cell.setValue(1600); cell = cells.get("F12"); cell.setValue(600); cell = cells.get("F13"); cell.setValue(2000); cell = cells.get("F14"); cell.setValue(500); cell = cells.get("F15"); cell.setValue(900); cell = cells.get("F16"); cell.setValue(700); cell = cells.get("F17"); cell.setValue(1400); cell = cells.get("F18"); cell.setValue(1350); cell = cells.get("F19"); cell.setValue(300); cell = cells.get("F20"); cell.setValue(500); cell = cells.get("F21"); cell.setValue(1000); cell = cells.get("F22"); cell.setValue(1500); cell = cells.get("F23"); cell.setValue(1500); cell = cells.get("F24"); cell.setValue(1600); cell = cells.get("F25"); cell.setValue(1000); cell = cells.get("F26"); cell.setValue(1200); cell = cells.get("F27"); cell.setValue(1300); cell = cells.get("F28"); cell.setValue(1500); cell = cells.get("F29"); cell.setValue(1400); cell = cells.get("F30"); cell.setValue(1000); // Adding a new sheet by calling the add method var sheetIndex = workbook.getWorksheets().add(); // get the Worksheet element at the specified index. var sheet2 = workbook.getWorksheets().get(sheetIndex); // Name the sheet by calling the setName method sheet2.setName("PivotTable"); // Getting the pivottables collection in the sheet var pivotTables = sheet2.getPivotTables(); // Add a PivotTable to the worksheet by invoking the add method var index = pivotTables.add("=Data!A1:F30", "B3", "PivotTable1"); // get the instance of the newly added PivotTable var pivotTable = pivotTables.get(index); // Show the grand totals by setting the value of setRowGrand method pivotTable.setRowGrand(true); // Set the value of the setColumnGrand method that indicates whether the PivotTable report shows grand totals for columns. pivotTable.setColumnGrand(true); // Set the value of the setAutoFormat method that indicates whether the PivotTable report is automatically formatted. pivotTable.setAutoFormat(true); // Set the PivotTable autoformat type by invoking the setAutoFormatType method pivotTable.setAutoFormatType(aspose.cells.PivotTableAutoFormatType.REPORT_6); // Invoke the addFieldToArea method to drag the first, second, third, fourth and fifth fields to the row area. pivotTable.addFieldToArea(aspose.cells.PivotFieldType.ROW, 0); pivotTable.addFieldToArea(aspose.cells.PivotFieldType.ROW, 2); pivotTable.addFieldToArea(aspose.cells.PivotFieldType.ROW, 1); pivotTable.addFieldToArea(aspose.cells.PivotFieldType.COLUMN, 3); pivotTable.addFieldToArea(aspose.cells.PivotFieldType.DATA, 5); // Set the number format of the first data field by calling the getDataFields method pivotTable.getDataFields().get(0).setNumber(7); // Saving the Excel file by invoking the save method workbook.save( "pivotTable_test.xls");
Hoe maak je een draaigrafiek in Excel programmatisch
Nu gaan we de functionaliteit implementeren om een draaitabel te maken op basis van de gegenereerde draaitabel met behulp van deze Excel JavaScript API. U kunt de onderstaande stappen en het onderstaande codefragment volgen:
- Maak een instantie van de klasse [Workbook][7].
- Roep de add-methode aan om een werkblad [toe te voegen][14] aan de verzameling.
- [krijg][15] het werkbladelement op de opgegeven index.
- Geef het blad een naam door de methode [setName][9] aan te roepen.
- Voeg een kolomdiagram toe door de methode [add][24] aan te roepen.
- [krijg][25] het kaartelement op de opgegeven index.
- Roep de methode [setPivotSource][26] aan om de gegevensbron voor het draaigrafiek in te stellen.
- Roep de methode [setHidePivotFieldButtons][27] aan om ofwel de draaigrafiekveldknoppen alleen te verbergen wanneer de grafiek een draaigrafiek is.
- [bewaar][23] het Excel-bestand.
Kopieer en plak de volgende code in uw hoofdbestand:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
var aspose = aspose || {}; aspose.cells = require("aspose.cells"); // Create an instance of the Workbook class var workbook = new aspose.cells.Workbook("pivotTable_test.xls"); // Invoke the add method to add a worksheet to the collection. var sheetIndex = workbook.getWorksheets().add(aspose.cells.SheetType.CHART); // get the Worksheet element at the specified index. var sheet3 = workbook.getWorksheets().get(sheetIndex); // Name the sheet by invoking the setName method sheet3.setName("PivotChart"); // Add a column chart by calling the add method var chartIndex = sheet3.getCharts().add(aspose.cells.ChartType.COLUMN, 0, 5, 28, 16); // get the Chart element at the specified index. var chart = sheet3.getCharts().get(chartIndex); // Invoke the setPivotSource method to set the pivot chart data source chart.setPivotSource("PivotTable!PivotTable1"); // Call the setHidePivotFieldButtons method to either hide the pivot chart field buttons only when the chart is PivotChart. chart.setHidePivotFieldButtons(false); // Save the Excel file workbook.save( "pivotChart_test.xls");
Ontvang een gratis licentie
U kunt gebruikmaken van een [gratis tijdelijke licentie][28] om de API zonder evaluatiebeperkingen uit te proberen.
Opsommen
Aangezien u deze Excel JavaScript-API op bedrijfsniveau hebt doorlopen. Daarnaast heb je geleerd hoe je een draaitabel maakt in Excel met behulp van Node.js programmatisch en hebben we ook de functionaliteit geïmplementeerd om een draaitabel te maken. Bovendien kunt u de [documentatie][34] bezoeken om de andere functies te leren kennen. Bovendien raden we u aan onze [Aan de slag-gids][29] te volgen. Ten slotte schrijft [conholdate.com][30] consequent nieuwe blogposts. Blijf daarom op de hoogte voor de laatste updates.
Een vraag stellen
U kunt uw vragen of verzoeken aan ons kenbaar maken op ons [forum][31].
Veelgestelde vragen
Hoe maak je een draaitabel in Excel en bewerk je deze? U kunt deze Excel JavaScript [API][2] installeren om programmatisch een draaitabel in Excel te maken. Daarnaast kunt u deze [link][4] bezoeken voor de stappen en het codefragment.
Zie ook
- [Grafieken maken in Excel met Node.js][32]
- [Excel converteren naar Markdown in Node.js][33] [1]: https://docs.fileformat.com/spreadsheet/xlsx/ [2]: https://products.aspose.com/cells/nodejs-java/ [3]: #Excel-JavaScript-API-installation [4]: #How-to-Create-a-Pivot-Table-in-Excel-using-Nodejs [5]: #How-to-make-a-pivot-chart-in-Excel-programmatically [6]: https://releases.aspose.com/cells/nodejs/ [7]: https://reference.aspose.com/cells/nodejs/Workbook [8]: https://reference.aspose.com/cells/nodejs/WorksheetCollection#get [9]: https://reference.aspose.com/cells/nodejs/Worksheet#setName [10]: https://reference.aspose.com/cells/nodejs/Worksheet#getCells [11]: https://reference.aspose.com/cells/nodejs/Cells#get [12]: https://reference.aspose.com/cells/nodejs/Range#setValue [13]: https://reference.aspose.com/cells/nodejs/Range#get [14]: https://reference.aspose.com/cells/nodejs/WorksheetCollection#add [15]: https://reference.aspose.com/cells/nodejs/WorksheetCollection#get [16]: https://reference.aspose.com/cells/nodejs/PivotTableCollection#add [17]: https://reference.aspose.com/cells/nodejs/PivotTable#setRowGrand [18]: https://reference.aspose.com/cells/nodejs/PivotTable#setColumnGrand [19]: https://reference.aspose.com/cells/nodejs/PivotTable#setAutoFormat [20]: https://reference.aspose.com/cells/nodejs/PivotTable#setAutoFormatType [21]: https://reference.aspose.com/cells/nodejs/PivotTable#addFieldToArea [22]: https://reference.aspose.com/cells/nodejs/PivotTable#getDataFields [23]: https://reference.aspose.com/cells/nodejs/Workbook#save [24]: https://reference.aspose.com/cells/nodejs/ChartCollection#add [25]: https://reference.aspose.com/cells/nodejs/ChartCollection#get [26]: https://reference.aspose.com/cells/nodejs/Chart#setPivotSource [27]: https://reference.aspose.com/cells/nodejs/Chart#setHidePivotFieldButtons [28]: https://purchase.conholdate.com/temporary-license [29]: https://docs.aspose.com/cells/net/getting-started/ [30]: https://conholdate.com/ [31]: https://forum.conholdate.com/ [32]: https://blog.conholdate.com/nl/total/how-to-make-charts-in-excel-using-nodejs/ [33]: https://blog.conholdate.com/nl/total/convert-excel-to-markdown-in-nodejs/ [34]: https://docs.aspose.com/cells/nodejsjava/