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 DataGrid from different dynamically created tabpages to multiple worksheets of an Excel spreasheet

Export DataGrid from different dynamically created tabpages to multiple worksheets of an Excel spreasheet

Scheduled Pinned Locked Moved C#
helpcomannouncement
1 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.
  • W Offline
    W Offline
    Walaza
    wrote on last edited by
    #1

    The code I have below only takes data from one dynamically created tabpage (the default)‘s datagrid and exports it to a spreadsheet. I would like the application to take the data from all the dynamically created tabpages’ datagrids and export the data to excel and create a new worksheet for each tabpage as it is exporting. Please help. Microsoft.Office.Interop.Excel._Application _appClass = new ApplicationClass(); _appClass.Visible = true; _appClass.Caption = "Exported ECS Errors"; Microsoft.Office.Interop.Excel.Workbook wbk = _appClass.Workbooks.Add(XlWBATemplate.xlWBATWorksheet); Worksheet obj = (Worksheet)wbk.ActiveSheet; // get the collection of sheets in the workbook Microsoft.Office.Interop.Excel.Sheets sheets = wbk.Worksheets; Range r = (Range)obj.Cells; try { //Load the Headers and make them BOLD for (int i = 0; i < ((DataGridView)this.dgOutput).Columns.Count; i++) { r[1, i + 1] = ((DataGridView)this.dgOutput).Columns[i].HeaderText.ToString(); ((Range)r.Cells[1, i + 1]).Font.Bold = true; } //Load the data from the datagrid to the new spreadsheet for (int i = 1; i < ((DataGridView)this.dgOutput).Rows.Count - 1; i++) { for (int j = 0; j < ((DataGridView)this.dgOutput).Columns.Count; j++) { r[i + 1, j + 1] = ((DataGridView)this.dgOutput).Rows[i].Cells[j].Value.ToString(); //Do the colors if (((DataGridView)this.dgOutput).Rows[i].Cells[j].Value.ToString() == "Update OK") { ((Range)r.Cells[i + 1, 1]).Font.Color = Color.Green.G; } } } } catch (Exception EX) //user closes the Excel file while it is loading data from the datagrid { MessageBox.Show(EX.Message + "The Excel file was closed while loading data from the datagrid.", "Error : Loading file!!", MessageBoxButtons.OK, MessageBoxIcon.Error); }

    Mvelo Walaza Developer Telkom SA

    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