惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
D
Docker
Blog — PlanetScale
Blog — PlanetScale
罗磊的独立博客
美团技术团队
V
V2EX
Last Week in AI
Last Week in AI
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
宝玉的分享
宝玉的分享
Microsoft Security Blog
Microsoft Security Blog
Microsoft Azure Blog
Microsoft Azure Blog
人人都是产品经理
人人都是产品经理
M
MIT News - Artificial intelligence
P
Proofpoint News Feed
B
Blog RSS Feed
博客园_首页
B
Blog
博客园 - 叶小钗
I
InfoQ
WordPress大学
WordPress大学
L
LangChain Blog
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
A
About on SuperTechFans
The GitHub Blog
The GitHub Blog
The Register - Security
The Register - Security
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
Latest news
Latest news
W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
Cyberwarzone
Cyberwarzone
Scott Helme
Scott Helme
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
CERT Recently Published Vulnerability Notes
C
CXSECURITY Database RSS Feed - CXSecurity.com
Recent Commits to openclaw:main
Recent Commits to openclaw:main
N
News and Events Feed by Topic
S
Secure Thoughts
The Hacker News
The Hacker News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News

博客园 - 章立民研究室

我在點部落 笑談我的學習之路 下一代网页开发技术Silverlight——笑谈程式人生系列讲座之二 确保您的Silverlight 1.0运行时间组件是最新版本 ToolkitScriptManager VS ScriptManager 读者问与答 如何于撰写 Silverlight 1.0 的XAML时拥有Intellisense Silverlight 1.0 RC SDK 预览 新世代网站开发技术ASP .NET AJAX——章立民北京研讨会 PPT下载 新世代网站开发技术ASP .NET AJAX——章立民北京研讨会邀请函 读者询问是否一定要安装ASP.NET AJAX Extensions 答读者问 SQL Server - 请问数据库字段加密问题 读者“翔”询问如何于asp.net中删除目录 答读者问 Visual C# - 读者询问如何复制目录以及目录下所有的子目录与文件 Visual C# 2005 – 如何使用通配符 *.* 复制所有文件 新书出版了 SQL Server 2005 - 如何利用CLR存储过程读取与写入二进制影像数据
读者问与答
章立民研究室 · 2007-04-24 · via 博客园 - 章立民研究室

原发问问题:

章老师:

自从ASP.NET拜读老师大作受益良多,由 Visual C# 2005 大作发现了EXCEL资料连线方式,然而在ASP.NET方面如何处理EXCEL却发现了下述问题。

ASP.NET C# GridView 转换为 Excel ,发现 EXCEL 笔数只要超过38笔,就会显示『无法读取档案』,请问是为何?造成EXCEL档无法开启,但是用纯GridView显示资料却正常。

DataSet myDataSet = new DataSet();

............................................................

.............................................................

GridView1.DataSource = myDataSet.Tables["tb"];

GridView1.DataBind();

Response.AddHeader("content-disposition", "attachment; filename= "a.xls");

Response.ContentType = "application/vnd.ms-excel";

System.IO.StringWriter tw = new System.IO.StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(tw);

GridView1.RenderControl(hw);

Response.Write(tw.ToString());

Response.End();

亲爱的amicky读者,您好

很感谢您对于章立民研究室的支持,有关于您提到的问题,回复如下:

首先,要说明的是如何连结至Excel档案。图表1所示者是网页范例的设计画面,从画面中可以清楚的看到,GridView控制项使用SqlDataSource控制项来连结至Excel档案:

 

图表1

在新增GridView控制项的资料连结时,请按下「加入连接」对话方块的「变更连结」按钮,然后如图表2所示,选取「资料提供者」下拉式清单的 .NET Framework Data Provider for OLE DB项目,按下「确定」按钮回到「加入连结」对话方块:

 

图表2

请如图表3所示,选取OLE DB提供者」下拉式清单的Microsoft Jet 4.0 OLE DB Prodiver项目,并按下「资料连结」按钮:

 

图表3

请于「资料连结内容」对话方块中,选取作为资料来源的Excel档案之后,按下「确定」按钮回到「加入连接」对话方块,如图表4所示:

 

图表4

请按下「加入连接」对话方块中的「进阶」按钮,将「进阶属性」对话方块的Extended Properties属性设定为Excel 8.0,完成之后按下「确定」按钮,回到「加入连接」对话方块,如图表5所示:

 

图表5

接下来,请自行依照「加入连接」对话方块的提示,继续完成新增GridView控制项之资料连结的作业。

加入资料连结之后,我们在画面上配置了一个Button控制项,以便当使用者用滑鼠按一下按钮时,会将目前GridView控制项的资料写出,产生Excel档案,请替Button控制项的Click事件处理常式撰写如下所示的程式码:

protected void Button1_Click(object sender, EventArgs e)
{
 System.IO.StringWriter tw = new System.IO.StringWriter();
 HtmlTextWriter hw = new HtmlTextWriter(tw);
 string excelFileName = "测试Excel档案.xls";
 
 Context.Response.ContentType = "application/x-excel";
 Context.Response.AddHeader("content-disposition", "attachment;filename=" +
   Server.UrlEncode(excelFileName));
 this.GridView1.RenderControl(hw);
 Context.Response.Write(tw.ToString());
 Context.Response.End();
}

请覆写VerifyRenderingInServerForm方法,以便让使用者能够下载Excel档案,如下所示:

public override void VerifyRenderingInServerForm(Control control)

{

 

}

请替GridView控制项的RowDataBound事件处理常式撰写如下所示的程式码,以便让GridView控制项每列资料的第一个栏位能够显示该笔资料的资料编号:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
 // 如果系结的资料列不是 GridView 控制项的页首,
 // 则将资料列第一个栏位之 Label 控制项的文字设定为「资料列数加1」,
 // 以便代表资料编号。
 if (e.Row.RowType != DataControlRowType.Header)
 {
  e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
 }
}

网页范例的执行结果如图表6所示,请用滑鼠按一下「产生Excel按钮,以便将目前GridView控制项的内容写入Excel档案,并开始下载,如图表7所示:

 

图表6

 

图表7

最后,测试Excel档案.xls档案的内容如图表8所示,大家会发现资料的内容正好与GirdView控制项的内容相符,而这也是我们希望的结果:

 

图表8