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. Adding data to a new worksheet in an existing workbook c#

Adding data to a new worksheet in an existing workbook c#

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

    Hey everyone. I've been scrounging around on here and finally figured out how to export the data to an excel document from my dataset, the only problem i'm having now is I can't add a new worksheet. I have a data set with that i've filled with three different tables and I need each table to go into a seperate worksheet (so the marketing guys can then use that for their photoshop stuff.) what I have so far is just adding different headers on the same sheet...

    Excel.ApplicationClass excel = new Excel.ApplicationClass();
    excel.Application.Workbooks.Add(true);
    System.Data.DataTable table = data1.Tables["table1"];

            int Collindex = 0;
            int rowIndex = 2;
            excel.Cells\[1, 1\] = "table 1";
            foreach (System.Data.DataColumn col in table.Columns)
            {
                Collindex ++;
                excel.Cells\[2, Collindex\] = col.ColumnName;
            }            
            foreach (DataRow row in table.Rows)
            {
                rowIndex++;
                progbar2.PerformStep();
                progbar2.Refresh();
                Collindex = 0;
                foreach (DataColumn col in table.Columns)
                {
                    Collindex ++;
                    excel.Cells\[rowIndex + 1, Collindex\] = row\[col.ColumnName\];
                }
            }
    
            progbar1.PerformStep();
            progbar1.Refresh();
            
            table.Clear();
            table = data1.Tables\["table2"\];
            progbar2.Value = 0;
            progbar2.Refresh();
            progbar2.Maximum = data1.Tables\["table2"\].Rows.Count;
            progbar2.Refresh();           
    
            rowIndex = rowIndex + 3;
            excel.Cells\[rowIndex, 1\] = "table 2";
            foreach (DataRow row in table.Rows)
            {
                rowIndex++;
                progbar2.PerformStep();
                progbar2.Refresh();
                Collindex = 0;
                foreach (DataColumn col in table.Columns)
                {
                    Collindex++;
                    excel.Cells\[rowIndex + 1, Collindex\] = row\[col.ColumnName\];
                }
            }
    
            progbar1.PerformStep();
            progbar1.Refresh();
    
            table.Clear();
            table = data1.Tables\["table3"\];
            progbar2.Value = 0;
            progbar2.Refresh();
            progbar2.Maximum = data1.Tables\["table3"\].Rows.Count;
            progbar2.Ref
    
    J 1 Reply Last reply
    0
    • C compninja25

      Hey everyone. I've been scrounging around on here and finally figured out how to export the data to an excel document from my dataset, the only problem i'm having now is I can't add a new worksheet. I have a data set with that i've filled with three different tables and I need each table to go into a seperate worksheet (so the marketing guys can then use that for their photoshop stuff.) what I have so far is just adding different headers on the same sheet...

      Excel.ApplicationClass excel = new Excel.ApplicationClass();
      excel.Application.Workbooks.Add(true);
      System.Data.DataTable table = data1.Tables["table1"];

              int Collindex = 0;
              int rowIndex = 2;
              excel.Cells\[1, 1\] = "table 1";
              foreach (System.Data.DataColumn col in table.Columns)
              {
                  Collindex ++;
                  excel.Cells\[2, Collindex\] = col.ColumnName;
              }            
              foreach (DataRow row in table.Rows)
              {
                  rowIndex++;
                  progbar2.PerformStep();
                  progbar2.Refresh();
                  Collindex = 0;
                  foreach (DataColumn col in table.Columns)
                  {
                      Collindex ++;
                      excel.Cells\[rowIndex + 1, Collindex\] = row\[col.ColumnName\];
                  }
              }
      
              progbar1.PerformStep();
              progbar1.Refresh();
              
              table.Clear();
              table = data1.Tables\["table2"\];
              progbar2.Value = 0;
              progbar2.Refresh();
              progbar2.Maximum = data1.Tables\["table2"\].Rows.Count;
              progbar2.Refresh();           
      
              rowIndex = rowIndex + 3;
              excel.Cells\[rowIndex, 1\] = "table 2";
              foreach (DataRow row in table.Rows)
              {
                  rowIndex++;
                  progbar2.PerformStep();
                  progbar2.Refresh();
                  Collindex = 0;
                  foreach (DataColumn col in table.Columns)
                  {
                      Collindex++;
                      excel.Cells\[rowIndex + 1, Collindex\] = row\[col.ColumnName\];
                  }
              }
      
              progbar1.PerformStep();
              progbar1.Refresh();
      
              table.Clear();
              table = data1.Tables\["table3"\];
              progbar2.Value = 0;
              progbar2.Refresh();
              progbar2.Maximum = data1.Tables\["table3"\].Rows.Count;
              progbar2.Ref
      
      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      MSDN How to add new worksheets to workbooks[^] That help?

      Life, family, faith: Give me a visit. From my latest post: "When Constantine severed the Hebrew origins of this faith in Messiah, a new religion was officially created. It is this religion that Orthodox Jews fear their relatives and friends becoming members of." Judah Himango

      C 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        MSDN How to add new worksheets to workbooks[^] That help?

        Life, family, faith: Give me a visit. From my latest post: "When Constantine severed the Hebrew origins of this faith in Messiah, a new religion was officially created. It is this religion that Orthodox Jews fear their relatives and friends becoming members of." Judah Himango

        C Offline
        C Offline
        compninja25
        wrote on last edited by
        #3

        Well, I've seen this before and tried it but the compiler yells at me saying missing isn't found in the current context. I even tried replacing globals.thisworkbook with excel.application.workbook but it still gave me the same problem. What I don't understand is why we can't just add a worksheet without passing any parameters.

        R 1 Reply Last reply
        0
        • C compninja25

          Well, I've seen this before and tried it but the compiler yells at me saying missing isn't found in the current context. I even tried replacing globals.thisworkbook with excel.application.workbook but it still gave me the same problem. What I don't understand is why we can't just add a worksheet without passing any parameters.

          R Offline
          R Offline
          Robert Ernst
          wrote on last edited by
          #4

          workbook.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value); The MS example was incomplete. The line above works.

          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