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 C# Windows Datagridview to excel

Export C# Windows Datagridview to excel

Scheduled Pinned Locked Moved C#
csharpwpfcom
3 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
    sumit7034
    wrote on last edited by
    #1

    Hi I want to export Datagridview to excel. I have formatted my datagridview such that he negative values are shown as red, some values are bold and other formatting styles are applied. I want when I export it to Excel then it export with same formatted type. Currently I Am using the below function to export. But unable to export with formatting. private void exportAsExcel() { Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); // creating new WorkBook within Excel application Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); // creating new Excelsheet in workbook Microsoft.Office.Interop.Excel._Worksheet worksheet = null; // see the excel sheet behind the program //Funny app.Visible = true; // get the reference of first sheet. By default its name is Sheet1. // store its reference to worksheet try { //Fixed:(Microsoft.Office.Interop.Excel.Worksheet) worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"]; worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet; // changing the name of active sheet worksheet.Name = "Sheet1"; // storing header part in Excel for (int i = 1; i < dgvDetails.Columns.Count + 1; i++) { worksheet.Cells[1, i] = dgvDetails.Columns[i - 1].HeaderText; } worksheet.get_Range("A1", "AZ1").Font.Bold = true; worksheet.get_Range("A1", "A" + (dgvDetails.RowCount + 2).ToString()).Font.Bold = true; // storing Each row and column value to excel sheet for (int i = 0; i < dgvDetails.Rows.Count; i++) { for (int j = 0; j < dgvDetails.Columns.Count; j++) { worksheet.Cells[i + 2, j + 1] = dgvDetails.Rows[i].Cells[j].Value.ToString(); } } } catch (System.Exception ex) { } finally { app.Quit(); workbook = null; app = null; } } Thanks in advance

    R A 2 Replies Last reply
    0
    • S sumit7034

      Hi I want to export Datagridview to excel. I have formatted my datagridview such that he negative values are shown as red, some values are bold and other formatting styles are applied. I want when I export it to Excel then it export with same formatted type. Currently I Am using the below function to export. But unable to export with formatting. private void exportAsExcel() { Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); // creating new WorkBook within Excel application Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); // creating new Excelsheet in workbook Microsoft.Office.Interop.Excel._Worksheet worksheet = null; // see the excel sheet behind the program //Funny app.Visible = true; // get the reference of first sheet. By default its name is Sheet1. // store its reference to worksheet try { //Fixed:(Microsoft.Office.Interop.Excel.Worksheet) worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"]; worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet; // changing the name of active sheet worksheet.Name = "Sheet1"; // storing header part in Excel for (int i = 1; i < dgvDetails.Columns.Count + 1; i++) { worksheet.Cells[1, i] = dgvDetails.Columns[i - 1].HeaderText; } worksheet.get_Range("A1", "AZ1").Font.Bold = true; worksheet.get_Range("A1", "A" + (dgvDetails.RowCount + 2).ToString()).Font.Bold = true; // storing Each row and column value to excel sheet for (int i = 0; i < dgvDetails.Rows.Count; i++) { for (int j = 0; j < dgvDetails.Columns.Count; j++) { worksheet.Cells[i + 2, j + 1] = dgvDetails.Rows[i].Cells[j].Value.ToString(); } } } catch (System.Exception ex) { } finally { app.Quit(); workbook = null; app = null; } } Thanks in advance

      R Offline
      R Offline
      Ravi Sant
      wrote on last edited by
      #2

      Please format your code with < Pre > tag first. For solution, Please refer to this article: How to export GridView to Excel, render cells, add WordArt, and fill a custom template[^]

      // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

      1 Reply Last reply
      0
      • S sumit7034

        Hi I want to export Datagridview to excel. I have formatted my datagridview such that he negative values are shown as red, some values are bold and other formatting styles are applied. I want when I export it to Excel then it export with same formatted type. Currently I Am using the below function to export. But unable to export with formatting. private void exportAsExcel() { Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); // creating new WorkBook within Excel application Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); // creating new Excelsheet in workbook Microsoft.Office.Interop.Excel._Worksheet worksheet = null; // see the excel sheet behind the program //Funny app.Visible = true; // get the reference of first sheet. By default its name is Sheet1. // store its reference to worksheet try { //Fixed:(Microsoft.Office.Interop.Excel.Worksheet) worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"]; worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet; // changing the name of active sheet worksheet.Name = "Sheet1"; // storing header part in Excel for (int i = 1; i < dgvDetails.Columns.Count + 1; i++) { worksheet.Cells[1, i] = dgvDetails.Columns[i - 1].HeaderText; } worksheet.get_Range("A1", "AZ1").Font.Bold = true; worksheet.get_Range("A1", "A" + (dgvDetails.RowCount + 2).ToString()).Font.Bold = true; // storing Each row and column value to excel sheet for (int i = 0; i < dgvDetails.Rows.Count; i++) { for (int j = 0; j < dgvDetails.Columns.Count; j++) { worksheet.Cells[i + 2, j + 1] = dgvDetails.Rows[i].Cells[j].Value.ToString(); } } } catch (System.Exception ex) { } finally { app.Quit(); workbook = null; app = null; } } Thanks in advance

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

        try this :) http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/27a9002e-9d53-440d-9750-e6c64406fdd6[^]

        -ambarish changanacherry-

        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