none
source code for exporting sharepoint list items to word

    Question

  • Hi,
    I have to export all list items of a list to worddocument on one click. For example, i have created a list and added some rows of item, on the bottom of the list page there should be a button, on clicking on this button all rows item should be exported to word doc. this I have to do programmatically.

    Code for sharepoint 2007

    Thanks
    Harsh

    • Edited by Mike Walsh FIN Friday, April 16, 2010 9:08 AM NEVER say in a forum post that something is urgent. These are forums. Post and wait your turn. If you are in such a hurry ring MS Customer Support Services and pay for support.
    Friday, April 16, 2010 9:02 AM

Answers

  • Hi, Harsh

           

          You could achieve this wtih a custom webpart, including Kumar or Peter’s suggestion.

          If your concern was how to get current list information, then you could retrieve the information from SPContext.

          Code like this:

          ---------------------------------------------------------------

           private Button exportBtn;

            protected override void CreateChildControls()

            {

                base.CreateChildControls();

                exportBtn = new Button();

                exportBtn.Text = "ExportToWord";

     

                //get list information from SPContext

                SPList list=SPContext.Current.List;

               

                exportBtn.Click += new EventHandler(exportButton_Click);

                this.Controls.Add(exportBtn);

            }

           ---------------------------------------------------------------

           Some more reference for the solution:

           http://www.etechplanet.com/post/2009/12/09/Export-SharePoint-List-to-Excel-Spreadsheet-Programmatically-using-C.aspx

           Using Open XML SDK solution as Peter suggested:

           http://exporter.codeplex.com/Wikipage

     

           Hope this can help.

     

     Best Regards,

     -Aaron

    • Marked as answer by Chengyi Wu Friday, April 23, 2010 9:16 AM
    Monday, April 19, 2010 8:39 AM

All replies

  • Hi Harsh,

    You should use OpenXML to generate the Word document from scratch. I suggest you to download the Open XML SDK 2.0 (http://www.microsoft.com/downloads/details.aspx?FamilyId=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&displaylang=en), create a Word document (using Office) that seems similar to the one you would like to generate, then use the SDK tool to reflect the code.

    See an intro here:
    Announcing the Release of the December 2009 CTP for the Open XML SDK
    http://blogs.msdn.com/brian_jones/archive/2009/12/14/announcing-the-release-of-the-december-2009-ctp-for-the-open-xml-sdk.aspx

    Then look for the code block that is generated for an item from the document (probably a table row or something like that), and refactor that into a repeatable code block for each item.

    I'm afraid you should create the code for yourself, especially if it is urgent for you.

    Otherwise I'm to blog something similar within the next week, but it is not exactly the same.

    Peter

    Friday, April 16, 2010 9:16 AM
  • Hi Peter,

    Thanks for response.

    It's very difficult for me the way you suggested but I will try to achieve. i will be most obliged to you if you suggested the similar in coming few days.

    Thanks

    Harsh

    Friday, April 16, 2010 9:32 AM
  • Hi,

    It is really not trivial, but after working with the SDK Tool with a few hours that is not so hard either.

    Peter

    Friday, April 16, 2010 10:21 AM
  • Harsh,

    Please follow the post on how export custom list items to Excel in Sharepoint using SPGridview where using SPGridView you can easily export the data to .csv, .xls, .doc etc., files

    but, you have to modify code on:

    Line # 50: ExportToExcel("CountryState.xls" , myGridView);      to    ExportToExcel("CountryState.doc", myGridView);

    Then after on,
    Line # 99:
    HttpContext.Current.Response.ContentType = "application/ms-excel";      to    HttpContext.Current.Response.ContentType = "application/ms-word";

    Let me know in case if you need more information


    Best Regards, G Vijai Kumar | My Sharepoint Blog
    Friday, April 16, 2010 12:11 PM
  • Hi Raj,

    the code is really helpul but can you suggest me how to call that webpart on clicking of 'Export' button in sharepoint designer. Actually I am confused how to provide url to call webpart on click event of the button. suppose I have created a webpart said ListExport.dll, I have put this dll into bin directory then what should be the url to call that dll and how to set for sending list information in query string.

    Please don't mind I am beginner in sharepoint 2007.

    Thanks,

    Harsh

    Friday, April 16, 2010 2:57 PM
  • Hi, Harsh

           

          You could achieve this wtih a custom webpart, including Kumar or Peter’s suggestion.

          If your concern was how to get current list information, then you could retrieve the information from SPContext.

          Code like this:

          ---------------------------------------------------------------

           private Button exportBtn;

            protected override void CreateChildControls()

            {

                base.CreateChildControls();

                exportBtn = new Button();

                exportBtn.Text = "ExportToWord";

     

                //get list information from SPContext

                SPList list=SPContext.Current.List;

               

                exportBtn.Click += new EventHandler(exportButton_Click);

                this.Controls.Add(exportBtn);

            }

           ---------------------------------------------------------------

           Some more reference for the solution:

           http://www.etechplanet.com/post/2009/12/09/Export-SharePoint-List-to-Excel-Spreadsheet-Programmatically-using-C.aspx

           Using Open XML SDK solution as Peter suggested:

           http://exporter.codeplex.com/Wikipage

     

           Hope this can help.

     

     Best Regards,

     -Aaron

    • Marked as answer by Chengyi Wu Friday, April 23, 2010 9:16 AM
    Monday, April 19, 2010 8:39 AM