Generate Word Document by retrieving html content from Database in asp.net

Generate Word Document by retrieving html content from Database in asp.net



protected void btnGenerateDocument_Click(object sender, EventArgs e)
{

catalog_GetFrontMatterContentByCodeTableAdapter frontMatterTA = new catalog_GetFrontMatterContentByCodeTableAdapter();
DataTable frontMatterDT = frontMatterTA.GetFrontMatterContentByCode("RGEN");
frontMatterDT.TableName = "frontmatterContentDS";

//string str = frontMatterDT.Rows[1]["SectionMarkup"].ToString();
string str = "";
str = "<div style='mso-element:header' id=h1><p class=MsoFooter><img src='http://localhost:3678/Generating Word Document/images/header.jpg'></p></div>";
str += "<h1 style='font-size:24pt; font-family:Times New Roman;'>"+frontMatterDT.Rows[0]["PageTitle"].ToString()+"</h1>";
for (int i = 0; i < frontMatterDT.Rows.Count; i++)
{

str += "<h3 style='font-size:18pt; font-family:Times New Roman;'>".ToString() + frontMatterDT.Rows[i]["SectionHeader"].ToString() + "</h3>".ToString();
str += frontMatterDT.Rows[i]["SectionMarkup"].ToString();
}

StringBuilder sb = new StringBuilder();

sb.Append(@"<html
xmlns:o='urn:schemas-microsoft-com:office:office'
xmlns:w='urn:schemas-microsoft-com:office:word'
<head><title>Catalog pdf</title>");

sb.Append(@"<!--[if gte mso 9]>
<xml>
<w:WordDocument>
<w:View>Print</w:View>
<w:Zoom>90</w:Zoom>
<w:DoNotOptimizeForBrowser/>
</w:WordDocument>
</xml>
<![endif]-->");

sb.Append(@"<style>
<!-- /* Style Definitions */
@page Section1
{
size:8.0in 11.0in;
margin:.5in 1.0in 1.0in 1.0in ;
mso-header-margin:.001in;
mso-page-orientation: portrait;
mso-footer-margin:.001in; mso-paper-source:0;}
div.Section1
{
page:Section1;
}
-->
</style></head>");

sb.Append(@"<body lang=EN-US style='tab-interval:.5in'>
<div class=Section1>
<br>");
sb.Append(@"<p><br>" + str + "</p>");
sb.Append(@"</div></body></html>");
Response.AppendHeader("Content-Type", "application/msword");
Response.AppendHeader("Content-disposition", "inline; filename=generatedwordoc.doc");
Response.Write(sb);
}

Comments

Popular posts from this blog

html to Word Document Converter using Open XML SDK

How to: Get Top n Rows of DataView in C# asp.net

Argument Essay Writing- Agree or Disagree