Export SharePoint List items to Word Document
C# Code:
protected void btnExportToWord_Click(object sender, EventArgs e)
{
try
{
this.varExport = true;
}
catch (Exception ex)
{
throw ex;
}
}
protected override void Render(HtmlTextWriter writer)
{
if (varExport)
{
Exportword();
}
base.Render(writer);
}
protected override void Render(HtmlTextWriter writer)
{
if (varExport)
{
Exportword();
}
base.Render(writer);
}
private void Exportword()
{
try
{
using (SPSite osite = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb oweb = osite.OpenWeb())
{
string siteUrl = osite.Url;
//Creating HttpContext
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
//HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentType = "application/vnd.ms-word";
//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 strHTMLMetaContent = new StringBuilder();
//Setting the Header content and Footer content
strHTMLMetaContent = GettingHeaderFooter();
strHTMLMetaContent.Append("EXPORT TO WORD SAMPLE".ToString());
//Getting the Job Aid metadata details
strHTMLMetaContent.Append(GettingJobAidMetadata(oweb));
//Getting the Job Aid Section details
strHTMLMetaContent.Append(GettingJobAidSectiondata(oweb));
strHTMLMetaContent.Append("
".ToString());
HttpContext.Current.Response.Write(strHTMLMetaContent);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
private StringBuilder GettingJobAidMetadata(SPWeb oweb)
{
StringBuilder strHTMLContent = new StringBuilder();
try
{
SPList olist = oweb.Lists.TryGetList("Job Aid Documents");
SPQuery oquery = new SPQuery();
oquery.Query = "52 ";
SPListItemCollection oitemcoll = olist.GetItems(oquery);
if (oitemcoll.Count > 0)
{
strHTMLContent.Append("".ToString());
foreach (SPListItem oitem in oitemcoll)
{
strHTMLContent.Append("
".ToString());
strHTMLContent.Append("
".ToString());
strHTMLContent.Append("
".ToString());
}
strHTMLContent.Append("Job Aid No: "+Convert.ToString(oitem["JobAidNo"])+ ".ToString())";
}
}
catch (Exception ex)
{
throw ex;
}
return strHTMLContent;
}
private StringBuilder GettingJobAidSectiondata(SPWeb oweb)
{
string siteUrl = oweb.Site.Url;
StringBuilder strHTMLContent = new StringBuilder();
try
{
SPList olist = oweb.Lists.TryGetList("Job Aid Sections");
SPQuery oquery = new SPQuery();
oquery.Query = "52 0 ";
SPListItemCollection oitemcoll = olist.GetItems(oquery);
if (oitemcoll.Count > 0)
{
strHTMLContent.Append("".ToString());
foreach (SPListItem oListItem in oitemcoll)
{
SectionContent = Convert.ToString(oListItem["SectionContent"]).Replace("src=\"/", "src=\"" + siteUrl + "/");
//SectionContent = Regex.Replace(SectionContent, "<(p)>.\\s?<(.?p)>", string.Empty);
//SectionContent = Regex.Replace(SectionContent, "<(span.*?)> <(.?span)>", string.Empty);
//SectionContent = Regex.Replace(SectionContent, "]+>.\\s?", string.Empty);
strHTMLContent.Append("
");
strHTMLContent.Append("
");
}
strHTMLContent.Append(" + oListItem["Title"].ToString() + " + SectionContent +".ToString());
}
}
catch (Exception ex)
{
throw ex;
}
return strHTMLContent;
}
protected override void Render(HtmlTextWriter writer)
{
if (varExport)
{
Exportword();
}
base.Render(writer);
}
protected override void Render(HtmlTextWriter writer)
{
if (varExport)
{
Exportword();
}
base.Render(writer);
}
private void Exportword()
{
try
{
using (SPSite osite = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb oweb = osite.OpenWeb())
{
string siteUrl = osite.Url;
//Creating HttpContext
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
//HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentType = "application/vnd.ms-word";
//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 strHTMLMetaContent = new StringBuilder();
//Setting the Header content and Footer content
strHTMLMetaContent = GettingHeaderFooter();
strHTMLMetaContent.Append("EXPORT TO WORD SAMPLE".ToString());
//Getting the Job Aid metadata details
strHTMLMetaContent.Append(GettingJobAidMetadata(oweb));
//Getting the Job Aid Section details
strHTMLMetaContent.Append(GettingJobAidSectiondata(oweb));
strHTMLMetaContent.Append("
".ToString());
".ToString());
HttpContext.Current.Response.Write(strHTMLMetaContent);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
private StringBuilder GettingJobAidMetadata(SPWeb oweb)
{
StringBuilder strHTMLContent = new StringBuilder();
try
{
SPList olist = oweb.Lists.TryGetList("Job Aid Documents");
SPQuery oquery = new SPQuery();
oquery.Query = "52 ";
SPListItemCollection oitemcoll = olist.GetItems(oquery);
if (oitemcoll.Count > 0)
{
strHTMLContent.Append("".ToString());
foreach (SPListItem oitem in oitemcoll)
{
strHTMLContent.Append("
".ToString());
strHTMLContent.Append("
".ToString());
strHTMLContent.Append("
".ToString());
}
strHTMLContent.Append("Job Aid No: "+Convert.ToString(oitem["JobAidNo"])+ ".ToString())";
}
}
catch (Exception ex)
{
throw ex;
}
return strHTMLContent;
}
private StringBuilder GettingJobAidSectiondata(SPWeb oweb)
{
string siteUrl = oweb.Site.Url;
StringBuilder strHTMLContent = new StringBuilder();
try
{
SPList olist = oweb.Lists.TryGetList("Job Aid Sections");
SPQuery oquery = new SPQuery();
oquery.Query = "52 0 ";
SPListItemCollection oitemcoll = olist.GetItems(oquery);
if (oitemcoll.Count > 0)
{
strHTMLContent.Append("".ToString());
foreach (SPListItem oListItem in oitemcoll)
{
SectionContent = Convert.ToString(oListItem["SectionContent"]).Replace("src=\"/", "src=\"" + siteUrl + "/");
//SectionContent = Regex.Replace(SectionContent, "<(p)>.\\s?<(.?p)>", string.Empty);
//SectionContent = Regex.Replace(SectionContent, "<(span.*?)> <(.?span)>", string.Empty);
//SectionContent = Regex.Replace(SectionContent, "]+>.\\s?", string.Empty);
strHTMLContent.Append("
");
strHTMLContent.Append("
");
}
strHTMLContent.Append(" + oListItem["Title"].ToString() + " + SectionContent +".ToString());
}
}
catch (Exception ex)
{
throw ex;
}
return strHTMLContent;
}
No comments:
Post a Comment