Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi!

Is it possible to create a file in .doc/.docx/.rtf format 1 page per record.
The record will be coming from gridview/datatable.

The fields and records should be listed in vertical format like:


HSBC DOLLAR ACCOUNT //this is a header
DOLLAR REMITANCE //this is a header

Employee: Juan Dela Cruz
AccountNo: 1234567
AccountName: Juan DLCruz
BankCode: Sample Bank of the Republic
BENEBNKMET: SSSS Met
Posted
Comments
Camects 15-Jan-16 4:56am    
You may (or may not) know this but I'd like to point out that the code that you posted in the below comments is not actually creating a DOC file:

Response.AddHeader("content-disposition", "attachment;filename=" + title + ".doc");
Response.Charset = "";
Response.ContentType = "application/ms-word";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvExport.RenderControl(hw);

string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();

What this does is it takes a HTML representation of a GridView and saves it as a file with a DOC extension. This is a sort of a workaround because MS Word is able to read and display that HTML file however when opening this file it will throw a warning that says that a file's format and extension are not the same. After you re-save it with MS Word then it will become a "real" DOC file.
Now depending on the situation this may not be an ideal solution, so as an alternative you can check out how to convert a HTML content into a Word document in C#.

Another alternative is this:
http://www.codeproject.com/Articles/91894/HTML-as-a-Source-for-a-DOCX-File
But note that this approach only works for MS Word. In other words the documents created by that approach will appear empty in any other Word application (except in MS Word).

1 solution

DOCX, sure, but better not DOC. You can use Microsoft open-source product Open XML Format SDK:
Office Open XML - Wikipedia, the free encyclopedia[^],
Microsoft Office XML formats - Wikipedia, the free encyclopedia[^];
this product is available on github:
OfficeDev/Open-XML-SDK · GitHub[^].

See also Microsoft warnings about using Office interop in server settings:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2[^],
http://support.microsoft.com/kb/257757/en-us[^].

See also this Microsoft article of 2007 (the version of SDK referenced to is obsolete, but you can get a good idea how to use it): Creating Documents by Using the Open XML Format SDK Version 2.0 CTP (Part 1 of 3)[^].

I really hope we don't need to discuss obsolete DOC formats, which are bad, messed up and proprietary, without official open documentation. It's also possible, but mach harder to do.

But RTF is much simpler. For example, see these CodeProject articles:
RTF Document Constructor Library[^],
RichText Builder (StringBuilder for RTF)[^].

—SA
 
Share this answer
 
Comments
bjay tiamsic 8-Jan-16 23:09pm    
I am trying to render the gridview. I tried this

gvExportDoc.DataSource = dtInput;
gvExportDoc.DataBind();

string title = "OthersAbroad_BankReport";
Response.Clear();
Response.Buffer = true;


Response.AddHeader("content-disposition", "attachment;filename=" + title + ".doc");
Response.Charset = "";
Response.ContentType = "application/ms-word";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvExport.RenderControl(hw);

string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();


It works, however, my GridView font is Arial but Times New Roman is being defaulted when the word file was produced and I couldn't customize the margin.
Sergey Alexandrovich Kryukov 8-Jan-16 23:56pm    
How is this related to your original question. Did you understand my answer? Are you going to accept it formally?
Do you have follow-up questions?
—SA
bjay tiamsic 9-Jan-16 1:10am    
Wait. So i tried it locally and I needed to install it in order to get the .dll
Do I also need to install it to the server (may affect other application)? or possible to just copy and paste the .dll from my local project to the server project folder?
Sergey Alexandrovich Kryukov 9-Jan-16 2:01am    
I'm not sure what you are asking about. Please read the documentation. For a library, you can usually just use it without installation.
—SA
bjay tiamsic 9-Jan-16 1:12am    
Can you also provide a link where I can customize it like the margin, font family, size etc.? I will be exporting a data from datatable and the word document will have a header and footer (Word Header and Footer is not necessary, just a custom header/footer would do)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900