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

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

问题描述

我有,我想导出到Excel一个GridView。当我使用的样品code我在网上找到的,它导出内容到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?我已经贴了code我使用下面,假定存在一个名为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   我会总结一下做与code。

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