This example illustrates how to add a filter to the table element in the dashboard and store the result in the Excel file. First of all, you should add Stimulsoft modules to the project. To do this, just use one universal package, all other dependencies will be loaded automatically:
// Stimulsoft Reports module
var Stimulsoft = require('stimulsoft-reports-js');
console.log("Stimulsoft Reports loaded");
...
For the correct rendering of reports and accurate calculation of the sizes of all elements, you should load the fonts you are going to use. In this example, the Roboto-Black font is used, load it using a special
addOpentypeFontFile()
static method. The font will be loaded and added to the internal font collection:
...
// Loading fonts
Stimulsoft.Base.StiFontCollection.addOpentypeFontFile("Roboto-Black.ttf");
console.log("Font loaded");
...
Now you need to create a new dashboard and load data:
...
// Creating new dashboard
var report = new Stimulsoft.Report.StiReport.createNewDashboard();
var dashboard = report.pages.getByIndex(0);
// Load and add reg data
var dataSet = new Stimulsoft.System.Data.DataSet();
dataSet.readXmlFile("Demo.xml");
report.regData("Demo", "Demo", dataSet);
report.dictionary.synchronize();
...
Next, create new table element and add columns to table:
...
// Create new table element
var tableElement = new Stimulsoft.Dashboard.Components.Table.StiTableElement();
tableElement.left = 100;
tableElement.top = 100;
tableElement.width = 500;
tableElement.height = 500;
tableElement.backColor = Stimulsoft.System.Drawing.Color.lightGray;
tableElement.name = "Example";
dashboard.components.add(tableElement);
// Add column to table
var productIDСolumns = new Stimulsoft.Dashboard.Components.Table.StiDimensionColumn();
productIDСolumns.expression = "Products.ProductID";
tableElement.columns.add(productIDСolumns);
var productNameСolumns = new Stimulsoft.Dashboard.Components.Table.StiDimensionColumn();
productNameСolumns.expression = "Products.ProductName";
tableElement.columns.add(productNameСolumns);
var unitPriceСolumns = new Stimulsoft.Dashboard.Components.Table.StiDimensionColumn();
unitPriceСolumns.expression = "Products.UnitPrice";
tableElement.columns.add(unitPriceСolumns);
...
After that, add filters to the table:
...
// Add filter to table
var productIDFilter = new Stimulsoft.Data.Engine.StiDataFilterRule();
productIDFilter.condition = Stimulsoft.Data.Engine.StiDataFilterCondition.BeginningWith;
productIDFilter.path = "Products.ProductID";
productIDFilter.value = "1";
tableElement.dataFilters.add(productIDFilter);
// Add filter to table
var unitPriceFilter = new Stimulsoft.Data.Engine.StiDataFilterRule();
unitPriceFilter.condition = Stimulsoft.Data.Engine.StiDataFilterCondition.EndingWith;
unitPriceFilter.path = "Products.UnitPrice";
unitPriceFilter.value = "1";
tableElement.dataFilters.add(unitPriceFilter);
...
To export dashboard to Excel, you can use the special
exportDocumentAsync()
asynchronous method. As the first argument, you should specify the callback function, which will receive the export result after its completion. You may specify the required format as the second argument of the function, in this case, specify
StiExportFormat.Excel2007
format. You can save result to a file using standard methods for working with files:
...
// Export to Excel
report.exportDocumentAsync((data) => {
// Converting Array into buffer
var buffer = Buffer.from(data)
// File System module
var fs = require('fs');
//Saving to a file
fs.writeFileSync('./SampleDashboard.xlsx', buffer);
console.log("Dashboard saved into Excel-file.");
}, Stimulsoft.Report.StiExportFormat.Excel2007);
Auf dem Screenshot unten Sie können das Ergebnis des Beispiel-Codes ansehen: