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

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

问题描述

我有一个GridView要导出到Excel。当我使用我在线查找的示例代码时,它会将内容导出到Excel中,但由于某些原因,它也会清除导出表格外的所有网格线。



对于您的平均excel用户来说,这很容易解决,但是我需要这个解决方案为每个人工作。



那么有一种方法可以导出数据将GridView转换为Excel工作簿,使其看起来像是直接输入到Excel中?我已经粘贴了我在下面使用的代码,假设一个名为toPrint的GridView存在并具有准确的数据。

  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文件,则它将打开,并显示所有网格线(即使是导出数据之外的所有网格线)。这个唯一的问题当然是在导出之前必须从我的值中删除每个逗号和换行符。

解决方案

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

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

//放入excel网格线
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>);
// *******把你的桌子放在这里*******


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.

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

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

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

解决方案

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天全站免登陆