Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to Export Contents of DataTable and DataGridView in Excel without using Excel object.

How to Export Contents of DataTable and DataGridView in Excel without using Excel object.

Scheduled Pinned Locked Moved C#
htmlcsstutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sharad Pyakurel
    wrote on last edited by
    #1

    I have written code to export content of DatagridView to Excel using Strem. It works but in some case some texts are found missing. And Numeric Datas are not formatted(i.e. while adding or calculating from exported data, unable to perform calculation. My code is like this: public class ExcelReport { #region private string Heading1; private DataGridView Grid; private int ReportType; private int StartCol = 0; private int StartRow = 0; #endregion #region Constructors /// <summary> /// Report Type:Excel=1,HTML=2,Word=3 and All=4 /// </summary> /// <param name="_heading1"></param> /// <param name="_grid"></param> public ExcelReport(string _heading1,int _reportType, DataGridView _grid) { this.Heading1 = _heading1; this.ReportType = _reportType; this.Grid = _grid; WriteReport(); } #endregion #region Main Function For Exporting private void WriteReport() { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); if (ReportType == 1) { saveFileDialog1.Filter = "Excel Worksheet files|*.xls"; } else if (ReportType == 2) { saveFileDialog1.Filter = "HTML Files|*.htm"; } else if (ReportType == 3) { saveFileDialog1.Filter = "Word Files|*.doc"; } else if (ReportType == 4) { saveFileDialog1.Filter = "Excel Worksheet files|*.xls|HTML Files|*.html|Word Files|*.doc|All Files|*.*"; } //Exporting To Different Formates using Stream if (saveFileDialog1.ShowDialog() == DialogResult.OK) { FileInfo fi = new FileInfo(saveFileDialog1.FileName); StreamWriter sw = fi.CreateText(); sw.WriteLine("<table bgcolor='#FFFFFF' border=1 width='100%'>"); sw.Write("<tr>"); foreach (DataGridViewColumn col in Grid.Columns) { string FontName; if (Grid.ColumnHeadersDefaultCellStyle.Font.Name.ToString() != null) { FontName = Grid.ColumnHeadersDefaultCellStyle.Font.Name.ToString(); } else {

    I S 2 Replies Last reply
    0
    • S sharad Pyakurel

      I have written code to export content of DatagridView to Excel using Strem. It works but in some case some texts are found missing. And Numeric Datas are not formatted(i.e. while adding or calculating from exported data, unable to perform calculation. My code is like this: public class ExcelReport { #region private string Heading1; private DataGridView Grid; private int ReportType; private int StartCol = 0; private int StartRow = 0; #endregion #region Constructors /// <summary> /// Report Type:Excel=1,HTML=2,Word=3 and All=4 /// </summary> /// <param name="_heading1"></param> /// <param name="_grid"></param> public ExcelReport(string _heading1,int _reportType, DataGridView _grid) { this.Heading1 = _heading1; this.ReportType = _reportType; this.Grid = _grid; WriteReport(); } #endregion #region Main Function For Exporting private void WriteReport() { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); if (ReportType == 1) { saveFileDialog1.Filter = "Excel Worksheet files|*.xls"; } else if (ReportType == 2) { saveFileDialog1.Filter = "HTML Files|*.htm"; } else if (ReportType == 3) { saveFileDialog1.Filter = "Word Files|*.doc"; } else if (ReportType == 4) { saveFileDialog1.Filter = "Excel Worksheet files|*.xls|HTML Files|*.html|Word Files|*.doc|All Files|*.*"; } //Exporting To Different Formates using Stream if (saveFileDialog1.ShowDialog() == DialogResult.OK) { FileInfo fi = new FileInfo(saveFileDialog1.FileName); StreamWriter sw = fi.CreateText(); sw.WriteLine("<table bgcolor='#FFFFFF' border=1 width='100%'>"); sw.Write("<tr>"); foreach (DataGridViewColumn col in Grid.Columns) { string FontName; if (Grid.ColumnHeadersDefaultCellStyle.Font.Name.ToString() != null) { FontName = Grid.ColumnHeadersDefaultCellStyle.Font.Name.ToString(); } else {

      I Offline
      I Offline
      Infarkt
      wrote on last edited by
      #2

      try this http://www.codeplex.com/ExcelExportLib[^]

      1 Reply Last reply
      0
      • S sharad Pyakurel

        I have written code to export content of DatagridView to Excel using Strem. It works but in some case some texts are found missing. And Numeric Datas are not formatted(i.e. while adding or calculating from exported data, unable to perform calculation. My code is like this: public class ExcelReport { #region private string Heading1; private DataGridView Grid; private int ReportType; private int StartCol = 0; private int StartRow = 0; #endregion #region Constructors /// <summary> /// Report Type:Excel=1,HTML=2,Word=3 and All=4 /// </summary> /// <param name="_heading1"></param> /// <param name="_grid"></param> public ExcelReport(string _heading1,int _reportType, DataGridView _grid) { this.Heading1 = _heading1; this.ReportType = _reportType; this.Grid = _grid; WriteReport(); } #endregion #region Main Function For Exporting private void WriteReport() { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); if (ReportType == 1) { saveFileDialog1.Filter = "Excel Worksheet files|*.xls"; } else if (ReportType == 2) { saveFileDialog1.Filter = "HTML Files|*.htm"; } else if (ReportType == 3) { saveFileDialog1.Filter = "Word Files|*.doc"; } else if (ReportType == 4) { saveFileDialog1.Filter = "Excel Worksheet files|*.xls|HTML Files|*.html|Word Files|*.doc|All Files|*.*"; } //Exporting To Different Formates using Stream if (saveFileDialog1.ShowDialog() == DialogResult.OK) { FileInfo fi = new FileInfo(saveFileDialog1.FileName); StreamWriter sw = fi.CreateText(); sw.WriteLine("<table bgcolor='#FFFFFF' border=1 width='100%'>"); sw.Write("<tr>"); foreach (DataGridViewColumn col in Grid.Columns) { string FontName; if (Grid.ColumnHeadersDefaultCellStyle.Font.Name.ToString() != null) { FontName = Grid.ColumnHeadersDefaultCellStyle.Font.Name.ToString(); } else {

        S Offline
        S Offline
        sharad Pyakurel
        wrote on last edited by
        #3

        Thank you very much for your reply.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups