DataGrid to Excel in Windows app.
-
I have seen the guidelines(Code project - before 2 days) to export the data from datagrid to excel sheet in Web application. How to make up the same operation in windows application. Jey
You can use the csv concept. SqlConnection cn ; SqlCommand cmd ; string filename ; SqlDataReader dr ; System.Text.StringBuilder sb ; private void Button1_Click(object sender, System.EventArgs e) { cn=newSqlConnection("server=localhost;uid=sa;pwd=;database=northwind"); filename = "products.csv"; cmd = new SqlCommand("select * from products ", cn); cmd.Connection.Open(); dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); sb = new System.Text.StringBuilder(); //For field Names for(int i = 0 ;i< = dr.FieldCount - 1;i++) { if( i < (dr.FieldCount - 1) ) { sb.Append("\t" + dr.GetName(i) + "\t" + ","); } else { sb.Append("\t" + dr.GetName(i) + "\t" + "\n"); } } //For field Values while( dr.Read()) { for(int i = 0 ;i<= dr.FieldCount - 1;i++) { if( i < (dr.FieldCount - 1) ) { sb.Append("\t" + dr.GetValue(i) + "\t" + ","); } else { sb.Append("\t" + dr.GetValue(i) + "\t" + "\n"); } } } dr.Close(); cn.Close(); Response.ContentType = "Application/x-msexcel"; Response.AddHeader ("content-disposition", "attachment; filename=" + filename ) ; //Write the file directly to the HTTP output stream. Response.Write(sb.ToString()); Response.End(); } /**************************************************************************** private void btExport_Click(object sender, EventArgs e) { string strTemp = String.Empty; dst = new DataSet(); dst.Tables.Add("UserTable"); string strConnection = "Data Source=QUEST03;Initial Catalog=UserDatabase;User ID=hari; Password=shriji;"; SqlConnection conn = new SqlConnection(strConnection); conn.Open(); SqlCommand cmd = new SqlCommand("Select * From UserTable",conn); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); System.Text.StringBuilder sb = new System.Text.StringBuilder(); using (StreamWriter sw = new StreamWriter("E:/Hari/Problem_Import_Export/Problem1/t
-
I have seen the guidelines(Code project - before 2 days) to export the data from datagrid to excel sheet in Web application. How to make up the same operation in windows application. Jey
using System; using System.IO; using System.Data; using System.Windows.Forms; namespace AzoControls.MyDataExport { public class AzoDataExportToXLS { private int rowCount = 0; private int sheetCount = 1; private System.IO.StreamWriter excelDoc; const string endExcelXML = ""; const string startExcelXML = "\r\n\r\n \r\n " + "\r\n " + "<Alignment ss:Vertical=\"Bottom\"/>\r\n <Borders/>" + "\r\n <Font/>\r\n <Interior/>\r\n <NumberFormat/>" + "\r\n <Protection/>\r\n \r\n " + "\r\n <Font " + "x:Family=\"Swiss\" ss:Bold=\"1\"/>\r\n \r\n " + "\r\n <NumberFormat" + " ss:Format=\"@\"/>\r\n \r\n \r\n <NumberFormat " + "ss:Format=\"0.00\"/>\r\n \r\n " + "\r\n <NumberFormat " + "ss:Format=\"0\"/>\r\n \r\n \r\n <NumberFormat " + "ss:Format=\"dd/mm/yyyy;@\"/>\r\n \r\n " + "\r\n "; private DataTable dtXLS; private DataRow drXLS; public AzoDataExportToXLS(DataTable parTableName) { //Parameter DataTable Object this.dtXLS = parTableName; } public void ExportToXLS() { try { string strFileName = "File001"; SaveFileDialog saveDlg = new SaveFileDialog(); saveDlg.InitialDirectory = Path.GetDirectoryName(strFileName); saveDlg.FileName = Path.GetFileNameWithoutExtension(strFileName); saveDlg.AddExtension = true; saveDlg.Filter = "Windows Excel (*.xls)|*.xls"; if (saveDlg.ShowDialog() == DialogResult.OK) { try { this.exportToExcel(saveDlg.FileName.ToString()); //this.WriteDataToXLS(
-
using System; using System.IO; using System.Data; using System.Windows.Forms; namespace AzoControls.MyDataExport { public class AzoDataExportToXLS { private int rowCount = 0; private int sheetCount = 1; private System.IO.StreamWriter excelDoc; const string endExcelXML = ""; const string startExcelXML = "\r\n\r\n \r\n " + "\r\n " + "<Alignment ss:Vertical=\"Bottom\"/>\r\n <Borders/>" + "\r\n <Font/>\r\n <Interior/>\r\n <NumberFormat/>" + "\r\n <Protection/>\r\n \r\n " + "\r\n <Font " + "x:Family=\"Swiss\" ss:Bold=\"1\"/>\r\n \r\n " + "\r\n <NumberFormat" + " ss:Format=\"@\"/>\r\n \r\n \r\n <NumberFormat " + "ss:Format=\"0.00\"/>\r\n \r\n " + "\r\n <NumberFormat " + "ss:Format=\"0\"/>\r\n \r\n \r\n <NumberFormat " + "ss:Format=\"dd/mm/yyyy;@\"/>\r\n \r\n " + "\r\n "; private DataTable dtXLS; private DataRow drXLS; public AzoDataExportToXLS(DataTable parTableName) { //Parameter DataTable Object this.dtXLS = parTableName; } public void ExportToXLS() { try { string strFileName = "File001"; SaveFileDialog saveDlg = new SaveFileDialog(); saveDlg.InitialDirectory = Path.GetDirectoryName(strFileName); saveDlg.FileName = Path.GetFileNameWithoutExtension(strFileName); saveDlg.AddExtension = true; saveDlg.Filter = "Windows Excel (*.xls)|*.xls"; if (saveDlg.ShowDialog() == DialogResult.OK) { try { this.exportToExcel(saveDlg.FileName.ToString()); //this.WriteDataToXLS(