// Create an xlsx file stream FileStream outputStream = new FileStream("GenerateReportForSpecificSheet.xlsx", FileMode.Create); //create a new workbook var workbook = new GrapeCity.Documents.Excel.Workbook(); var templateFile = this.GetResourceStream("xlsx\\Template_GenerateReportForSpecificSheet.xlsx"); workbook.Open(templateFile); #region Define custom classes //public class SalesData //{ // public List<SalesRecord> Sales; //} //public class SalesRecord //{ // public string Area; // public string City; // public string Category; // public string Name; // public double Revenue; //} #endregion var salesData = new SalesData { Sales = new List<SalesRecord>() }; #region Init Data var record1 = new SalesRecord { Area = "North America", City = "Chicago", Category = "Consumer Electronics", Name = "Bose 785593-0050", Revenue = 92800 }; salesData.Sales.Add(record1); var record2 = new SalesRecord { Area = "North America", City = "New York", Category = "Consumer Electronics", Name = "Bose 785593-0050", Revenue = 92800 }; salesData.Sales.Add(record2); var record3 = new SalesRecord { Area = "South America", City = "Santiago", Category = "Consumer Electronics", Name = "Bose 785593-0050", Revenue = 19550 }; salesData.Sales.Add(record3); var record4 = new SalesRecord { Area = "North America", City = "Chicago", Category = "Consumer Electronics", Name = "Canon EOS 1500D", Revenue = 98650 }; salesData.Sales.Add(record4); var record5 = new SalesRecord { Area = "North America", City = "Minnesota", Category = "Consumer Electronics", Name = "Canon EOS 1500D", Revenue = 89110 }; salesData.Sales.Add(record5); var record6 = new SalesRecord { Area = "South America", City = "Santiago", Category = "Consumer Electronics", Name = "Canon EOS 1500D", Revenue = 459000 }; salesData.Sales.Add(record6); var record7 = new SalesRecord { Area = "North America", City = "Chicago", Category = "Consumer Electronics", Name = "Haier 394L 4Star", Revenue = 367050 }; salesData.Sales.Add(record7); var record8 = new SalesRecord { Area = "South America", City = "Quito", Category = "Consumer Electronics", Name = "Haier 394L 4Star", Revenue = 729100 }; salesData.Sales.Add(record8); var record9 = new SalesRecord { Area = "South America", City = "Santiago", Category = "Consumer Electronics", Name = "Haier 394L 4Star", Revenue = 578900 }; salesData.Sales.Add(record9); var record10 = new SalesRecord { Area = "North America", City = "Fremont", Category = "Consumer Electronics", Name = "IFB 6.5 Kg FullyAuto", Revenue = 904930 }; salesData.Sales.Add(record10); var record11 = new SalesRecord { Area = "South America", City = "Buenos Aires", Category = "Consumer Electronics", Name = "IFB 6.5 Kg FullyAuto", Revenue = 673800 }; salesData.Sales.Add(record11); var record12 = new SalesRecord { Area = "South America", City = "Medillin", Category = "Consumer Electronics", Name = "IFB 6.5 Kg FullyAuto", Revenue = 82910 }; salesData.Sales.Add(record12); var record13 = new SalesRecord { Area = "North America", City = "Chicago", Category = "Consumer Electronics", Name = "Mi LED 40inch", Revenue = 550010 }; salesData.Sales.Add(record13); var record14 = new SalesRecord { Area = "North America", City = "Minnesota", Category = "Consumer Electronics", Name = "Mi LED 40inch", Revenue = 1784702 }; salesData.Sales.Add(record14); var record15 = new SalesRecord { Area = "South America", City = "Santiago", Category = "Consumer Electronics", Name = "Mi LED 40inch", Revenue = 102905 }; salesData.Sales.Add(record15); var record16 = new SalesRecord { Area = "North America", City = "Chicago", Category = "Consumer Electronics", Name = "Sennheiser HD 4.40-BT", Revenue = 178100 }; salesData.Sales.Add(record16); var record17 = new SalesRecord { Area = "South America", City = "Quito", Category = "Consumer Electronics", Name = "Sennheiser HD 4.40-BT", Revenue = 234459 }; salesData.Sales.Add(record17); var record18 = new SalesRecord { Area = "North America", City = "Minnesota", Category = "Mobile", Name = "Iphone XR", Revenue = 1734621 }; salesData.Sales.Add(record18); var record19 = new SalesRecord { Area = "South America", City = "Santiago", Category = "Mobile", Name = "Iphone XR", Revenue = 109300 }; salesData.Sales.Add(record19); var record20 = new SalesRecord { Area = "North America", City = "Chicago", Category = "Mobile", Name = "OnePlus 7Pro", Revenue = 499100 }; salesData.Sales.Add(record20); var record21 = new SalesRecord { Area = "South America", City = "Quito", Category = "Mobile", Name = "OnePlus 7Pro", Revenue = 215000 }; salesData.Sales.Add(record21); var record22 = new SalesRecord { Area = "North America", City = "Minnesota", Category = "Mobile", Name = "Redmi 7", Revenue = 81650 }; salesData.Sales.Add(record22); var record23 = new SalesRecord { Area = "South America", City = "Quito", Category = "Mobile", Name = "Redmi 7", Revenue = 276390 }; salesData.Sales.Add(record23); var record24 = new SalesRecord { Area = "North America", City = "Minnesota", Category = "Mobile", Name = "Samsung S9", Revenue = 896250 }; salesData.Sales.Add(record24); var record25 = new SalesRecord { Area = "South America", City = "Buenos Aires", Category = "Mobile", Name = "Samsung S9", Revenue = 896250 }; salesData.Sales.Add(record25); var record26 = new SalesRecord { Area = "South America", City = "Quito", Category = "Mobile", Name = "Samsung S9", Revenue = 716520 }; salesData.Sales.Add(record26); #endregion // Add data source workbook.AddDataSource("dt", salesData); // Init template global settings workbook.Names.Add("TemplateOptions.KeepLineSize", "true"); // Process the template and return the specified report IWorkbook report = workbook.GenerateReport(workbook.Worksheets["Sales"]); // Save the report as xlsx to a stream report.Save(outputStream); // Close the xlsx stream outputStream.Close();
' Create an xlsx file stream Dim outputStream = File.Create("GenerateReportForSpecificSheet.xlsx") ' Create a new Workbook Dim workbook As New Workbook Dim templateFile = GetResourceStream("xlsx\Template_GenerateReportForSpecificSheet.xlsx") workbook.Open(templateFile) #Region "Define custom classes" 'public class SalesData '{ ' public List<SalesRecord> Sales; '} 'public class SalesRecord '{ ' public string Area; ' public string City; ' public string Category; ' public string Name; ' public double Revenue; '} #End Region Dim salesData = New SalesData With {.Sales = New List(Of SalesRecord)()} #Region "Init Data" Dim record1 = New SalesRecord With { .Area = "North America", .City = "Chicago", .Category = "Consumer Electronics", .Name = "Bose 785593-0050", .Revenue = 92800 } salesData.Sales.Add(record1) Dim record2 = New SalesRecord With { .Area = "North America", .City = "New York", .Category = "Consumer Electronics", .Name = "Bose 785593-0050", .Revenue = 92800 } salesData.Sales.Add(record2) Dim record3 = New SalesRecord With { .Area = "South America", .City = "Santiago", .Category = "Consumer Electronics", .Name = "Bose 785593-0050", .Revenue = 19550 } salesData.Sales.Add(record3) Dim record4 = New SalesRecord With { .Area = "North America", .City = "Chicago", .Category = "Consumer Electronics", .Name = "Canon EOS 1500D", .Revenue = 98650 } salesData.Sales.Add(record4) Dim record5 = New SalesRecord With { .Area = "North America", .City = "Minnesota", .Category = "Consumer Electronics", .Name = "Canon EOS 1500D", .Revenue = 89110 } salesData.Sales.Add(record5) Dim record6 = New SalesRecord With { .Area = "South America", .City = "Santiago", .Category = "Consumer Electronics", .Name = "Canon EOS 1500D", .Revenue = 459000 } salesData.Sales.Add(record6) Dim record7 = New SalesRecord With { .Area = "North America", .City = "Chicago", .Category = "Consumer Electronics", .Name = "Haier 394L 4Star", .Revenue = 367050 } salesData.Sales.Add(record7) Dim record8 = New SalesRecord With { .Area = "South America", .City = "Quito", .Category = "Consumer Electronics", .Name = "Haier 394L 4Star", .Revenue = 729100 } salesData.Sales.Add(record8) Dim record9 = New SalesRecord With { .Area = "South America", .City = "Santiago", .Category = "Consumer Electronics", .Name = "Haier 394L 4Star", .Revenue = 578900 } salesData.Sales.Add(record9) Dim record10 = New SalesRecord With { .Area = "North America", .City = "Fremont", .Category = "Consumer Electronics", .Name = "IFB 6.5 Kg FullyAuto", .Revenue = 904930 } salesData.Sales.Add(record10) Dim record11 = New SalesRecord With { .Area = "South America", .City = "Buenos Aires", .Category = "Consumer Electronics", .Name = "IFB 6.5 Kg FullyAuto", .Revenue = 673800 } salesData.Sales.Add(record11) Dim record12 = New SalesRecord With { .Area = "South America", .City = "Medillin", .Category = "Consumer Electronics", .Name = "IFB 6.5 Kg FullyAuto", .Revenue = 82910 } salesData.Sales.Add(record12) Dim record13 = New SalesRecord With { .Area = "North America", .City = "Chicago", .Category = "Consumer Electronics", .Name = "Mi LED 40inch", .Revenue = 550010 } salesData.Sales.Add(record13) Dim record14 = New SalesRecord With { .Area = "North America", .City = "Minnesota", .Category = "Consumer Electronics", .Name = "Mi LED 40inch", .Revenue = 1784702 } salesData.Sales.Add(record14) Dim record15 = New SalesRecord With { .Area = "South America", .City = "Santiago", .Category = "Consumer Electronics", .Name = "Mi LED 40inch", .Revenue = 102905 } salesData.Sales.Add(record15) Dim record16 = New SalesRecord With { .Area = "North America", .City = "Chicago", .Category = "Consumer Electronics", .Name = "Sennheiser HD 4.40-BT", .Revenue = 178100 } salesData.Sales.Add(record16) Dim record17 = New SalesRecord With { .Area = "South America", .City = "Quito", .Category = "Consumer Electronics", .Name = "Sennheiser HD 4.40-BT", .Revenue = 234459 } salesData.Sales.Add(record17) Dim record18 = New SalesRecord With { .Area = "North America", .City = "Minnesota", .Category = "Mobile", .Name = "Iphone XR", .Revenue = 1734621 } salesData.Sales.Add(record18) Dim record19 = New SalesRecord With { .Area = "South America", .City = "Santiago", .Category = "Mobile", .Name = "Iphone XR", .Revenue = 109300 } salesData.Sales.Add(record19) Dim record20 = New SalesRecord With { .Area = "North America", .City = "Chicago", .Category = "Mobile", .Name = "OnePlus 7Pro", .Revenue = 499100 } salesData.Sales.Add(record20) Dim record21 = New SalesRecord With { .Area = "South America", .City = "Quito", .Category = "Mobile", .Name = "OnePlus 7Pro", .Revenue = 215000 } salesData.Sales.Add(record21) Dim record22 = New SalesRecord With { .Area = "North America", .City = "Minnesota", .Category = "Mobile", .Name = "Redmi 7", .Revenue = 81650 } salesData.Sales.Add(record22) Dim record23 = New SalesRecord With { .Area = "South America", .City = "Quito", .Category = "Mobile", .Name = "Redmi 7", .Revenue = 276390 } salesData.Sales.Add(record23) Dim record24 = New SalesRecord With { .Area = "North America", .City = "Minnesota", .Category = "Mobile", .Name = "Samsung S9", .Revenue = 896250 } salesData.Sales.Add(record24) Dim record25 = New SalesRecord With { .Area = "South America", .City = "Buenos Aires", .Category = "Mobile", .Name = "Samsung S9", .Revenue = 896250 } salesData.Sales.Add(record25) Dim record26 = New SalesRecord With { .Area = "South America", .City = "Quito", .Category = "Mobile", .Name = "Samsung S9", .Revenue = 716520 } salesData.Sales.Add(record26) #End Region ' Add data source workbook.AddDataSource("dt", salesData) ' Init template global settings workbook.Names.Add("TemplateOptions.KeepLineSize", "true") ' Process the template and return the specified report Dim report As IWorkbook = workbook.GenerateReport(workbook.Worksheets("Sales")) ' Save the report as xlsx to a stream report.Save(outputStream) ' close the xlsx stream outputStream.Close()