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 100459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:35:01+00:00 2026-05-11T00:35:01+00:00

I need a solution to export a dataset to an excel file without any

  • 0

I need a solution to export a dataset to an excel file without any asp code (HttpResonpsne…) but i did not find a good example to do this…

Best thanks in advance

  • 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. 2026-05-11T00:35:02+00:00Added an answer on May 11, 2026 at 12:35 am

    I’ve created a class that exports a DataGridView or DataTable to an Excel file. You can probably change it a bit to make it use your DataSet instead (iterating through the DataTables in it). It also does some basic formatting which you could also extend.

    To use it, simply call ExcelExport, and specify a filename and whether to open the file automatically or not after exporting. I also could have made them extension methods, but I didn’t. Feel free to.

    Note that Excel files can be saved as a glorified XML document and this makes use of that.

    EDIT: This used to use a vanilla StreamWriter, but as pointed out, things would not be escaped correctly in many cases. Now it uses a XmlWriter, which will do the escaping for you.

    The ExcelWriter class wraps an XmlWriter. I haven’t bothered, but you might want to do a bit more error checking to make sure you can’t write cell data before starting a row, and such. The code is below.

    public class ExcelWriter : IDisposable {     private XmlWriter _writer;      public enum CellStyle { General, Number, Currency, DateTime, ShortDate };      public void WriteStartDocument()     {         if (_writer == null) throw new InvalidOperationException('Cannot write after closing.');          _writer.WriteProcessingInstruction('mso-application', 'progid=\'Excel.Sheet\'');         _writer.WriteStartElement('ss', 'Workbook', 'urn:schemas-microsoft-com:office:spreadsheet');          WriteExcelStyles();    }      public void WriteEndDocument()     {         if (_writer == null) throw new InvalidOperationException('Cannot write after closing.');          _writer.WriteEndElement();     }      private void WriteExcelStyleElement(CellStyle style)     {         _writer.WriteStartElement('Style', 'urn:schemas-microsoft-com:office:spreadsheet');         _writer.WriteAttributeString('ID', 'urn:schemas-microsoft-com:office:spreadsheet', style.ToString());         _writer.WriteEndElement();     }      private void WriteExcelStyleElement(CellStyle style, string NumberFormat)     {         _writer.WriteStartElement('Style', 'urn:schemas-microsoft-com:office:spreadsheet');          _writer.WriteAttributeString('ID', 'urn:schemas-microsoft-com:office:spreadsheet', style.ToString());         _writer.WriteStartElement('NumberFormat', 'urn:schemas-microsoft-com:office:spreadsheet');         _writer.WriteAttributeString('Format', 'urn:schemas-microsoft-com:office:spreadsheet', NumberFormat);         _writer.WriteEndElement();          _writer.WriteEndElement();      }      private void WriteExcelStyles()     {         _writer.WriteStartElement('Styles', 'urn:schemas-microsoft-com:office:spreadsheet');          WriteExcelStyleElement(CellStyle.General);         WriteExcelStyleElement(CellStyle.Number, 'General Number');         WriteExcelStyleElement(CellStyle.DateTime, 'General Date');         WriteExcelStyleElement(CellStyle.Currency, 'Currency');         WriteExcelStyleElement(CellStyle.ShortDate, 'Short Date');          _writer.WriteEndElement();     }      public void WriteStartWorksheet(string name)     {         if (_writer == null) throw new InvalidOperationException('Cannot write after closing.');          _writer.WriteStartElement('Worksheet', 'urn:schemas-microsoft-com:office:spreadsheet');         _writer.WriteAttributeString('Name', 'urn:schemas-microsoft-com:office:spreadsheet', name);         _writer.WriteStartElement('Table', 'urn:schemas-microsoft-com:office:spreadsheet');     }      public void WriteEndWorksheet()     {         if (_writer == null) throw new InvalidOperationException('Cannot write after closing.');          _writer.WriteEndElement();         _writer.WriteEndElement();     }      public ExcelWriter(string outputFileName)     {         XmlWriterSettings settings = new XmlWriterSettings();         settings.Indent = true;         _writer = XmlWriter.Create(outputFileName, settings);     }      public void Close()     {         if (_writer == null) throw new InvalidOperationException('Already closed.');          _writer.Close();         _writer = null;     }      public void WriteExcelColumnDefinition(int columnWidth)     {         if (_writer == null) throw new InvalidOperationException('Cannot write after closing.');          _writer.WriteStartElement('Column', 'urn:schemas-microsoft-com:office:spreadsheet');         _writer.WriteStartAttribute('Width', 'urn:schemas-microsoft-com:office:spreadsheet');         _writer.WriteValue(columnWidth);         _writer.WriteEndAttribute();         _writer.WriteEndElement();     }      public void WriteExcelUnstyledCell(string value)     {         if (_writer == null) throw new InvalidOperationException('Cannot write after closing.');          _writer.WriteStartElement('Cell', 'urn:schemas-microsoft-com:office:spreadsheet');         _writer.WriteStartElement('Data', 'urn:schemas-microsoft-com:office:spreadsheet');         _writer.WriteAttributeString('Type', 'urn:schemas-microsoft-com:office:spreadsheet', 'String');         _writer.WriteValue(value);         _writer.WriteEndElement();         _writer.WriteEndElement();     }      public void WriteStartRow()     {         if (_writer == null) throw new InvalidOperationException('Cannot write after closing.');          _writer.WriteStartElement('Row', 'urn:schemas-microsoft-com:office:spreadsheet');     }      public void WriteEndRow()     {         if (_writer == null) throw new InvalidOperationException('Cannot write after closing.');          _writer.WriteEndElement();     }      public void WriteExcelStyledCell(object value, CellStyle style)     {         if (_writer == null) throw new InvalidOperationException('Cannot write after closing.');          _writer.WriteStartElement('Cell', 'urn:schemas-microsoft-com:office:spreadsheet');         _writer.WriteAttributeString('StyleID', 'urn:schemas-microsoft-com:office:spreadsheet', style.ToString());         _writer.WriteStartElement('Data', 'urn:schemas-microsoft-com:office:spreadsheet');         switch (style)         {             case CellStyle.General:                 _writer.WriteAttributeString('Type', 'urn:schemas-microsoft-com:office:spreadsheet', 'String');                 break;             case CellStyle.Number:             case CellStyle.Currency:                 _writer.WriteAttributeString('Type', 'urn:schemas-microsoft-com:office:spreadsheet', 'Number');                 break;             case CellStyle.ShortDate:             case CellStyle.DateTime:                 _writer.WriteAttributeString('Type', 'urn:schemas-microsoft-com:office:spreadsheet', 'DateTime');                 break;         }         _writer.WriteValue(value);         //  tag += String.Format('{1}\'><ss:Data ss:Type=\'DateTime\'>{0:yyyy\\-MM\\-dd\\THH\\:mm\\:ss\\.fff}</ss:Data>', value,          _writer.WriteEndElement();         _writer.WriteEndElement();     }      public void WriteExcelAutoStyledCell(object value)     {         if (_writer == null) throw new InvalidOperationException('Cannot write after closing.');          //write the <ss:Cell> and <ss:Data> tags for something         if (value is Int16 || value is Int32 || value is Int64 || value is SByte ||             value is UInt16 || value is UInt32 || value is UInt64 || value is Byte)         {             WriteExcelStyledCell(value, CellStyle.Number);         }         else if (value is Single || value is Double || value is Decimal) //we'll assume it's a currency         {             WriteExcelStyledCell(value, CellStyle.Currency);         }         else if (value is DateTime)         {             //check if there's no time information and use the appropriate style             WriteExcelStyledCell(value, ((DateTime)value).TimeOfDay.CompareTo(new TimeSpan(0, 0, 0, 0, 0)) == 0 ? CellStyle.ShortDate : CellStyle.DateTime);         }         else         {             WriteExcelStyledCell(value, CellStyle.General);         }     }      #region IDisposable Members      public void Dispose()     {         if (_writer == null)             return;          _writer.Close();         _writer = null;     }      #endregion } 

    Then you can export your DataTable using the following:

    public static void ExcelExport(DataTable data, String fileName, bool openAfter) {     //export a DataTable to Excel     DialogResult retry = DialogResult.Retry;      while (retry == DialogResult.Retry)     {         try         {             using (ExcelWriter writer = new ExcelWriter(fileName))             {                 writer.WriteStartDocument();                  // Write the worksheet contents                 writer.WriteStartWorksheet('Sheet1');                  //Write header row                 writer.WriteStartRow();                 foreach (DataColumn col in data.Columns)                     writer.WriteExcelUnstyledCell(col.Caption);                 writer.WriteEndRow();                  //write data                 foreach (DataRow row in data.Rows)                 {                     writer.WriteStartRow();                     foreach (object o in row.ItemArray)                     {                         writer.WriteExcelAutoStyledCell(o);                     }                     writer.WriteEndRow();                 }                  // Close up the document                 writer.WriteEndWorksheet();                 writer.WriteEndDocument();                 writer.Close();                 if (openAfter)                     OpenFile(fileName);                 retry = DialogResult.Cancel;             }         }         catch (Exception myException)         {             retry = MessageBox.Show(myException.Message, 'Excel Export', MessageBoxButtons.RetryCancel, MessageBoxIcon.Asterisk);         }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 57k
  • Answers 57k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You could create a web service on the target server… May 11, 2026 at 8:32 am
  • added an answer ha, I just realized that the web server (which is… May 11, 2026 at 8:32 am
  • added an answer You can implement this kind of function by creating a… May 11, 2026 at 8:32 am

Top Members

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

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.