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 columns from datagridview to Excel

Export columns from datagridview to Excel

Scheduled Pinned Locked Moved C#
cssdatabasequestion
2 Posts 1 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
    simsen
    wrote on last edited by
    #1

    Hi, I have found som code on the Internet, which should export my datagridview to Excel. It also do that BUT it only export 4 of my 5 columns to excel (it's column number 4 it doesn't export which is Produkter.PrisExMoms from the database). The code I use to export is: #region Fakturalinier int rIndex = 12; foreach (DataRow row in dtOrdrelinier.Rows) { rIndex++; int cIndex = 0; foreach (DataColumn col in dtOrdrelinier.Columns) { cIndex++; xlApp.Cells[rIndex + 1, cIndex] = row[col.ColumnName].ToString(); } } } #endregion The code where I put the 5 columns into my datagridview: #region FelterIDataGridView // Databinding for the Grid da2 = new OleDbDataAdapter("SELECT Ordre.Antal, Ordre.VareNr, Produkter.Varenavn, Produkter.PrisExMoms FROM Produkter INNER JOIN (Faktura INNER JOIN Ordre ON Faktura.FakturaID = Ordre.FakturaNr) ON Produkter.VareNr = Ordre.VareNr WHERE FakturaID=@fakid", cn); da2.SelectCommand.Parameters.Add("@fakid", OleDbType.Integer); da2.SelectCommand.Parameters["@fakid"].Value = fakid; //create datatable dtOrdrelinier = new DataTable(); //fill datatable da2.Fill(dtOrdrelinier); //create colum to hold the sum of other two colums DataColumn col = new DataColumn("RaekkeBeloeb"); col.DataType = typeof(System.Decimal); col.Expression = "Antal*PrisExMoms"; //add the colums dtOrdrelinier.Columns.Add(col); //bind datatable to grid view dgrOrdre.DataSource = dtOrdrelinier; #endregion Anyone who can see why the column 4 (Produkter.PrisExMoms from the database) doesn't export to excel with the other columns? Kind regards, simsen :-)

    S 1 Reply Last reply
    0
    • S simsen

      Hi, I have found som code on the Internet, which should export my datagridview to Excel. It also do that BUT it only export 4 of my 5 columns to excel (it's column number 4 it doesn't export which is Produkter.PrisExMoms from the database). The code I use to export is: #region Fakturalinier int rIndex = 12; foreach (DataRow row in dtOrdrelinier.Rows) { rIndex++; int cIndex = 0; foreach (DataColumn col in dtOrdrelinier.Columns) { cIndex++; xlApp.Cells[rIndex + 1, cIndex] = row[col.ColumnName].ToString(); } } } #endregion The code where I put the 5 columns into my datagridview: #region FelterIDataGridView // Databinding for the Grid da2 = new OleDbDataAdapter("SELECT Ordre.Antal, Ordre.VareNr, Produkter.Varenavn, Produkter.PrisExMoms FROM Produkter INNER JOIN (Faktura INNER JOIN Ordre ON Faktura.FakturaID = Ordre.FakturaNr) ON Produkter.VareNr = Ordre.VareNr WHERE FakturaID=@fakid", cn); da2.SelectCommand.Parameters.Add("@fakid", OleDbType.Integer); da2.SelectCommand.Parameters["@fakid"].Value = fakid; //create datatable dtOrdrelinier = new DataTable(); //fill datatable da2.Fill(dtOrdrelinier); //create colum to hold the sum of other two colums DataColumn col = new DataColumn("RaekkeBeloeb"); col.DataType = typeof(System.Decimal); col.Expression = "Antal*PrisExMoms"; //add the colums dtOrdrelinier.Columns.Add(col); //bind datatable to grid view dgrOrdre.DataSource = dtOrdrelinier; #endregion Anyone who can see why the column 4 (Produkter.PrisExMoms from the database) doesn't export to excel with the other columns? Kind regards, simsen :-)

      S Offline
      S Offline
      simsen
      wrote on last edited by
      #2

      I found out, that the problem didn't was in the code, but in excel, where I had merged 2 columns together in the rows, where the datagridview should be. I worked around the problem with the following code: #region Fakturalinier int rIndex = 12; foreach (DataRow row in dtOrdrelinier.Rows) { rIndex++; int cIndex = 0; foreach (DataColumn col in dtOrdrelinier.Columns) { cIndex++; if (cIndex == 4) { cIndex++; } xlApp.Cells[rIndex + 1, cIndex] = row[col.ColumnName].ToString(); } } #endregion simsen :-)

      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