ASP.NET技巧:数据岛出到Excel最为简易的方法
只需将ContentType设置为"application/vnd.ms-excel",表示以Excel方式输出.
代码如下:
DataToExcel.aspx:
<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="DataToExcel.aspx.cs"Inherits="DataToExcel"%>
<htmlxmlns="
<headrunat="server">
<title>DataToExcel</title>
</head>
<body>
<formid="form1"runat="server">
<asp:GridViewID="GridView1"runat="server">
</asp:GridView>
</form>
</body>
</html>DataToExcel.aspx.cs
usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.Data.SqlClient;
publicpartialclassDataToExcel:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!this.IsPostBack)
{
this.Response.ContentType="application/vnd.ms-excel";
stringConnStr="server=localhost;uid=sa;pwd=;database=northwind";
SqlConnectionConn=newSqlConnection(ConnStr);
Conn.Open();
stringsqlcmd="selectlastname,firstname,title,address,cityfromemployees";
SqlCommandcmd=newSqlCommand(sqlcmd,Conn);
SqlDataAdapteradapter=newSqlDataAdapter(cmd);
DataSetds=newDataSet();
adapter.Fill(ds);
this.GridView1.DataSource=ds.Tables[0].DefaultView;
this.GridView1.DataBind();
}
}
}
相关文章
- [.NET控件]Telerik RadControls for ASP.NET AJAX 2008 Q1 net 2.0 Web.UI「建议收藏」
- [接上篇]在Window10/11的Linux子系统Docker上部署VB.NET Asp.Net Core WebAPI应用
- 使用POI生成Excel文件,可以自动调整excel列宽详解编程语言
- Linux.Net:开启新技术之旅(linux.net)
- 导入Excel数据到Oracle中的完美解决方案(oracle导入excel)
- .net搭建ASP.NET应用程序在Linux系统上(linux搭建asp)
- 利用Excel快速连接MySQL数据库(excel连接mysql)
- Linux上安装.NET:提高开发效率,拓展技术栈(linux安装.net)
- Oracle NET突破技术界限,精彩开启新纪元(oracle net??)
- 应用使用Linux环境部署.Net应用程序(linux部署.net)
- NET环境下操作Oracle数据库的技巧(.net操作oracle)
- 面向企业的Net框架开发与Oracle集成(net框架oracle)
- asp.net下获取Excel所有的工作表名称
- asp.net操作excel的实现代码
- asp.net下生成英文字符数字验证码的代码
- asp.net选择excel类型文件,利用Dos命令成批复制文件
- ASP.NET操作Excel备忘录
- asp.net下用Aspose.Wordsfor.NET动态生成word文档中的数据表格的方法
- 答你所问.NET小常识方便学习asp.net的朋友
- ASP.NET导出Excel打开时提示:与文件扩展名指定文件不一致解决方法
- ASP.NET(C#)读取EXCEL另加解决日期问题的方法分享
- asp.net导出Excel类库代码分享
- 三步将Asp.Net页面输出到EXCEL里
- ASP.NET中上传并读取Excel文件数据示例