SIGN UP MEMBER LOGIN:    
ARTICLE

Export SharePoint List items to Word Document

Posted by Destin joy Articles | SharePoint November 03, 2010
In this article I am showing you how to export list items in to word document.
Reader Level:
Download Files:
 

In this article I am showing you how to export list items in to word document. In SharePoint we have a default feature to export to Excel but there is no default feature to export to word. This article will help you to achieve the functionality.

First of all you can make use of the code to develop it as a web part or you can make it as a feature to deploy it to SharePoint so that it will come as a new menu item with list Actions. I am just explaining the code sample only so that you can make use of it where ever you want. We can select the fields from the list which we want to export to the word document.

//Opening the current site
SPSite oSiteCollection = SPContext.Current.Site; 

//Fetching the current list
SPList oList = oSiteCollection.AllWebs["Site_Name"].Lists["List_Name"];
SPListItemCollection collListItems = oList.Items;

//Creating HttpContext
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/msword";

//File name for the exported word document
string strFileName = "SampleDocument" + ".doc";
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);

//Giving heading to the Word Document with style
StringBuilder strHTMLContent = new StringBuilder();
strHTMLContent.Append(" <h1 title='Heading' align='Center' style='font-family:verdana;font-size:80%;color:black'><u> EXPORT TO WORD SAMPLE </u></h1>".ToString());
strHTMLContent.Append("<br>".ToString());
strHTMLContent.Append("<table style=margin-top: 8px; border=1 bordercolor=#808080 frame=hsides rules=rows cellpadding=0 cellspacing=0 width=100%>".ToString());

//Looping through each list item in  the list
foreach (SPListItem oListItem in collListItems)
{
    strHTMLContent.Append("<tr><td>" + oListItem["Choice_Field_Name"].ToString() +"</td></tr>");
}
strHTMLContent.Append("</table>".ToString());
strHTMLContent.Append("<br><br>".ToString());
HttpContext.Current.Response.Write(strHTMLContent);
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();

This will cause an Open / Save As dialog box to pop up with the filename SampleDocument.doc.

I have uploaded the sample code also so that you can make use I t in your application.

Login to add your contents and source code to this article
share this article :
post comment
 
Posted by Destin joy Nov 26, 2010

No i just want to export one image from my picture library
I tried to pass the URL of the image but in exported word file it is not showing me the image. Instead it is taking my internet temp file path where he is not getting that image.

Posted by Macleena Almeida Nov 25, 2010

Not clear :(You want to export a picture library to word document?

Posted by Destin joy Nov 25, 2010

Thanks !! I Solved it.
Now If i want to export the image which is there in my style library how to do that?

Posted by Macleena Almeida Nov 25, 2010

You can make use of the code in a webpart.Have a look at this article if u are using Sharepoint 2007

http://www.c-sharpcorner.com/UploadFile/sarav82/webpartlifecycle01102008061313AM/webpartlifecycle.aspx

or check my article in c-sharp corner  for SharePoint 2010

Posted by Destin joy Nov 12, 2010
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor