Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

i have a gridview which is bounded with the database. have to export this gridview to msword in editable mode.i have msword-2007.I had tried the following way but it produced a saved word file and produced an error on opening "word cannot start the converter mswrd32.wpc".

C#
DataSet ds = new DataSet();
ds=obj_bal.BAL_ShowFrom_Reply();
GridView1.DataSource = ds;
GridView1.DataBind();
Response.AppendHeader("Content-Type", "application/vnd.ms-word");
Response.AddHeader("Content-Disposition","test.docx");
Response.Write(DropDownList1.Text);
Response.End();
Posted
Updated 7-Jul-10 3:40am
v2
Comments
Rutvik Dave 8-Jul-10 9:42am    
Make sure you are doing a full post back, and the button is not inside the update panel. Or try creating a separate page just with this code in Page_Load event, and add a link to your page which will open that page in new window. It should work I am using this same code in many web applications.

And Yes, It will give you a message for untrusted source, just click Yes.

1 solution

You can try the following code block instead of your code.

DataSet ds = new DataSet();
ds=obj_bal.BAL_ShowFrom_Reply();
GridView1.DataSource = ds;
GridView1.DataBind();

Response.Clear();
Response.ClearHeaders();
Response.AddHeader("content-disposition", "attachment;filename=test.docx");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
HtmlForm frm = new HtmlForm();
GridView1.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(GridView1);
frm.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
 
Share this answer
 
Comments
mcapatna 8-Jul-10 5:27am    
thank you Rutvik Dave for your reply.i tried the above solution.Although it create a word file but an error is occurred at the time of opening the file.The error message is "The file is corrupt and cannot be open" when i
click on ok button of this error message then another information flashed "Word found unreadable content in test.docx.Doyou want to recover the contents of this document?if,you trust the source of the document click,yes.when i click to yes then nothing happens and the ms word cannot open any file.
mcapatna 8-Jul-10 10:43am    
Reason for my vote of 5
this is what i want
mcapatna 10-Jul-10 9:21am    
thanks its working.can i send the entire page to msword.the page having various text boxes,labels,panels.please help me.

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