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.

Maro HamamjyanPosted May 16, 2018, 1:53 AM
Can you please help to write the same code on a word document that is stored in a SharePoint Document Library?
Adriano SouzaPosted May 29, 2017, 1:53 PM
How do I apply this to items?
Humayun Kabir MamunPosted Mar 8, 2016, 11:09 PM
Nice...
Vinesh NagpalPosted Aug 4, 2014, 9:23 AM
How can i show some data on one page the other data on other pages using this
Destin JoyPosted May 27, 2014, 10:17 AM
one work around is to reload the page after export. I think there are some java scrip also you can use
Vinesh NagpalPosted May 27, 2014, 7:21 AM
I tried the same code in visual webpart its working fine but when i click on button and retrieve the docs after that button click is not working why this code disable the onclick of button
Leonid SalavatoveditedPosted Feb 1, 2013, 5:52 PMEdited Feb 1, 2013, 5:58 PM
export to word for sharepoint 2013: http://www.youtube.com/watch?v=ygolBfGGtOQ&hd=1
Liakath Ali MohammadPosted Aug 2, 2012, 8:26 AM
Hi Sir, Thanks for your post, I need to export list items to PDF file instead of word document, i have changed content type to application/PDF but still missing something, getting error as unknown format. and also i want to take barcode print out also as we did for list items. word document getting loading problem if i use Barcode or Barcode Value list columns in code. As this is was my urgent need. How should i move ? i am looking this link http://stackoverflow.com/questions/8505995/itextsharp-generated-pdf-how-to-send-the-pdf-to-the-client-and-add-a-prompt
Raj LakhanpalPosted Nov 12, 2010, 8:11 PM
Hi there, I am a fresh user for sharepoint. I downloaded the zipped file reg code. Where should I put this code now?
Macleena AlmeidaPosted Nov 10, 2010, 12:44 AM
I tried the code above but i didn't get any save/open dialog box pop up. Instead the data is displayed as a alert. Please help!
John SamuelPosted Nov 8, 2010, 10:25 AM
My requirement is to add this option as a menu in the list .Is it possible?