我先给出部份程序的解释,然后给出整个源代码。
dataset objdataset = new dataset();
objconn = new sqlconnection(configurationsettings.appsettings[“connectionsqlserver”].tostring());
objconn.open();//在web.config中的配置文件。
sqldataadapter objadapter = new sqldataadapter(“select top 10 * from customers where country=usa”,objconn);//这里要改成你的数据库相应的表的选择句语
objadapter.fill(objdataset);
dataview oview = new dataview(objdataset.tables[0]);
dgexcel.datasource = oview;
dgexcel.databind();
objconn.close();
objconn.dispose();
objconn = null;
上面部分是从数据库中读出数据显示到datagrid中去。
response.contenttype = “application/vnd.ms-excel”;
response.charset = “”;
//关闭 viewstate
enableviewstate = false;
system.io.stringwriter tw = new system.io.stringwriter();//将信息写入字符串
system.web.ui.htmltextwriter hw = new system.web.ui.htmltextwriter(tw);//在web窗体页上写出一系列连续的html特定字符和文本。
//此类提供asp.net服务器控件在将html内容呈现给客户端时所使用的格式化功能
//参见http://msdn.microsoft.com/library/chs/default.asp?url=/library/chs/cpref/html/frlrfsystemwebuihtmltextwriterclasstopic.asp
//获取control的html
dgexcel.rendercontrol(hw);//将datagrid中的内容输出到htmltextwriter对象中
// 把html写回浏览器
response.write(tw.tostring());
response.end();
全部代码如下:
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.drawing;
using system.web;
using system.web.sessionstate;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.data.sqlclient;
using system.configuration;
namespace commonfunction
{
/// <summary>
/// excel 的摘要说明。
/// </summary>
public class excel : system.web.ui.page
{
protected system.web.ui.webcontrols.button btngetexcel;
protected system.web.ui.webcontrols.datagrid dgexcel;
protected system.data.sqlclient.sqlconnection objconn;
private void page_load(object sender, system.eventargs e)
{
if(!ispostback)
{
dataset objdataset = new dataset();
objconn = new sqlconnection(configurationsettings.appsettings[“connectionsqlserver”].tostring());
objconn.open();
sqldataadapter objadapter = new sqldataadapter(“select top 10 * from customers where country=usa”,objconn);
objadapter.fill(objdataset);
dataview oview = new dataview(objdataset.tables[0]);
dgexcel.datasource = oview;
dgexcel.databind();
objconn.close();
objconn.dispose();
objconn = null;
}
}
#region web form designer generated code
override protected void oninit(eventargs e)
{
//
// codegen:该调用是 asp.net web 窗体设计器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 设计器支持所需的方法 – 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.btngetexcel.click += new system.eventhandler(this.btngetexcel_click);
this.load += new system.eventhandler(this.page_load);
}
#endregion
private void btngetexcel_click(object sender, system.eventargs e)
{
response.contenttype = “application/vnd.ms-excel”;
response.charset = “”;
//关闭 viewstate
enableviewstate = false;
system.io.stringwriter tw = new system.io.stringwriter();//将信息写入字符串
system.web.ui.htmltextwriter hw = new system.web.ui.htmltextwriter(tw);//在web窗体页上写出一系列连续的html特定字符和文本。
//此类提供asp.net服务器控件在将html内容呈现给客户端时所使用的格式化功能
//获取control的html
dgexcel.rendercontrol(hw);//将datagrid中的内容输出到htmltextwriter对象中
// 把html写回浏览器
response.write(tw.tostring());
response.end();
}
}
}
前台的页面:
<%@ page language=”c#” codebehind=”excel.aspx.cs” autoeventwireup=”false” inherits=”commonfunction.excel” %>
<!doctype html public “-//w3c//dtd html 4.0 transitional//en” >
<html>
<head>
<title>excel</title>
<meta content=”microsoft visual studio 7.0″ name=”generator”>
<meta content=”c#” name=”code_language”>
<meta content=”javascript” name=”vs_defaultclientscript”>
<meta content=”http://schemas.microsoft.com/intellisense/ie5″ name=”vs_targetschema”>
</head>
<body ms_positioning=”gridlayout”>
<form id=”excel” method=”post” runat=”server”>
<table id=”table1″ style=”z-index: 102; left: 16px; width: 288px; position: absolute; top: 16px; height: 178px”
cellspacing=”1″ cellpadding=”1″ width=”288″ border=”0″>
<tr>
<td>
<asp:datagrid id=”dgexcel” runat=”server” font-names=”宋体” font-size=”9pt” height=”100%” width=”100%”
borderstyle=”none” borderwidth=”1px” bordercolor=”#cc9966″ backcolor=”white” cellpadding=”4″>
<selecteditemstyle font-bold=”true” forecolor=”#663399″ backcolor=”#ffcc66″></selecteditemstyle>
<alternatingitemstyle backcolor=”#ffcc99″></alternatingitemstyle>
<itemstyle borderwidth=”2px” forecolor=”#330099″ borderstyle=”solid” bordercolor=”black” backcolor=”white”></itemstyle>
<headerstyle font-bold=”true” horizontalalign=”center” borderwidth=”2px” forecolor=”#ffffcc”
borderstyle=”solid” bordercolor=”black” backcolor=”#990000″></headerstyle>
</asp:datagrid></td>
</tr>
<tr>
<td>
<asp:button id=”btngetexcel” runat=”server” text=”把datagrid中的保存到excel”></asp:button></td>
</tr>
</table>
</form>
</body>
</html>