SIGN UP MEMBER LOGIN:    
ARTICLE

Export DataSet to Excel

Posted by Manikavelu Velayutham Articles | ADO.NET in C# September 15, 2010
Exporting to single sheet excel file is easier and it requires only few line of codes. Exporting to Multiple sheet is little bit complicated and it requires 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.
Reader Level:
 

Export DataSet to Excel - Multiple Sheets - Max Row Check

Exporting to single sheet excel file is easier and it requires only few line of codes. Exporting to Multiple sheet is little bit complicated and it requires 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 a XML file which could be easily converted to an Excel file. In the code, I will just create an XML file which will store the each DataTable in each sheet of an excel file.

Maximum Row Size in Excel File

Each sheet will allow the user to store only upto 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 way of exporting to excel is much faster than any other method. If you are using 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 conversion process.

Code Explanation

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 difficult in exporting your dataset.

Login to add your contents and source code to this article
share this article :
post comment
 

This logic works only with the extension .xls and not with xlsx. Because the internal logic of xlsx file is entirely different. If you rename any .xlsx file to .zip file, you can see all the files related to that particular file. If the error happens with the extension .xls, surely the error will be logged in a log file, you can see the exact error over there. Surely it will be a formatting issue in the xml. Just analyze the complete logic and implement your requirement.

Posted by Manikavelu Velayutham Mar 09, 2012

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);

Posted by Ross Rooker Mar 08, 2012

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.

Posted by Ross Rooker Mar 08, 2012

thanx i fixed it,dosent work because my button was in update panel

Posted by azar Nov 25, 2011

Hi mate i am getting this error as the code reaching the very first line please help me Thanks

Posted by srikanth gade Nov 25, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor