将 GridView 导出到 Excel,而不会丢失 Excel 中的网格线 [英] Export GridView to Excel without losing grid lines in Excel

查看:23
本文介绍了将 GridView 导出到 Excel,而不会丢失 Excel 中的网格线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要导出到 Excel 的 GridView.当我使用我在网上找到的示例代码时,它可以很好地将内容导出到 Excel,但由于某种原因,它还清除了导出表之外的所有网格线.

I have a GridView that I want to export to Excel. When I use the sample code I find online, it exports the content to Excel just fine, but for some reason it also clears out all grid lines outside of my exported table.

对于普通的 excel 用户来说,这很容易解决,但我需要这个解决方案适合所有人.

For your average excel user this is easy enough to fix, but I need this solution to work for everyone.

那么有没有办法将GridView中的数据导出到Excel工作簿中,使其看起来像是刚刚输入到Excel中?我已经粘贴了我在下面使用的代码,假设存在一个名为 toPrint 的 GridView 并且有准确的数据.

So then is there a way to export the data in a GridView into an Excel Workbook so that it looks like it was just typed into Excel? I've pasted the code I am using below, assume that a GridView called toPrint exists and has accurate data.

Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=" + name + "_Registration_Forms.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
Page.EnableViewState = false;

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

toPrint.RenderControl(htmlWrite);

Response.Write(stringWrite.ToString());
Response.End();

找到了一个部分解决方案.如果我导出为逗号分隔的列表并将标题设置为 CSV 文件,它会打开并显示所有网格线(即使是导出数据之外的网格线).当然,唯一的问题是必须在导出之前从我的值中删除每个逗号和换行符.

Found one partial solution. If I export as a comma-delimited list and set the header to be a CSV file, it opens fine and all grid lines (even those outside of the exported data) are showing. The only problem with this of course is having to strip out every comma and newline character from my values before exporting them.

推荐答案

下面的帖子给了我答案.http://forums.asp.net/t/1074110.aspx我将总结如何处理代码.

The following Post gave me the answer. http://forums.asp.net/t/1074110.aspx I will summarize what to do with the code.

System.IO.StringWriter dvWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter OutputGenerator = new System.Web.UI.HtmlTextWriter(dvWriter);

            //To put in the excel gridlines 
            dvWriter.Write(@"<html xmlns:x=""urn:schemas-microsoft-com:office:excel"">");
            dvWriter.Write(@"<head>
                               <xml>
                                 <x:ExcelWorkbook> 
                                    <x:ExcelWorksheets>
                                       <x:ExcelWorksheet>
                                          <x:WorksheetOptions>
                                             <x:Panes></x:Panes>
                                             <x:Print><x:Gridlines /></x:Print>
                                          </x:WorksheetOptions>
                                        </x:ExcelWorksheet>
                                      </x:ExcelWorksheets>
                                    </x:ExcelWorkbook>
                                  </xml>
                                </head>");
//*******Put your Table Body here *******

这篇关于将 GridView 导出到 Excel,而不会丢失 Excel 中的网格线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆