How to Export Contents of DataTable and DataGridView in Excel without using Excel object.
-
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 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 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 {
Thank you very much for your reply.