Free Support Forum - aspose.com

Export datagrid to MS Word with some standard text

Dear Roman/Support team,

I am evaluating Aspose Word and need your advise on how to export a datagrid to Word with some standard text in front. I am talking of generating a Qoutation doc. Is it possible to open a Word Template( which has the standard text) and then export the datagrid? So the User would see a pop-up of the final doc while the template would be on the Server.

Looking forward for a response. I posted a similar message earlier but I am not able to trace it.

Cheers
Pavan
Singapore

Hi Pavan,

Thank you for considering Aspose.

I saw your message and replied to it but it seems to be lost due to some reason.

First, what data source do you use to populate the datagrid? The point is that the most applicable way to "export the datagrid" is to execute a mail merge with regions. If your datagrid is populated by any ADO .NET data source object, you can pass it to MailMerge.ExecuteWithRegions and perform mail merge with regions on the prepared template. Here are the places where you can obtain information about it:

Demo projects: Product Catalog, Sales Invoice
API reference:
http://www.aspose.com/Products/Aspose.Word/Api/Aspose.Word.MailMerge.html
http://www.aspose.com/Products/Aspose.Word/Api/Aspose.Word.MailMerge.ExecuteWithRegions.html
A number of examples in the forums.

Please provide the details of your situation if you have any difficulties.

Thanx Dmitry. Yes it is a ADO dataset. But how to open a template file from the server and then plant the datagrid in a particular position. I have seen a few demos and messages here - it seems possible but would be great if you can just give a brief outline of the commands.

Cheers
Pavan

First, you need to define so-called mail merge regions in the template. A mail merge region is marked with a pair of special merge fields named TableStart:TableName and TableEnd:TableName where TableName is the name of the table from your dataset you want to be placed at this position. Then insert the appropriate merge fields inside the region that correspond to the table fields (columns). Finally, run the mail merge process as in the following example:

Document doc = new Document(“MyDocument.doc”);

// Pass your DataSet here
doc.MailMerge.ExecuteWithRegions(dataSet);

// If you need to send the result to the cliemt browser…doc.Save(“Result.doc”, SaveFormat.FormatDocument, SaveType.OpenInBrowser, Response);

To obtain more information, you can refer to these articles (although our programmers guide is still “under construction” and the articles are “beta version” Smile):

http://www.aspose.com/Wiki/default.aspx/Aspose.Word/PreparingDocument.html
http://www.aspose.com/Wiki/default.aspx/Aspose.Word/ExecutingMailMerge.html

See how to perform mail merge with regions there.

Thanx for the response Dmitry. I hope to get this working.

Dear Dmitry,

I got it working in a breeze! Let me ask an outrageous question:

1) We are now passing the dataset to the Document. Is it possible to pass a datagrid to it. The reason is in one of my datagrids, some calculations are done runtime and rendered to the template columns used. for e.g. the User might just change the value (I provide a text box in column) and press a button to reclaculate. (Pl find attached). In this case the dataset would be outdated.

Cheers
Pavan

Passing a datagrid itself to the merge engine is impossible because it is a control. You should implement a way to update the data source object with the changes made by the user or create a new object accumulating the data from the grid and then use this object to populate the template.

Dear Dmitry,

I have decided to purchase Aspose Word. While the process is going on, I have a question.

As indicated above I managed to get the Dataset into the Word document(suing ExecuteWithRegions) and it works. Now I need to plant the Customer Name also into the same document. While ofocurse its possible to do it independently, how to do it into the same doc. i.e one dataset goes as the Customer name and Address while the other as Rates.

Cheers
Pavan

There are no obstacles to put multiple merge regions into the document and execute mail merge as many times as needed. Is it what you mean or maybe you need to get all the data in the same region (table)?