Free Support Forum - aspose.com

How to convert a asp.net web page to word document?

hi,i have an asp.net web page that has nearly 15 GridView.I Want When User Clicked On Save Button,web application convert This webpage to a word document.please help me.im new in aspose.net.
thanks in advance


This message was posted using Page2Forum from Aspose.Words for .NET - Documentation
1 Like

Hi behzad306,


Thank you for contacting Aspose.Words. I think, in your case, you can first create a template word document that depicts your ASP.Net web page. Then, for each GridView control, you have to insert a table template having merge fields inside each column. Import this document inside memory and by using mail merge feature of Aspose.Words, populate these templates with data from any type of data source.

Moreover, to render a table inside Word document, please try using the way suggested here:
http://www.aspose.com/demos/.net-components/aspose.words/csharp/general/alternating-rows.aspx

I hope this will help.

Best regards,

thanks for your guidances.can u send me a clear example of your above desriptions?
i couldnt understand demo you put its link here.
and finally exaplain how to use mail merge in a word document?
best regards.

Hi there,


Thanks for your inquiry.

Additionally, you can load an HTML page directly into an Aspose.Words document and then save as .DOC.

Please see the code examples here for details: http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.documentconstructor5.html

Thanks,

You can learn everything you need to know about mail merge in the documentation here: http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/mail-merge-and-reporting.html


Also you can view a video tutorial on mail merge here: http://www.aspose.com/videos/aspose.words/aspose.words-for-.net/merging-data-into-document.aspx

Thanks,

thanks for your help.so how to create a mail merge for a grid view in word document?
i mean to merge an image in word document we have to do following as:
image:merge mail field
and question is how to act in merging GridView?

<span style=“font-size:12.0pt;
line-height:115%;font-family:“Times New Roman”,“serif”;mso-fareast-font-family:
“Times New Roman””>Hi,

<span style=“font-size:12.0pt;
line-height:115%;font-family:“Times New Roman”,“serif”;mso-fareast-font-family:
“Times New Roman””>

<span style=“font-size:12.0pt;
line-height:115%;font-family:“Times New Roman”,“serif”;mso-fareast-font-family:
“Times New Roman””>Thanks for your request. Unfortunately, there is no direct
way to merge a grid view in the document using mail merge. In your case, you
can achieve this by adding a table in your template document for each grid view
as shown below:<o:p></o:p>

Name

Address

Mobile

NTN

«TableStart:TableName»

«Name» «Address» «Mobile» «NTN»

«TableEnd:TableName»

<span style=“font-size:12.0pt;line-height:115%;font-family:“Times New Roman”,“serif”;
mso-fareast-font-family:“Times New Roman”;color:#333333;background:white;
mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA”>Best
Regards,


<span style=“font-size:12.0pt;line-height:115%;font-family:“Times New Roman”,“serif”;
mso-fareast-font-family:“Times New Roman”;color:#333333;background:white;
mso-ansi-language:EN-US;mso-fareast-language:EN-US;mso-bidi-language:AR-SA”>Nabeel
Ahmad

thanks for your answer.
now question is how to code in asp.net to merge Informations in word document?im beginner in aspose.net

and second one is how to export a gridview indirectly in word document?
i mean from where does word document template can recognize the rows number of GridZView to set the same rows count in Table created in word document template??
thanks in advance

Hi there,


Thanks for this additional information.

I think you can first convert your GridView to a DataTable using one of the techniques demonstrated on this site here: http://www.vbforums.com/showthread.php?t=474895

After that you can merge the DataTable normally into your template document as suggested by Nabeel.

Thanks,

thanks all.but my problem is following as:
i have created my word document template with mail merge fields in it.now how do i code in asp.net c# to replace this fields with my values in database?
note that i have some grid view too.and how to replace a mail merge field with my gridview?
best regards

someone help me please!! its Urgent

Hello

Thanks for your inquiry. Just pass a data source object containing data rows to theMailMerge.ExecuteWithRegions method. You can even use a DataSet object to execute a mail merge for several regions filling each of them with the data from a separate table. Please see the following link for more information:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/mail-merge-with-regions-explained.html

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-execute-mail-merge-with-regions.html

Please let me know in case of any issues, I will be glad to help you.

Best regards,

thanks for your help.i Perform mail merge from a DataSet into a document with mail merge regions.but when run asp.net this error display:

Found end of mail merge region ‘table’ without matching start.

which ‘table’ is my datatable name in dataset.

this is my code:


protected void Button2_Click(object sender, EventArgs e)
{
const string path = “C:\Users\n\Desktop\”;
Document doc = new Document(path + “a.Docx”);
DataSet data=new DataSet();
DataTable table = new DataTable();

table.Columns.Add(“a”);
table.Columns.Add(“b”);
table.Columns.Add(“c”);
table.Columns.Add(“d”);
table.Columns.Add(“e”);
table.Rows.Add(new string[] { “behzad”, “arsenal”, “man”, “to”, “they” });

//table.Rows.Add(new string[] { “1”, “2”, “3”, “4”, “5” });
data.Tables.Add(table);
doc.MailMerge.ExecuteWithRegions(data);

doc.Save(path + “2.Docx”);
}


can u tell me cause of this error?


Hi behzad306,

You can also override the 'Render' method of the web page and write its content, that is to be rendered on the client's browser, to a word document by using the following code snippet:

protected override void Render(HtmlTextWriter output)
{
// Get HTML of the page
System.IO.StringWriter oStringWriter = new StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
base.Render(oHtmlTextWriter);

StringReader reader = new StringReader(oStringWriter.ToString());
MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(reader.ReadToEnd()));

Document doc = new Document(stream);
doc.Save(Response, "Asp.Net to word.docx", ContentDisposition.Inline, null);
}

I hope this will help

Best regards,
AwaisH:
Hi behzad306,

You can also override the Render method of the web page and write its content, that is to be rendered on the client's browser, to a word document by using the following code snippet:

protected override void Render(HtmlTextWriter output)
{
// Get HTML of the page
System.IO.StringWriter oStringWriter = new StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
base.Render(oHtmlTextWriter);

StringReader reader = new StringReader(oStringWriter.ToString());
MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(reader.ReadToEnd()));

Document doc = new Document(stream);
doc.Save(Response, "Asp.Net to word.docx", ContentDisposition.Inline, null);
}

I hope this will help

Best regards,


thanks for your help but i would rather to use mail merge method in aspose.net.
can anyone answer my previous post?
Hi behzad306,

Could you please attach your input template document here for testing?

Best regards,
AwaisH:
Hi behzad306,

Could you please attach your input template document here for testing?

Best regards,


yes,of course.
i attached it.
best regards.

Dear Behzad,

Thank you for your request. I have slightly modified your given function to work with aspose.Words.net. Please note the highlighted line in which the tablename Property has been set before executing mailmerge. This is essential. As you were missing this, you were getting the error.

private void button1_Click(object sender, EventArgs e)

{

const string path = “C:\”;

Document doc = new Document(@“a.Docx”);

DataSet data = new DataSet();

DataTable table = new DataTable();



table.Columns.Add(“a”);

table.Columns.Add(“b”);

table.Columns.Add(“c”);

table.Columns.Add(“d”);

table.Columns.Add(“e”);

table.Rows.Add(new string[] { “behzad”, “arsenal”, “man”, “to”, “they” });



table.TableName = “Temp”;



//table.Rows.Add(new string[] { “1”, “2”, “3”, “4”, “5” });

data.Tables.Add(table);

doc.MailMerge.ExecuteWithRegions(data);



doc.Save(@“2.Docx”);

}


I have created the template file (a.Docx) and I am also attaching this file for comparison purposes. If you open this file in MSWord 2007 and press ALT+F9, you will see various merge fields. In order to create a table with such merge fields, I would recommend you to consult the following 2 very useful tutorials.

1.How to Create a Mail Merge Template for use in Aspose.Words
2.Merging Data into a Document using Simple Mail Merge
In case you have any other concern, please feel free to ask.

Adnan Mujahid Khan



adnan.mujahid:
Dear Behzad,

Thank you for your request. I have slightly modified your given function to work with aspose.Words.net. Please note the highlighted line in which the tablename Property has been set before executing mailmerge. This is essential. As you were missing this, you were getting the error.

private void button1_Click(object sender, EventArgs e)
{
const string path = "C:\\";
Document doc = new Document(@"a.Docx");
DataSet data = new DataSet();
DataTable table = new DataTable();

table.Columns.Add("a");
table.Columns.Add("b");
table.Columns.Add("c");
table.Columns.Add("d");
table.Columns.Add("e");
table.Rows.Add(new string[] { "behzad", "arsenal", "man", "to", "they" });

table.TableName = "Temp";

//table.Rows.Add(new string[] { "1", "2", "3", "4", "5" });
data.Tables.Add(table);
doc.MailMerge.ExecuteWithRegions(data);

doc.Save(@"2.Docx");
}


I have created the template file (a.Docx) and I am also attaching this file for comparison purposes. If you open this file in MSWord 2007 and press ALT+F9, you will see various merge fields. In order to create a table with such merge fields, I would recommend you to consult the following 2 very useful tutorials.
1.How to Create a Mail Merge Template for use in Aspose.Words
2.Merging Data into a Document using Simple Mail Merge
In case you have any other concern, please feel free to ask.

Adnan Mujahid Khan





thanks for useful help.it works great,but when my data table has for example 3 rows while merging in word document each row is displayed separate from each others so that its now seem as same as a table structure.
can u give me a solution to display data table contents exactly like a table structure in template word?
best regards