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. Export data to datagerid

Export data to datagerid

Scheduled Pinned Locked Moved C#
help
2 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.
  • M Offline
    M Offline
    Mamphekgo
    wrote on last edited by
    #1

    hi.i need to export data from datagrid to excel.can you please help me or give me link that i can refer from it

    B 1 Reply Last reply
    0
    • M Mamphekgo

      hi.i need to export data from datagrid to excel.can you please help me or give me link that i can refer from it

      B Offline
      B Offline
      basumca
      wrote on last edited by
      #2

      public string Convert(DataTable oDataTable, string directoryPath, string fileName) { string fullpath = ""; if (directoryPath.Substring(directoryPath.Length - 1,1) == @"\" ||directoryPath.Substring(directoryPath.Length - 1,1) == "/") { fullpath = directoryPath + fileName; } else { fullpath = directoryPath + @"\" + fileName; } StreamWriter SW; SW=File.CreateText(fullpath); StringBuilder oStringBuilder = new StringBuilder(); /******************************************************************* * Start, Creating column header * *****************************************************************/ foreach(DataColumn oDataColumn in oDataTable.Columns) { oStringBuilder.Append(oDataColumn.ColumnName + ","); } SW.WriteLine(oStringBuilder.ToString().Substring(0,oStringBuilder.ToString().Length - 1)); oStringBuilder.Length = 0; /******************************************************************* * End, Creating column header * *****************************************************************/ /******************************************************************* * Start, Creating rows * *****************************************************************/ foreach(DataRow oDataRow in oDataTable.Rows) { foreach(DataColumn oDataColumn in oDataTable.Columns) { oStringBuilder.Append(oDataRow[oDataColumn.ColumnName] + ","); } SW.WriteLine(oStringBuilder.ToString().Substring(0,oStringBuilder.ToString().Length - 1)); oStringBuilder.Length = 0; } /******************************************************************* * End, Creating rows * *****************************************************************/ SW.Close(); return fullpath; } write the above code in a class file. and write the below code in your button click event string fileName = System.Guid.NewGuid().ToString().Replace("-","") + ".xls"; (new BLC.CSVConvertor()).Convert(Generatexlsheet() ,Server.MapPath("."),fileName); Response.Redirect(fileName); end of button click event //below is the code for Generatexlsheet : private DataTable Generatexlsheet() { String dbstr = BLC.Class1.Constring(); SqlConnection objconn = new SqlConnection(dbstr); objconn.Open(); // SqlDataReader sqldr = null; // // // SqlCommand cmd = new SqlCommand("select * from employees",objconn

      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