Need to export content of a datagrid to EXCEL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maria L.

    Need to export content of a datagrid to EXCEL

    Hi,

    I need to export the content of a DataGrid (in Windows application in C#), into
    an Excel spreadsheet.

    Anyone knows how to do this? Any code snippets would help!

    thanks a lot,

    Maria
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Need to export content of a datagrid to EXCEL

    Maria,

    Your options are expanded or reduced based on the version of Excel that
    you are running. If you are runing Office XP (I believe), then I believe
    that Excel has support for XML in it (I know that 2003 has it, and you can
    definitely do it in this case). If there is XML support, you could save
    your contents as an XML file and then import that into Excel.

    If not, then you will have to cycle through the rows and columns and
    insert the values one by one.

    Either way, you will have to set a reference to the Microsoft Office
    Object Library, as well as the Microsoft Excel Object library, and use the
    classes in there to instatiate/get the Excel sheet and then write the values
    to it.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Maria L." <anonymous@disc ussions.microso ft.com> wrote in message
    news:1ABEB498-1BCA-47CA-AFFA-3E54071394A0@mi crosoft.com...[color=blue]
    > Hi,
    >
    > I need to export the content of a DataGrid (in Windows application in C#),[/color]
    into[color=blue]
    > an Excel spreadsheet.
    >
    > Anyone knows how to do this? Any code snippets would help!
    >
    > thanks a lot,
    >
    > Maria[/color]


    Comment

    • Maria L.

      #3
      Re: Need to export content of a datagrid to EXCEL

      thanks for your reply

      Actually, I am running Office 2000, so I will need to do it row by row

      What is the name of the libraries that I have to import
      Do you have any code snippet that might give me an idea
      I know the general idea, but I don't know where to start

      Maria

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Need to export content of a datagrid to EXCEL

        Maria,

        You will have to go to the COM tab when you select "Add Reference" to
        your project. The "Microsoft Office Object Library" will be there, as well
        as the "Microsoft Excel Object Library". When you set references to that,
        it will create interop assemblies in your project which you will have to
        ship with your product.

        Also, you will have to install Excel separately, and make sure that it
        is the same version. The interop assemblies only handle the calling of the
        COM objects, they don't wrap the COM objects for distribution.


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Maria L." <anonymous@disc ussions.microso ft.com> wrote in message
        news:9F379877-EFAA-4C0B-AD3D-9EC9FDE7216B@mi crosoft.com...[color=blue]
        > thanks for your reply.
        >
        > Actually, I am running Office 2000, so I will need to do it row by row.
        >
        > What is the name of the libraries that I have to import?
        > Do you have any code snippet that might give me an idea?
        > I know the general idea, but I don't know where to start!
        >
        > Maria[/color]


        Comment

        • Martin Dew

          #5
          Re: Need to export content of a datagrid to EXCEL

          here is a code snippet from one of my apps..it works for me..

          public void ExportToExcel(D ataSet myDataSet) {

          try {

          // Export The Dataset passed to an Excel Spreadsheet.

          Excel.Applicati on oXL;

          Excel.Workbook oWB;

          Excel.Worksheet oSheet;

          oXL = new Excel.Applicati onClass();

          oXL.Visible = true;

          oWB = (Excel.Workbook )(oXL.Workbooks .Add(Missing.Va lue));

          oSheet = (Excel.Workshee t)oWB.ActiveShe et;

          oSheet.Name = "QueryBuild er Export";

          oXL.WindowState = Excel.XlWindowS tate.xlMinimize d;

          // Initialise the progress form.

          frmProgress newProgress = new frmProgress();

          newProgress.pbP rogress.Minimum = 1;

          newProgress.pbP rogress.Value = 1;

          newProgress.pbP rogress.Step = 1;

          newProgress.Sho w();


          foreach(DataTab le table in myDataSet.Table s) {

          // Set Maximum to the total number of files to copy.

          newProgress.pbP rogress.Maximum = table.Rows.Coun t;

          int row = 1, column = 1;

          // Need to export the column headers here.

          foreach (object oH in table.Columns) {

          oSheet.Cells[row, column] = oH.ToString();

          column++;

          }

          row++;

          foreach(DataRow r in table.Rows) {

          column = 1;

          foreach (object o in r.ItemArray) {

          oSheet.Cells[row, column] = o.ToString();

          column++;

          }

          row++;

          newProgress.pbP rogress.Perform Step();

          Application.DoE vents();

          }

          }

          newProgress.Clo se();


          MessageBox.Show ("Export Process has completed");

          oXL.WindowState = Excel.XlWindowS tate.xlMaximize d;


          }

          catch (Exception e) {

          // Exception Handler

          MessageBox.Show ("Export Process has Errored : " + e.Message);

          }

          }



          "Maria L." <anonymous@disc ussions.microso ft.com> wrote in message
          news:9F379877-EFAA-4C0B-AD3D-9EC9FDE7216B@mi crosoft.com...[color=blue]
          > thanks for your reply.
          >
          > Actually, I am running Office 2000, so I will need to do it row by row.
          >
          > What is the name of the libraries that I have to import?
          > Do you have any code snippet that might give me an idea?
          > I know the general idea, but I don't know where to start!
          >
          > Maria[/color]


          Comment

          • Miha Markic

            #6
            Re: Need to export content of a datagrid to EXCEL

            Hi Maria,

            Check out this article:


            There is an another way which is more difficult.
            You might build xls file directly...

            --
            Miha Markic - RightHand .NET consulting & development
            miha at rthand com

            "Maria L." <anonymous@disc ussions.microso ft.com> wrote in message
            news:9F379877-EFAA-4C0B-AD3D-9EC9FDE7216B@mi crosoft.com...[color=blue]
            > thanks for your reply.
            >
            > Actually, I am running Office 2000, so I will need to do it row by row.
            >
            > What is the name of the libraries that I have to import?
            > Do you have any code snippet that might give me an idea?
            > I know the general idea, but I don't know where to start!
            >
            > Maria[/color]


            Comment

            Working...