Click here to Skip to main content
15,890,412 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Following link http://www.c-sharpcorner.com/UploadFile/0c1bb2/export-gridview-to-word/[^] , this is my code is export gridview to word.

gridLedgerReport.AllowPaging = false;
            gridLedgerReport.DataSourceID = string.Empty;            
            gridLedgerReport.DataSource = ViewState["grid"];            
            gridLedgerReport.DataBind();
                       
            Response.Clear();
            Response.Buffer = true;
            Response.ClearContent();
            Response.ClearHeaders();
            Response.Charset = "";
            string fileName = "LedgerReportOn" + System.DateTime.Now.ToString("MM/dd/yyyy") + ".doc";
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/msword";  
            Response.AddHeader("Content-Disposition", "attachment;filename="+fileName );
            gridLedgerReport.GridLines = GridLines.Both;
            gridLedgerReport.RenderControl(hw);  
            Response.Write(sw.ToString());            
            Response.End();


However, this code is not working for me. As per my understanding, a download Save or Open option was to be shown. However, this code shows no effect on my application. On debugging, the all codes executes but no response.

Any help on how can I export my gridview to word.
Thanks in advance...
Posted

You can use Microsoft Open XML SDK to produce Word document on the server side. Please see my past answers:
How to add microsoft excel 15.0 object library from Add Reference in MS Visual Studio 2010[^],
Microsot office Interop[^].

See also Microsoft articles warning against the use of 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[^].

—SA
 
Share this answer
 
Comments
Codes DeCodes 14-Jul-15 11:48am    
thanks for the link,,, will try this and let you know....
Sergey Alexandrovich Kryukov 14-Jul-15 12:00pm    
Very good. I hope you will accept the answer formally. In all cases, your follow-up questions will be welcome.
—SA
Codes DeCodes 29-Jul-15 11:17am    
I removed update panel from the page where in was the code to export. Now its working...
I removed update panel from the page where in was the code to export. Now its working...
 
Share this answer
 

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