1+ @page " /Excel"
2+ @using Syncfusion .XlsIO ;
3+ @using Syncfusion .Drawing ;
4+ @using System .IO ;
5+ @inject Microsoft .JSInterop .IJSRuntime JS
6+ @using System .Data ;
7+
8+ <h2 >Syncfusion Excel library (Essential XlsIO)</h2 >
9+ <p >Syncfusion Excel library (Essential XlsIO) is a Blazor Excel library used to create, read, edit, and convert Excel files in your applications without Microsoft Office dependencies.</p >
10+ <button class =" btn btn-primary" @onclick =" @CreateDocument" >Create Document</button >
11+
12+ @code {
13+ /// <summary >
14+ /// Create an Excel document
15+ /// </summary >
16+ public async void CreateDocument ()
17+ {
18+ // Create an instance of ExcelEngine
19+ using (ExcelEngine excelEngine = new ExcelEngine ())
20+ {
21+ IApplication application = excelEngine .Excel ;
22+ application .DefaultVersion = ExcelVersion .Xlsx ;
23+
24+ // Create a workbook
25+ IWorkbook workbook = application .Workbooks .Create (1 );
26+ IWorksheet worksheet = workbook .Worksheets [0 ];
27+
28+ // Initialize DataTable and data get from SampleDataTable method
29+ DataTable table = SampleDataTable ();
30+
31+ // Import data from DataTable
32+ worksheet .ImportDataTable (table , true , 1 , 1 );
33+
34+ worksheet .UsedRange .AutofitColumns ();
35+
36+
37+ // Save the document as a stream and retrun the stream.
38+ using (MemoryStream stream = new MemoryStream ())
39+ {
40+ // Save the created Excel document to MemoryStream
41+ workbook .SaveAs (stream );
42+
43+ // Download the excel file
44+ await JS .SaveAs (" Sample.xlsx" , stream .ToArray ());
45+ }
46+ }
47+ }
48+ private DataTable SampleDataTable ()
49+ {
50+ DataTable reports = new DataTable ();
51+ reports .Columns .Add (" SalesPerson" );
52+ reports .Columns .Add (" Age" , typeof (int ));
53+ reports .Columns .Add (" Salary" , typeof (int ));
54+ reports .Rows .Add (" Andy Bernard" , 21 , 30000 );
55+ reports .Rows .Add (" Jim Halpert" , 25 , 40000 );
56+ reports .Rows .Add (" Karen Fillippelli" , 30 , 50000 );
57+ reports .Rows .Add (" Phyllis Lapin" , 34 , 39000 );
58+ reports .Rows .Add (" Stanley Hudson" , 45 , 58000 );
59+
60+ return reports ;
61+ }
62+ }
0 commit comments