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. DataGrid to Excel in Windows app.

DataGrid to Excel in Windows app.

Scheduled Pinned Locked Moved C#
tutorial
4 Posts 3 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
    SysJey
    wrote on last edited by
    #1

    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

    C A 2 Replies Last reply
    0
    • S SysJey

      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

      C Offline
      C Offline
      Chetan Ranpariya
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • S SysJey

        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

        A Offline
        A Offline
        Azoli
        wrote on last edited by
        #3

        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(

        A 1 Reply Last reply
        0
        • A Azoli

          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(

          A Offline
          A Offline
          Azoli
          wrote on last edited by
          #4

          private void ExportData(string parType) { if (parType.ToUpper() == "XLS") { //Parse DataTable AzoControls.MyDataExport.AzoDataExportToXLS ExportXLS = new AzoControls.MyDataExport.AzoDataExportToXLS(table); ExportXLS.ExportToXLS(); } } grandy

          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