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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. dataTable save as excel programatically

dataTable save as excel programatically

Scheduled Pinned Locked Moved C#
comhelpquestion
5 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
    michael_jhons
    wrote on last edited by
    #1

    hello everybody i have a function that save my datatable to an excel file:

                int columnIndex = 0;
                Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
                excel.Application.Workbooks.Add(true);
                foreach (DataColumn dc in dataSet.Tables\[1\].Columns)
                {
                    columnIndex++;
                    excel.Cells.ColumnWidth = 25;
                    excel.Cells\[1, columnIndex\] = dc.ColumnName;
                }
    
                int rowIndex = 0;
                foreach (DataRow dr in dataSet.Tables\[1\].Rows)
                {
                    rowIndex++;
                    columnIndex = 0;
                    foreach (DataColumn dc in dataSet.Tables\[1\].Columns)
                    {
                        columnIndex++;
                        excel.Cells\[rowIndex + 1, columnIndex\] = dr\[dc.ColumnName\];
                    }
    
                }
    
                
                excel.Save(@"C:\\SomeName.xls");
    

    what is going on is that a pop up window ask me "if i need to save my excel sheet', if i agree both files are saved one in c:/someName.xls and another for the pop up window. i don't want the pop up to be displayed i just need to save programatically my datatable. i'm new in excel things please any help kind regards

    W 1 Reply Last reply
    0
    • M michael_jhons

      hello everybody i have a function that save my datatable to an excel file:

                  int columnIndex = 0;
                  Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
                  excel.Application.Workbooks.Add(true);
                  foreach (DataColumn dc in dataSet.Tables\[1\].Columns)
                  {
                      columnIndex++;
                      excel.Cells.ColumnWidth = 25;
                      excel.Cells\[1, columnIndex\] = dc.ColumnName;
                  }
      
                  int rowIndex = 0;
                  foreach (DataRow dr in dataSet.Tables\[1\].Rows)
                  {
                      rowIndex++;
                      columnIndex = 0;
                      foreach (DataColumn dc in dataSet.Tables\[1\].Columns)
                      {
                          columnIndex++;
                          excel.Cells\[rowIndex + 1, columnIndex\] = dr\[dc.ColumnName\];
                      }
      
                  }
      
                  
                  excel.Save(@"C:\\SomeName.xls");
      

      what is going on is that a pop up window ask me "if i need to save my excel sheet', if i agree both files are saved one in c:/someName.xls and another for the pop up window. i don't want the pop up to be displayed i just need to save programatically my datatable. i'm new in excel things please any help kind regards

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      If I remember correctly, you must set the Saved property of a workbook to true. Save method doesn't automatically mark the workbook as saved.

      The need to optimize rises from a bad design

      M 2 Replies Last reply
      0
      • W Wendelius

        If I remember correctly, you must set the Saved property of a workbook to true. Save method doesn't automatically mark the workbook as saved.

        The need to optimize rises from a bad design

        M Offline
        M Offline
        michael_jhons
        wrote on last edited by
        #3

        dear Mika, thank you for your answer i aklready added to my code : excel.ThisWorkbook.Saved = true; it gave me an error "0x800A03EC"...get.thisWorkbooks()... please could you help me how to set this property to true. best regards

        1 Reply Last reply
        0
        • W Wendelius

          If I remember correctly, you must set the Saved property of a workbook to true. Save method doesn't automatically mark the workbook as saved.

          The need to optimize rises from a bad design

          M Offline
          M Offline
          michael_jhons
          wrote on last edited by
          #4

          i'm sorry it was ActiveWorkbook.Saved not thisWorkbook.saved, now it's working fine thank you for your advise

          W 1 Reply Last reply
          0
          • M michael_jhons

            i'm sorry it was ActiveWorkbook.Saved not thisWorkbook.saved, now it's working fine thank you for your advise

            W Offline
            W Offline
            Wendelius
            wrote on last edited by
            #5

            No problem, glad you got it solved :)

            The need to optimize rises from a bad design

            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