Click here to Skip to main content
15,921,250 members
Articles / Programming Languages / C#
Article

Export a DataSet to Microsoft Excel without the use of COM objects

Rate me:
Please Sign up or sign in to vote.
3.84/5 (84 votes)
28 May 20051 min read 668.1K   120   158
A simple function that writes a DataSet to a Microsoft Excel document.

Introduction

This function takes in a DataSet and file name and writes the DataSet to an Excel worksheet. The code is pretty straightforward. Great thing about this function is that, it's technically an XML file that is saved as an XLS file. So it can be used as either file format. No more leading zero truncation on numbers that look like strings. Example, if you made a tab delimited file and put a field such as "00036" (a field that looks like a number but should be regarded as a string), MS Excel would truncate the leading zeros... This problem is solved with this method.

Here is the code:

C#
public static void exportToExcel(DataSet source, string fileName)

{

    System.IO.StreamWriter excelDoc;

    excelDoc = new System.IO.StreamWriter(fileName);
    const string startExcelXML = "<xml version>\r\n<Workbook " + 
          "xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\r\n" + 
          " 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 <Styles>\r\n " + 
          "<Style ss:ID=\"Default\" ss:Name=\"Normal\">\r\n " + 
          "<Alignment ss:Vertical=\"Bottom\"/>\r\n <Borders/>" + 
          "\r\n <Font/>\r\n <Interior/>\r\n <NumberFormat/>" + 
          "\r\n <Protection/>\r\n </Style>\r\n " + 
          "<Style ss:ID=\"BoldColumn\">\r\n <Font " + 
          "x:Family=\"Swiss\" ss:Bold=\"1\"/>\r\n </Style>\r\n " + 
          "<Style     ss:ID=\"StringLiteral\">\r\n <NumberFormat" + 
          " ss:Format=\"@\"/>\r\n </Style>\r\n <Style " + 
          "ss:ID=\"Decimal\">\r\n <NumberFormat " + 
          "ss:Format=\"0.0000\"/>\r\n </Style>\r\n " + 
          "<Style ss:ID=\"Integer\">\r\n <NumberFormat " + 
          "ss:Format=\"0\"/>\r\n </Style>\r\n <Style " + 
          "ss:ID=\"DateLiteral\">\r\n <NumberFormat " + 
          "ss:Format=\"mm/dd/yyyy;@\"/>\r\n </Style>\r\n " + 
          "</Styles>\r\n ";
     const string endExcelXML = "</Workbook>";

     int rowCount = 0;
     int sheetCount = 1;
     /*
    <xml version>
    <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
    xmlns:o="urn:schemas-microsoft-com:office:office"
    xmlns:x="urn:schemas-microsoft-com:office:excel"
    xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
    <Styles>
    <Style ss:ID="Default" ss:Name="Normal">
      <Alignment ss:Vertical="Bottom"/>
      <Borders/>
      <Font/>
      <Interior/>
      <NumberFormat/>
      <Protection/>
    </Style>
    <Style ss:ID="BoldColumn">
      <Font x:Family="Swiss" ss:Bold="1"/>
    </Style>
    <Style ss:ID="StringLiteral">
      <NumberFormat ss:Format="@"/>
    </Style>
    <Style ss:ID="Decimal">
      <NumberFormat ss:Format="0.0000"/>
    </Style>
    <Style ss:ID="Integer">
      <NumberFormat ss:Format="0"/>
    </Style>
    <Style ss:ID="DateLiteral">
      <NumberFormat ss:Format="mm/dd/yyyy;@"/>
    </Style>
    </Styles>
    <Worksheet ss:Name="Sheet1">
    </Worksheet>
    </Workbook>
    */
    excelDoc.Write(startExcelXML);
    excelDoc.Write("<Worksheet ss:Name=\"Sheet" + sheetCount + "\">");
    excelDoc.Write("<Table>");
    excelDoc.Write("<Row>");
    for(int x = 0; x < source.Tables[0].Columns.Count; x++)
    {
      excelDoc.Write("<Cell ss:StyleID=\"BoldColumn\"><Data ss:Type=\"String\">");
      excelDoc.Write(source.Tables[0].Columns[x].ColumnName);
      excelDoc.Write("</Data></Cell>");
    }
    excelDoc.Write("</Row>");
    foreach(DataRow x in source.Tables[0].Rows)
    {
      rowCount++;
      //if the number of rows is > 64000 create a new page to continue output
      if(rowCount==64000) 
      {
        rowCount = 0;
        sheetCount++;
        excelDoc.Write("</Table>");
        excelDoc.Write(" </Worksheet>");
        excelDoc.Write("<Worksheet ss:Name=\"Sheet" + sheetCount + "\">");
        excelDoc.Write("<Table>");
      }
      excelDoc.Write("<Row>"); //ID=" + rowCount + "
      for(int y = 0; y < source.Tables[0].Columns.Count; y++)
      {
        System.Type rowType;
        rowType = x[y].GetType();
        switch(rowType.ToString())
        {
          case "System.String":
             string XMLstring = x[y].ToString();
             XMLstring = XMLstring.Trim();
             XMLstring = XMLstring.Replace("&","&");
             XMLstring = XMLstring.Replace(">",">");
             XMLstring = XMLstring.Replace("<","<");
             excelDoc.Write("<Cell ss:StyleID=\"StringLiteral\">" + 
                            "<Data ss:Type=\"String\">");
             excelDoc.Write(XMLstring);
             excelDoc.Write("</Data></Cell>");
             break;
           case "System.DateTime":
             //Excel has a specific Date Format of YYYY-MM-DD followed by  
             //the letter 'T' then hh:mm:sss.lll Example 2005-01-31T24:01:21.000
             //The Following Code puts the date stored in XMLDate 
             //to the format above
             DateTime XMLDate = (DateTime)x[y];
             string XMLDatetoString = ""; //Excel Converted Date
             XMLDatetoString = XMLDate.Year.ToString() +
                  "-" + 
                  (XMLDate.Month < 10 ? "0" + 
                  XMLDate.Month.ToString() : XMLDate.Month.ToString()) +
                  "-" +
                  (XMLDate.Day < 10 ? "0" + 
                  XMLDate.Day.ToString() : XMLDate.Day.ToString()) +
                  "T" +
                  (XMLDate.Hour < 10 ? "0" + 
                  XMLDate.Hour.ToString() : XMLDate.Hour.ToString()) +
                  ":" +
                  (XMLDate.Minute < 10 ? "0" + 
                  XMLDate.Minute.ToString() : XMLDate.Minute.ToString()) +
                  ":" +
                  (XMLDate.Second < 10 ? "0" + 
                  XMLDate.Second.ToString() : XMLDate.Second.ToString()) + 
                  ".000";
                excelDoc.Write("<Cell ss:StyleID=\"DateLiteral\">" + 
                             "<Data ss:Type=\"DateTime\">");
                excelDoc.Write(XMLDatetoString);
                excelDoc.Write("</Data></Cell>");
                break;
              case "System.Boolean":
                excelDoc.Write("<Cell ss:StyleID=\"StringLiteral\">" + 
                            "<Data ss:Type=\"String\">");
                excelDoc.Write(x[y].ToString());
                excelDoc.Write("</Data></Cell>");
                break;
              case "System.Int16":
              case "System.Int32":
              case "System.Int64":
              case "System.Byte":
                excelDoc.Write("<Cell ss:StyleID=\"Integer\">" + 
                        "<Data ss:Type=\"Number\">");
                excelDoc.Write(x[y].ToString());
                excelDoc.Write("</Data></Cell>");
                break;
              case "System.Decimal":
              case "System.Double":
                excelDoc.Write("<Cell ss:StyleID=\"Decimal\">" + 
                      "<Data ss:Type=\"Number\">");
                excelDoc.Write(x[y].ToString());
                excelDoc.Write("</Data></Cell>");
                break;
              case "System.DBNull":
                excelDoc.Write("<Cell ss:StyleID=\"StringLiteral\">" + 
                      "<Data ss:Type=\"String\">");
                excelDoc.Write("");
                excelDoc.Write("</Data></Cell>");
                break;
              default:
                throw(new Exception(rowType.ToString() + " not handled."));
            }
          }
          excelDoc.Write("</Row>");
        }
        excelDoc.Write("</Table>");
        excelDoc.Write(" </Worksheet>");
        excelDoc.Write(endExcelXML);
        excelDoc.Close();
    }

Note

To see what generated, just pass the file name with a .txt extension. For Excel format, the file name will be .xls. For XML format, the file name will be .xml.

The Export Routine does have one side effect! (if anyone can figure out a solution to this, it would be greatly appreciated). The file is saved as an .XLS file, but it technically is still an XML file. This little nuance makes the file size larger then it really should be. A quick fix to this is to just do File Save As.... after the file has been exported. When you do the Save As in Excel, it will reconstruct it as a "real" Excel file, and it will bring the file size down to what it should be.

Feed Back is always welcome.

Simple as that...Enjoy.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralSaving it as an Microsoft Office Excel Workbook Pin
JimmyJamz15-Apr-09 4:13
JimmyJamz15-Apr-09 4:13 
GeneralRe: Saving it as an Microsoft Office Excel Workbook Pin
Xodiak15-Apr-09 5:42
Xodiak15-Apr-09 5:42 
QuestionTime Calulation Pin
Nishant Pathak1-Apr-09 3:19
professionalNishant Pathak1-Apr-09 3:19 
AnswerRe: Time Calulation Pin
Xodiak1-Apr-09 4:50
Xodiak1-Apr-09 4:50 
GeneralExporting multiple datatable to multiple sheets in excel using C# Pin
venkateswaran0223-Feb-09 20:20
venkateswaran0223-Feb-09 20:20 
GeneralRe: Exporting multiple datatable to multiple sheets in excel using C# Pin
Xodiak24-Feb-09 4:47
Xodiak24-Feb-09 4:47 
GeneralRe: Exporting multiple datatable to multiple sheets in excel using C# Pin
vikram_asv4-Mar-09 7:17
vikram_asv4-Mar-09 7:17 
GeneralVariation on a theme Pin
Ger Rietman11-Feb-09 21:59
Ger Rietman11-Feb-09 21:59 
Good article! I made a variation on the vb.net version of the previous poster, so it does no longer support styles, which makes it simpler and more general IMHO. I turned it into a class and compiled it as dll. I also extended it with the possibility to include a sheet name, where the sheetname becomes something like "Sheetn_xxxxxx" where xxxxx represents the name you give as parameter and n the sheet order number.
Here is the call:

Dim myStream As New System.IO.StringWriter()
Dim dstox As New DatasetToExcel.DatasetToExcel
myStream = dstox.exportToExcel(dsTotalSalesJanuary, "Jan2009")
myStream.Close()

And here is the code:

Imports System.Text

Public Class DatasetToExcel

Public Function exportToExcel(ByVal source As DataSet, ByVal sheetName As String)

Dim excelDoc As New System.IO.StringWriter()

Dim sb As New StringBuilder

sb.Append("&lt;?xml version=""1.0""?&gt;")
sb.Append(vbCrLf)
sb.Append("&lt;?mso-application progid=""Excel.Sheet""?&gt;")
sb.Append(vbCrLf)
sb.Append("&lt;Workbook xmlns=""urn:schemas-microsoft-com:office:spreadsheet""")
sb.Append(vbCrLf)
sb.Append("xmlnsBlush | :O =""urn:schemas-microsoft-com:office:office""")
sb.Append(vbCrLf)
sb.Append("xmlns:x=""urn:schemas-microsoft-com:office:excel""")
sb.Append(vbCrLf)
sb.Append("xmlns:ss=""urn:schemas-microsoft-com:office:spreadsheet""")
sb.Append(vbCrLf)
sb.Append("xmlns:html=""http://www.w3.org/TR/REC-html40""&gt;")
sb.Append(vbCrLf)

Dim startExcelXML As String = sb.ToString

Const endExcelXML As String = "&lt;/Workbook&gt;"

Dim rowCount As Integer = 0
Dim sheetCount As Integer = 1

'WORKSHEET AND TABLE TAGS:
excelDoc.Write(startExcelXML)
excelDoc.Write("&lt;Worksheet ss:Name=""Sheet" &amp; sheetCount &amp; "_" &amp; sheetName &amp; """&gt;")
excelDoc.Write("&lt;Table&gt;")

'CREATE HEADER ROW:
excelDoc.Write("&lt;Row&gt;")
For x As Integer = 0 To source.Tables(0).Columns.Count - 1
excelDoc.Write("&lt;Cell&gt;&lt;Data ss:Type=""String""&gt;")
excelDoc.Write(source.Tables(0).Columns(x).ColumnName)
excelDoc.Write("&lt;/Data&gt;&lt;/Cell&gt;")
Next
excelDoc.Write("&lt;/Row&gt;")

'INSERT THE DATA ROWS:
For Each x As DataRow In source.Tables(0).Rows
rowCount += 1
If rowCount = 64000 Then
rowCount = 0
sheetCount += 1
excelDoc.Write("&lt;/Table&gt;")
excelDoc.Write(" &lt;/Worksheet&gt;")
excelDoc.Write("&lt;Worksheet ss:Name=""Sheet" &amp; sheetCount &amp; """&gt;")
excelDoc.Write("&lt;Table&gt;")
End If
excelDoc.Write("&lt;Row&gt;")

For y As Integer = 0 To source.Tables(0).Columns.Count - 1
Dim rowType As System.Type
rowType = x(y).GetType()
Select Case rowType.ToString()
Case "System.String"
Dim XMLstring As String = x(y).ToString()
XMLstring = XMLstring.Trim()
XMLstring = XMLstring.Replace("&amp;", "&amp;")
XMLstring = XMLstring.Replace("&gt;", "&gt;")
XMLstring = XMLstring.Replace("&lt;", "&lt;")
excelDoc.Write("&lt;Cell&gt;" &amp; "&lt;Data ss:Type=""String""&gt;")
excelDoc.Write(XMLstring)
excelDoc.Write("&lt;/Data&gt;&lt;/Cell&gt;")
Exit Select
Case "System.DateTime"
'Excel has a specific Date Format of YYYY-MM-DD followed by
'the letter 'T' then hh:mm:sss.lll Example 2005-01-31T24:01:21.000
'The Following Code puts the date stored in XMLDate
'to the format above
Dim XMLDate As DateTime = DirectCast(x(y), DateTime)
Dim XMLDatetoString As String = ""
'Excel Converted Date
XMLDatetoString = XMLDate.Year.ToString() &amp; "-" &amp; (If(XMLDate.Month &lt; 10, "0" &amp; XMLDate.Month.ToString(), XMLDate.Month.ToString())) &amp; "-" &amp; (If(XMLDate.Day &lt; 10, "0" &amp; XMLDate.Day.ToString(), XMLDate.Day.ToString())) &amp; "T" &amp; (If(XMLDate.Hour &lt; 10, "0" &amp; XMLDate.Hour.ToString(), XMLDate.Hour.ToString())) &amp; ":" &amp; (If(XMLDate.Minute &lt; 10, "0" &amp; XMLDate.Minute.ToString(), XMLDate.Minute.ToString())) &amp; ":" &amp; (If(XMLDate.Second &lt; 10, "0" &amp; XMLDate.Second.ToString(), XMLDate.Second.ToString())) &amp; ".000"
excelDoc.Write("&lt;Cell ss:StyleID=""DateLiteral""&gt;" &amp; "&lt;Data ss:Type=""DateTime""&gt;")
'excelDoc.Write("&lt;Cell&gt;" &amp; "&lt;Data ss:Type=""DateTime""&gt;")
excelDoc.Write(XMLDatetoString)
excelDoc.Write("&lt;/Data&gt;&lt;/Cell&gt;")
Exit Select
Case "System.Boolean"
excelDoc.Write("&lt;Cell&gt;" &amp; "&lt;Data ss:Type=""String""&gt;")
excelDoc.Write(x(y).ToString())
excelDoc.Write("&lt;/Data&gt;&lt;/Cell&gt;")
Exit Select
Case "System.Int16", "System.Int32", "System.Int64", "System.Byte"
excelDoc.Write("&lt;Cell&gt;" &amp; "&lt;Data ss:Type=""Number""&gt;")
excelDoc.Write(x(y).ToString())
excelDoc.Write("&lt;/Data&gt;&lt;/Cell&gt;")
Exit Select
Case "System.Decimal", "System.Double"
excelDoc.Write("&lt;Cell&gt;" &amp; "&lt;Data ss:Type=""Number""&gt;")
excelDoc.Write(Replace(x(y).ToString(), ",", "."))
excelDoc.Write("&lt;/Data&gt;&lt;/Cell&gt;")
Exit Select
Case "System.DBNull"
excelDoc.Write("&lt;Cell&gt;" &amp; "&lt;Data ss:Type=""String""&gt;")
excelDoc.Write("")
excelDoc.Write("&lt;/Data&gt;&lt;/Cell&gt;")
Exit Select
Case Else
Throw (New Exception(rowType.ToString() &amp; " not handled."))
End Select
Next
excelDoc.Write("&lt;/Row&gt;")
Next

'INSERT THE CLOSING TAGS:
excelDoc.Write("&lt;/Table&gt;")
excelDoc.Write(" &lt;/Worksheet&gt;")
excelDoc.Write(endExcelXML)
excelDoc.Close()

'RETURN THE DOCUMENT AS STRINGWRITER
Return excelDoc

End Function

End Class

Thank you Xodiak for an excellent article!
GeneralConverted it to VB.NET and then changed from StreamWriter to StringWriter Pin
Member 161590819-Nov-08 4:20
Member 161590819-Nov-08 4:20 
QuestionVB ? Pin
Fernando Velazco29-Sep-08 13:46
Fernando Velazco29-Sep-08 13:46 
GeneralMultiple DataTables inside DataSet Pin
JLuterek3-Sep-08 4:41
JLuterek3-Sep-08 4:41 
Questionhow to convert datagridview content to dataset? Pin
Saeed.3949-May-08 21:31
Saeed.3949-May-08 21:31 
AnswerRe: how to convert datagridview content to dataset? Pin
Xodiak9-Jun-08 4:04
Xodiak9-Jun-08 4:04 
QuestionExcel Dates with a year less than 1900 Pin
jeff0077-Mar-08 3:39
jeff0077-Mar-08 3:39 
GeneralRe: Excel Dates with a year less than 1900 Pin
jeff00710-Mar-08 3:07
jeff00710-Mar-08 3:07 
AnswerRe: Excel Dates with a year less than 1900 Pin
ahmed-itani9-Jun-08 3:45
professionalahmed-itani9-Jun-08 3:45 
GeneralExport for Office 2000 Pin
Member 383654719-Feb-08 14:36
Member 383654719-Feb-08 14:36 
GeneralRe: Export for Office 2000 Pin
Member 383654725-Feb-08 9:24
Member 383654725-Feb-08 9:24 
GeneralRe: Export for Office 2000 Pin
Xodiak26-Feb-08 16:49
Xodiak26-Feb-08 16:49 
GeneralRe: Export for Office 2000 Pin
ahmed-itani9-Jun-08 3:53
professionalahmed-itani9-Jun-08 3:53 
GeneralFill with color a Cell Pin
Member 38365478-Feb-08 7:41
Member 38365478-Feb-08 7:41 
GeneralRe: Fill with color a Cell Pin
Xodiak8-Feb-08 8:47
Xodiak8-Feb-08 8:47 
GeneralWorks great Pin
GutterBoy23-Jan-08 7:23
GutterBoy23-Jan-08 7:23 
GeneralGread Article!! [modified] Pin
NingNing28-Nov-07 19:23
NingNing28-Nov-07 19:23 
GeneralReg exporting data Pin
girishganeshan2-Oct-07 23:53
girishganeshan2-Oct-07 23:53 

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

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