Click here to Skip to main content
11,414,234 members (69,093 online)
Click here to Skip to main content

Export DataSet to Multiple Excel Sheets

, 12 Dec 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Exporting multiple tables in a DataSet to multiple sheets in an Excel file

Introduction

I was in need of exporting multiple tables in a DataSet to an Excel file with multiple sheets, and I couldn't find anything that actually works. So, I wrote one to help people who might be in the same situation. The complete code in ExcelHelper.cs is shown below.

This only works for Excel 2003 and later versions. If a table in the dataset has more than 65,000 rows, it will break it into multiple sheets for the table with sheet name (tableNameX). replaceXmlChar() function is added to escape XML reserved characters.

Limitations of the Code

It does not handle data tables with more than 256 columns (Excel 2003 column limit), and when data tables have very large rows count, it might throw OutOfMemory exception.

Using the Code

To export a DataSet, just call the ExcelHelper.ToExcel() function as follows:

 var ds = new DataSet();
            var dt = new DataTable("TableName For Sheet1");
            dt.Columns.Add("col1");
            dt.Columns.Add("col2");
            dt.Rows.Add("Value1", "Value2");

            var dt2 = new DataTable("TableName For Sheet2");
            dt2.Columns.Add("col1");
            dt2.Columns.Add("col2");
            dt2.Rows.Add("Value1", "Value2");
            ds.Tables.Add(dt);
            ds.Tables.Add(dt2);
            ExcelHelper.ToExcel(ds, "test.xls", Page.Response);      

Here is the code that does the exporting:

//ExcelHelper.cs

public class ExcelHelper
{
    //Row limits older Excel version per sheet
        const int rowLimit = 65000;

        private static string getWorkbookTemplate()
        {
            var sb = new StringBuilder();
            sb.Append("<xml version>\r\n<Workbook
		xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\r\n");
            sb.Append(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n
		xmlns:x=\"urn:schemas- microsoft-com:office:excel\"\r\n
		xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">\r\n");
            sb.Append(" <Styles>\r\n
		<Style ss:ID=\"Default\" ss:Name=\"Normal\">\r\n
		<Alignment ss:Vertical=\"Bottom\"/>\r\n <Borders/>");
            sb.Append("\r\n <Font/>\r\n <Interior/>\r\n <NumberFormat/>\r\n
		<Protection/>\r\n </Style>\r\n
		<Style ss:ID=\"BoldColumn\">\r\n <Font ");
            sb.Append("x:Family=\"Swiss\" ss:Bold=\"1\"/>\r\n </Style>\r\n
		<Style ss:ID=\"s62\">\r\n <NumberFormat");
            sb.Append(" ss:Format=\"@\"/>\r\n </Style>\r\n
		<Style ss:ID=\"Decimal\">\r\n
		<NumberFormat ss:Format=\"0.0000\"/>\r\n </Style>\r\n ");
            sb.Append("<Style ss:ID=\"Integer\">\r\n
		<NumberFormat ss:Format=\"0\"/>\r\n </Style>\r\n
		<Style ss:ID=\"DateLiteral\">\r\n <NumberFormat ");
            sb.Append("ss:Format=\"mm/dd/yyyy;@\"/>\r\n </Style>\r\n
		<Style ss:ID=\"s28\">\r\n");
            sb.Append("<Alignment ss:Horizontal=\"Left\" ss:Vertical=\"Top\"
		ss:ReadingOrder=\"LeftToRight\" ss:WrapText=\"1\"/>\r\n");
            sb.Append("<Font x:CharSet=\"1\" ss:Size=\"9\"
		ss:Color=\"#808080\" ss:Underline=\"Single\"/>\r\n");
            sb.Append("<Interior ss:Color=\"#FFFFFF\" ss:Pattern=\"Solid\"/>

		</Style>\r\n</Styles>\r\n {0}</Workbook>");
            return sb.ToString();
        }

        private static string replaceXmlChar(string input)
        {
            input = input.Replace("&", "&");
            input = input.Replace("<", "<");
            input = input.Replace(">", ">");
            input = input.Replace("\"", """);
            input = input.Replace("'", "&apos;");
            return input;
        }

        private static string getWorksheets(DataSet source)
        {
            var sw = new StringWriter();
            if (source == null || source.Tables.Count == 0)
            {
                sw.Write("<Worksheet ss:Name=\"Sheet1\"><Table><Row>

		<Cell  ss:StyleID=\"s62\"><Data ss:Type=\"String\"></Data>
		</Cell></Row></Table></Worksheet>");
                return sw.ToString();
            }
            foreach (DataTable dt in source.Tables)
            {
                if (dt.Rows.Count == 0)
                    sw.Write("<Worksheet ss:Name=\"" + replaceXmlChar(dt.TableName) +
			"\"><Table><Row><Cell  ss:StyleID=\"s62\">

			<Data ss:Type=\"String\"></Data></Cell></Row>
			</Table></Worksheet>");
                else
                {
                    //write each row data
                    var sheetCount = 0;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if ((i % rowLimit) == 0)
                        {
                            //add close tags for previous sheet of the same data table
                            if ((i / rowLimit) > sheetCount)
                            {
                                sw.Write("</Table></Worksheet>");
                                sheetCount = (i / rowLimit);
                            }
                            sw.Write("<Worksheet ss:Name=\"" +
				replaceXmlChar(dt.TableName) +
                                     (((i / rowLimit) == 0) ? "" :
				Convert.ToString(i / rowLimit)) + "\"><Table>");
                            //write column name row
                            sw.Write("<Row>");
                            foreach (DataColumn dc in dt.Columns)
                                sw.Write(
                                    string.Format(
                                        "<Cell ss:StyleID=\"BoldColumn\">

				<Data ss:Type=\"String\">{0}</Data></Cell>",
                                        replaceXmlChar(dc.ColumnName)));
                            sw.Write("</Row>\r\n");
                        }
                        sw.Write("<Row>\r\n");
                        foreach (DataColumn dc in dt.Columns)
                            sw.Write(
                                string.Format(
                                    "<Cell ss:StyleID=\"s62\"><Data ss:Type=\"String\">

					{0}</Data></Cell>",
                                    		replaceXmlChar
					(dt.Rows[i][dc.ColumnName].ToString())));
                        sw.Write("</Row>\r\n");
                    }
                    sw.Write("</Table></Worksheet>");
                }
            }

            return sw.ToString();
        }
        public static string GetExcelXml(DataTable dtInput, string filename)
        {
            var excelTemplate = getWorkbookTemplate();
            var ds = new DataSet();
            ds.Tables.Add(dtInput.Copy());
            var worksheets = getWorksheets(ds);
            var excelXml = string.Format(excelTemplate, worksheets);
            return excelXml;
        }

        public static string GetExcelXml(DataSet dsInput, string filename)
        {
            var excelTemplate = getWorkbookTemplate();
            var worksheets = getWorksheets(dsInput);
            var excelXml = string.Format(excelTemplate, worksheets);
            return excelXml;
        }

        public static void ToExcel
		(DataSet dsInput, string filename, HttpResponse response)
        {
            var excelXml = GetExcelXml(dsInput, filename);
            response.Clear();
            response.AppendHeader("Content-Type", "application/vnd.ms-excel");
            response.AppendHeader
		("Content-disposition", "attachment; filename=" + filename);
            response.Write(excelXml);
            response.Flush();
            response.End();
        }

        public static void ToExcel
		(DataTable dtInput, string filename, HttpResponse response)
        {
            var ds = new DataSet();
            ds.Tables.Add(dtInput.Copy());
            ToExcel(ds, filename, response);
        }
    }

History

  • 7th December, 2008: Initial post
  • 8th December, 2008: Source code and article updated
  • 11th December, 2008: Source code and article updated

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ming_Lu
Software Developer Oliver Wyman Groups
United States United States
Education: Masters in Applied mathematics

Certification:
MCP in Asp.net
MCP in SQL Server 2008 Implementation.

Working Experience In .Net since 2005

Comments and Discussions

 
QuestionThanks... :) Pin
aambhi, 3-Mar-15 5:35
memberaambhi3-Mar-15 5:35 
AnswerDatetime format issue - resolved Pin
Babuji Godem, 16-Feb-15 1:37
memberBabuji Godem16-Feb-15 1:37 
QuestionProbelm with datetime Pin
Babuji Godem, 15-Feb-15 23:19
memberBabuji Godem15-Feb-15 23:19 
QuestionSave xls file at directory Pin
Member 8414501, 23-Oct-14 11:56
memberMember 841450123-Oct-14 11:56 
QuestionSave as Xlsx file Pin
vimalrvs, 16-Sep-14 20:41
membervimalrvs16-Sep-14 20:41 
Questionhow can i replace Table1,Table2(Sheet name) to some other value Pin
Jeno M, 18-Mar-14 2:09
memberJeno M18-Mar-14 2:09 
QuestionRegarding implementing the sample code Pin
dk deepak, 17-Oct-13 0:41
memberdk deepak17-Oct-13 0:41 
QuestionIt's great Pin
Ahmed Mostafa, 1-Oct-13 21:50
memberAhmed Mostafa1-Oct-13 21:50 
QuestionExport DataSet to Multiple Excel Sheets Pin
hossein nasirzade, 31-May-13 22:03
memberhossein nasirzade31-May-13 22:03 
QuestionThread Exception [modified] Pin
Member 9952350, 21-May-13 4:57
memberMember 995235021-May-13 4:57 
Questionjet4.0 Pin
nadersghir, 28-Jan-13 4:41
membernadersghir28-Jan-13 4:41 
QuestionHow to store the excel file to Network shared Drive instead of opening on client machine Pin
keyur_parekh, 25-Oct-12 12:37
memberkeyur_parekh25-Oct-12 12:37 
QuestionUnable to export if there are 1 lakh records in the dataset Pin
Abhishek K Gohil, 11-Oct-12 2:51
memberAbhishek K Gohil11-Oct-12 2:51 
Questionnot in correct format for 2010. Pin
Teenustar, 10-Oct-12 11:42
memberTeenustar10-Oct-12 11:42 
QuestionRe: not in correct format for 2010. Pin
vimalrvs, 16-Sep-14 20:38
membervimalrvs16-Sep-14 20:38 
GeneralThanks For Share this artical very helpful Pin
sapnapal, 8-Aug-12 23:29
membersapnapal8-Aug-12 23:29 
GeneralMy vote of 5 Pin
chus24, 25-Jun-12 15:43
memberchus2425-Jun-12 15:43 
Questiondata display in gridview depend on user key in.how to export it? Pin
musiw, 13-Jun-12 0:11
membermusiw13-Jun-12 0:11 
QuestionWorks Great...Thanks Pin
Member 332427, 5-Jun-12 9:29
memberMember 3324275-Jun-12 9:29 
GeneralNice Pin
sivasankari ts, 15-May-12 0:28
membersivasankari ts15-May-12 0:28 
GeneralVote of 5! Pin
mk_ln, 12-Apr-12 12:07
membermk_ln12-Apr-12 12:07 
GeneralMy vote of 5 Pin
manoj kumar choubey, 5-Apr-12 0:27
membermanoj kumar choubey5-Apr-12 0:27 
GeneralThanks for this helpful code Pin
monika nafdey, 14-Mar-12 20:19
membermonika nafdey14-Mar-12 20:19 
GeneralMy vote of 5 Pin
ProEnggSoft, 1-Mar-12 19:30
memberProEnggSoft1-Mar-12 19:30 
Questionerror message when opening text.xls Pin
Member 8692747, 1-Mar-12 17:55
memberMember 86927471-Mar-12 17:55 
AnswerRe: error message when opening text.xls Pin
cadetduke, 30-Mar-12 10:49
membercadetduke30-Mar-12 10:49 
GeneralRe: error message when opening text.xls Pin
Teenustar, 10-Oct-12 11:55
memberTeenustar10-Oct-12 11:55 
GeneralMy vote of 5 Pin
ARK Kambhampati, 27-Feb-12 21:03
memberARK Kambhampati27-Feb-12 21:03 
QuestionAlternate row color Pin
kinjalin, 29-Nov-11 22:02
memberkinjalin29-Nov-11 22:02 
GeneralMy vote of 5 Pin
Fredde1977, 28-Sep-11 4:50
memberFredde197728-Sep-11 4:50 
Questionneed help with style sheet error [modified] Pin
talmans, 26-Aug-11 13:47
membertalmans26-Aug-11 13:47 
I get an error when download a file. The ... style\main.css cannot be found. I don't see a reference to that in the example. Where does it reference the file?

This line generates the error
sb.Append("\r\n");

Why do many attribute tags have a leading '\' character?

modified on Friday, August 26, 2011 6:59 PM

BugNot woking with Marquee taq? Pin
muthura1, 20-Jun-11 2:39
membermuthura120-Jun-11 2:39 
GeneralMy vote of 5! Pin
Filip D'haene, 14-May-11 12:11
memberFilip D'haene14-May-11 12:11 
QuestionCell Formatting.. Issues...? Pin
codingrocks, 13-Mar-11 3:24
membercodingrocks13-Mar-11 3:24 
AnswerRe: Cell Formatting.. Issues...? Pin
Ming_Lu, 13-Mar-11 15:41
memberMing_Lu13-Mar-11 15:41 
GeneralMy vote of 4 Pin
praveen_adb, 9-Jan-11 21:26
memberpraveen_adb9-Jan-11 21:26 
GeneralMy vote of 5 Pin
bacoares, 23-Nov-10 5:48
memberbacoares23-Nov-10 5:48 
GeneralMy vote of 5 Pin
L.H.C, 1-Oct-10 19:56
memberL.H.C1-Oct-10 19:56 
GeneralEasy alternative Pin
CikaPero, 13-Apr-10 23:52
memberCikaPero13-Apr-10 23:52 
GeneralAuto Fit Excel Columns Pin
Member 4651770, 14-Jan-10 2:10
memberMember 465177014-Jan-10 2:10 
GeneralSys.WebForms.PageRequestManagerParserErrorException Pin
Steve Crochet, 28-Dec-09 10:14
memberSteve Crochet28-Dec-09 10:14 
GeneralRe: Sys.WebForms.PageRequestManagerParserErrorException Pin
Ming_Lu, 2-Jan-10 4:55
memberMing_Lu2-Jan-10 4:55 
GeneralRe: Sys.WebForms.PageRequestManagerParserErrorException Pin
Steve Crochet, 4-Jan-10 6:44
memberSteve Crochet4-Jan-10 6:44 
QuestionWhat shd i use instead of var?? Pin
Bushra28, 13-Oct-09 0:12
memberBushra2813-Oct-09 0:12 
GeneralBig File Size Pin
txshxx, 4-Oct-09 18:12
membertxshxx4-Oct-09 18:12 
QuestionExport to Excel 2003 using ASP [modified] Pin
karansaxena, 15-Jun-09 11:46
memberkaransaxena15-Jun-09 11:46 
GeneralHandle linefeed in cells Pin
DACS, 13-Mar-09 14:43
memberDACS13-Mar-09 14:43 
AnswerRe: Handle linefeed in cells Pin
Ming_Lu, 3-Apr-09 8:18
memberMing_Lu3-Apr-09 8:18 
GeneralRe: Handle linefeed in cells Pin
DACS, 3-Apr-09 22:35
memberDACS3-Apr-09 22:35 
QuestionCan I use this class in Windows Application? Pin
Su Win, 8-Mar-09 5:17
memberSu Win8-Mar-09 5:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150427.2 | Last Updated 12 Dec 2008
Article Copyright 2008 by Ming_Lu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid