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

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:
https://github.com/aspose-words/Aspose.Words-for-.NET
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: https://reference.aspose.com/words/net/aspose.words/document/
Thanks,

You can learn everything you need to know about mail merge in the documentation here: https://docs.aspose.com/words/net/mail-merge-and-reporting/
Also you can view a video tutorial on mail merge here:
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?

Hi,

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:

Name Address Mobile NTN
Ā«TableStart:TableNameĀ»Ā«NameĀ» Ā«AddressĀ» Ā«MobileĀ» Ā«NTNĀ»Ā«TableEnd:TableNameĀ»

Best
Regards,

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:
https://www.vbforums.com/showthread.php?474895-RESOLVED-GridView-to-DataTable
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:
https://docs.aspose.com/words/net/types-of-mail-merge-operations/
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 ā€œHow to Create a Mail Merge Template for use in Aspose.Wordsā€)
  2. [Merging Data into a Document using Simple Mail Merge*]http://www.youtube.com/watch?v=swi1l0zkg1c ā€œMerging Data into a Document using Simple Mail Merge and Aspose.Words for . ā€¦ā€)*

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 ā€œHow to Create a Mail Merge Template for use in Aspose.Wordsā€)
2.Merging Data into a Document using Simple Mail Merge ā€œMerging Data into a Document using Simple Mail Merge and Aspose.Words for . ā€¦ā€)

*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