Export DataSet To Excel - Multiple Sheets - Max Row Check
Exporting to a single sheet excel file is easier and it requires only a few lines of codes. Exporting to Multiple sheets is a little bit complicated and it requires a little bit of extra effort. Mostly we will go for third party controls or dlls to achieve that. Here is the simple way to export your dataset to multiple sheets in an Excel file.
How the Excel files are internally stored?
Most of the excel files are generally stored in the format of an XML file internally. To achieve our goal, we need to create an XML file that could be easily converted to an Excel file. In the code, I will just create an XML file that will store each DataTable in each sheet of an excel file.
Maximum Row Size in Excel File
Each sheet will allow the user to store only up to 64000 rows, if you try to export your data without checking this condition, surely your code will throw the error. It's mandatory to check the condition while exporting the data to an excel file. It's clearly handled in this code. When the rows count reaches 64000, it will automatically store the next row in the next sheet as per the code.
XML way of Exporting to Excel
XML's way of exporting to excel is much faster than any other method. If you are using a third party dll to export an excel file, surely it will take more time in conversion. But this method is very much robust and faster in the conversion process.
Code Explanation
The attached code will just export only one table to one sheet and it checks the condition of max rows, if the row exceeds the limit of 64000, it will directly write the next row in the next sheet. You can apply the same logic to export all the tables in the dataset to multiple sheets. Just giving this work to the readers, to clearly understand the code rather than simply copy-pasting the code.
I will tell you the clue to implement your logic- for (int x = 0; x < dsInput.Tables[0].Columns.Count; x++)
You can put one more loop above this line in the code to export all the tables to multiple sheet. In this line you need to change the Tables[0] to Tables[i].
You can ask your questions if you find any difficulty in exporting your dataset.
Anil KumarPosted Aug 9, 2020, 9:08 AM
Hi, I am unable to open the generated excel file. It is showing like file is corrupt.
Ernesto CamaraPosted Sep 7, 2014, 10:04 PM
Wow!! Nice job. Really this code help me a lot!. Congratulations.!!!
Former memberPosted Sep 30, 2012, 1:16 PM
This is really nice article http://www.dotnetpools.com/Article/ArticleDetiail/?articleId=22&title=Gridview%20Export%20To%20Excel%20In%20Asp.Net%20C#
Ross RookerPosted Mar 8, 2012, 3:23 PM
Like this: MemoryStream ms = new MemoryStream(bytearray); System.Net.Mail.Attachment objAtt = new System.Net.Mail.Attachment(ms, "TextFile.xls"); objMail.Subject = "Exported Excel"; objMail.Body = "Exported Excel"; objMail.Attachments.Add(objAtt);
Ross RookerPosted Mar 8, 2012, 2:55 PM
Message: The file you are trying to open, 'Export.xls', is in a different format than specied by the extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now? If I click YES it does open.
azarPosted Oct 29, 2011, 6:38 AM
plz help me.this code dosnt work in the page that have MasterPageFile. when i execute this code anything happen
padma yeddulaeditedPosted Jun 30, 2011, 12:47 AMEdited Jun 30, 2011, 12:55 AM
hi, i need to export dataset tables to excel 2007 format(i.e xlsx file) for windows applications using c#.net. Dataset tables may contain half a million records .can u please help me the coding part.
sara ajayPosted May 11, 2011, 2:56 AM
hi! i am using as per your given code 1's i executed were can i fine .xls document in my system
Vishal WhawalPosted Mar 14, 2011, 2:14 PM
Thanks Manikavelu It works, But now this files conatins 4 sheets and it's size is almost 580 MB, when I have save this file by File --> Save As option to .xlsx it's size reduce up to 50 MB. So through code how can we Export it to client machine in .xlsx format ? Now I am using .net 2.0 framework and looking in to the same. using Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" Can you please help me in it? Thanks in advance.. Vishal
Archana PatilPosted Feb 28, 2011, 3:46 AM
Hi, I am unable to import using System.Collections.Generic; using System.Linq; please help
Archana PatilPosted Feb 28, 2011, 1:35 AM
hi, I used this code in vb 1.1 but its not working for me.I am not able to open the generated excel sheet please help. Thanks,
vijay gunasekaranPosted Sep 27, 2010, 5:28 AM
Hi, Can anybody help me to export the dataset to excel in console application . I looked all over the Internet and could not find anything useful, only ASP.NET ways of exporting. @Manikavel : Is the same code will work for Console application? Thanks in Advance., Vijay G
manoj dagdePosted Sep 23, 2010, 9:37 AM
Hi, I have exported 5 table to 5 different sheet. Now i need to hide column and one of worksheet.