如何在asp.net c#中将gridview导出到MS-word

如何在asp.net c#中将gridview导出到MS-word

问题描述:

以下链接 http://www.c-sharpcorner.com/UploadFile/ 0c1bb2 / export-gridview-to-word / [ ^ ],这是我的代码是导出gridview到word。



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();





但是,此代码对我不起作用。根据我的理解,将显示下载保存或打开选项。但是,此代码对我的应用程序没有任何影响。在调试时,所有代码都执行但没有响应。



有关如何将gridview导出到word的任何帮助。

提前谢谢...



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...

您可以使用Microsoft Open XML SDK在服务器端生成Word文档。请查看我过去的答案:

如何从Add添加microsoft excel 15.0对象库MS Visual Studio 2010中的参考 [ ^ ],

Microsot office Interop [ ^ ]。



另请参阅Microsoft文章警告不要在服务器设置中使用Office互操作:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2 [ ^ ],

http://support.microsoft.com/kb/257757/en-us [ ^ ]。



-SA
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


我从页面中删除了更新面板,其中in是要导出的代码。现在它的工作......
I removed update panel from the page where in was the code to export. Now its working...