Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4345816
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:05:18+00:00 2026-05-21T12:05:18+00:00

I’m looking for a good way to take a set of objects and convert

  • 0

I’m looking for a good way to take a set of objects and convert them to Excel format. I used VSTO in the past, but it was not well documented and I was not happy with many of the naming conventions and breaking changes.

I thought about maybe doing an XSLT transform, but don’t see any clean way to get from there to Excel. I saw something about going XSLT > HTML > Excel, but I dunno…html is such a loose standard I don’t trust it to produce consistent data transformations.

What do you recommend?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T12:05:19+00:00Added an answer on May 21, 2026 at 12:05 pm

    I use the code that can be found on The Code Project website.

    It creates an xml file that Excel understands as an excel workbook. It’s quirky; when you open it, you get a message like this:

    the file you are trying to open is in
    a different format than specified by
    the file extension

    It opens fine though and you can then save it to a real excel format.

    Looking at the code, it doesn’t look like it’d take much to create multiple worksheets (which I think is what you wanted to do). 🙂

    Here’s the code if for some reason the link doesn’t load:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Reflection;
    using System.Collections;
    
    namespace myNamespace
    {
        public class IOProcess
        {
    
    
            //http://www.codeproject.com/KB/dotnet/ExportToExcel.aspx?df=100&forumid=146533&exp=0&select=1357703
            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();
            }
    
    
            #region Convert to Dataset
            /// <summary>
            /// This will take anything that implements the ICollection interface and convert
            /// it to a DataSet.
            /// </summary>
            /// <example>
            /// CollectiontoDataSet converter = new CollectionToDataSet<Letters[]>(letters);
            /// DataSet ds = converter.CreateDataSet();
            /// </example>
            /// <typeparam name="T"></typeparam>
            public class CollectionToDataSet<T> where T : System.Collections.ICollection
            {
                T _collection;
                public CollectionToDataSet(T list)
                {
                    _collection = list;
                }
    
                private PropertyInfo[] _propertyCollection = null;
                private PropertyInfo[] PropertyCollection
                {
                    get
                    {
                        if (_propertyCollection == null)
                        {
                            _propertyCollection = GetPropertyCollection();
                        }
                        return _propertyCollection;
                    }
                }
    
                private PropertyInfo[] GetPropertyCollection()
                {
                    if (_collection.Count > 0)
                    {
                        IEnumerator enumerator = _collection.GetEnumerator();
                        enumerator.MoveNext();
                        return enumerator.Current.GetType().GetProperties();
                    }
                    return null;
                }
    
                public DataSet CreateDataSet()
                {
                    DataSet ds = new DataSet("GridDataSet");
                    ds.Tables.Add(FillDataTable());
                    return ds;
                }
    
                private DataTable FillDataTable()
                {
                    IEnumerator enumerator = _collection.GetEnumerator();
                    DataTable dt = CreateDataTable();
                    while (enumerator.MoveNext())
                    {
                        dt.Rows.Add(FillDataRow(dt.NewRow(), enumerator.Current));
                    }
                    return dt;
                }
    
                private DataRow FillDataRow(DataRow dataRow, object p)
                {
                    foreach (PropertyInfo property in PropertyCollection)
                    {
                        dataRow[property.Name.ToString()] = property.GetValue(p, null);
                    }
                    return dataRow;
                }
    
                private DataTable CreateDataTable()
                {
                    DataTable dt = new DataTable("GridDataTable");
                    foreach (PropertyInfo property in PropertyCollection)
                    {
                        dt.Columns.Add(property.Name.ToString());
                    }
                    return dt;
                }
            }
            #endregion Convert to Dataset
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.