How to Export SharePoint List Items to Word Document using Server Object Model in SharePoint 2010/2013/2016/2019

This SharePoint tutorial explains, how to export SharePoint 2013 list items to word document using SharePoint 2013 server object model. Here I have a SharePoint 2013 list which has few list items. My requirement is to export all items to a word document.

By using the below server object model code, we can export items to word document using the server object model code in SharePoint 2010/2013/2016/2019. The code will not work in SharePoint Online, because we can not use the server object model code in SharePoint Online.

If you want to export to excel then you can follow, Export selected record to excel in SharePoint 2013 using JavaScript client object model and SPServices.

Export SharePoint list items to word document

Below is the code to export sharepoint list items to word document. I have written the code in a button click inside a visual web part in SharePoint. Below is the code (on button click).

You can read below tutorial to know about SharePoint visual web parts.

SPSite oSiteCollection = SPContext.Current.Site;
SPWeb web = oSiteCollection.RootWeb;
SPList oList = web.Lists.TryGetList(“Employee”);
SPListItemCollection collListItems = oList.Items;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = “”;
HttpContext.Current.Response.ContentType = “application/msword”;
string strFileName = “EmployeeList” + “.doc”;
HttpContext.Current.Response.AddHeader(“Content-Disposition”, “inline;filename=” + strFileName);
StringBuilder strHTMLContent = new StringBuilder();
strHTMLContent.Append(“<table>”.ToString());
foreach (SPListItem oListItem in collListItems)
{
strHTMLContent.Append(“<tr><td>” + oListItem[“Title”].ToString() + “</td></tr>”);
}
strHTMLContent.Append(“</table>”.ToString());
HttpContext.Current.Response.Write(strHTMLContent);
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();

Once you deploy the Visual web part and then click on the button the file will be downloaded and will ask to Save. Now if you will open the word you will see the records like below:

export sharepoint list items to word document
export sharepoint list items to word document

Hope this SharePoint tutorial helps to export SharePoint list items to word document.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

>