- Part 1 : Introduction – How to install the Excel Plugin and begin coding with C#.
- Part 2 : How to create an Excel sheet using C# from the dataset MsSql.
- Part 3 : How to add more sheets to the Excel file.
- Part 4 : How to add colors and styling to Excel Sheet.
- Part 5 : How to send excel sheet as an attachment via Email by programatically creating an Excel sheet from dataset stored in MsSql.
In the previous article we discussed how to create an excel sheet using C# from dataset MsSql, the core part of how to create and add items to excel sheet have been discussed in the part 2 of the article, in this article we will discuss how to add an excel sheet using C#.
The main aim of this article is to produce the following output.
The above picture depicts 3 sheets formed in excel [testSheet1, testSheet2, testSheet3] programatically using C#.
Steps to add more sheets to the Excel file using C#
Step 1 : Create a memory stream and call a static memory stream method to return the memory stream
The above code depicts that create a normal method toExcel() or you can call the method on pageLoad and create a memory stream and call another method which contains the core part of the code of this article. Step 2 : Once you have done this, create an ExcelPackage [which creates the core components of the EPPlus dll file to access various methods]
Step 3 : Create an excel worksheet by using the object of ExcelPage, the complete flow of creating an excel sheet is given below
Step 4 : Now save the ExcelSheet pack created, to the memory stream.
Now the ExcelSheet has been created and saved in the memory stream, now we have to output the memory stream to the excel sheet, the content type of the httpContext should be “application/vnd.openxmlformats-officedocument.spreadsheetml.sheet” , and then you can add the header where the file will be transferred to the downloads.
The complete code for the above steps is given as
In the next article we will discuss how to style the excel sheet using C#. Any comments/suggestions related to the above article can be commented below.