下载:点击下载
DataGrid_Export_Excel.aspx
<%@ Page language="c#" Codebehind="DataGrid_Export_Excel.aspx.cs" Src="DataGrid_Export_Excel.aspx.cs" AutoEventWireup="false"
Inherits="Exam.DataGrid_Export_Excel" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Lion互动网络---演示:DataGrid导出到Excel当中</title>
</HEAD>
<body MS_POSITIONING="FlowLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">导出的Excel文件名:</FONT>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
<asp:Button id="Button1" runat="server" Text="Export"></asp:Button><asp:DataGrid id="DataGrid1"
runat="server" BorderWidth="1px"></asp:DataGrid>
</form>
</body>
</HTML>
DataGrid_Export_Excel.aspx.cs
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;
namespace Exam
{
/// <summary>
/// DataGrid_Export_Excel 的摘要说明。
/// </summary>
public class DataGrid_Export_Excel : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
Bind();
}
}
#region Function Custom
private void CreatCache()
{
System.Data.DataTable DT = new System.Data.DataTable();
DT.Columns.Add("ID",typeof(int));
DT.Columns.Add("Name",typeof(string));
DT.Columns.Add("AddTime",typeof(DateTime));
DT.Columns[0].AutoIncrement = true;
DT.Columns[0].AutoIncrementSeed = 1;
DT.Columns[0].AutoIncrementStep = 1;
for(int i=0;i<20;i++)
{
System.Data.DataRow dr= DT.NewRow();
dr[1] = "欢迎光临Lion互动网络,第 "+ (i+1) +" 行";
dr[2] = System.DateTime.Now.AddDays(1);
DT.Rows.Add(dr);
}
Context.Cache.Insert("Data", DT, null, DateTime.Now.AddMinutes(20), TimeSpan.Zero);
}
private void Bind()
{
if(Cache["Data"]==null)
{
CreatCache();
}
DataGrid1.DataSource = (System.Data.DataTable)Cache["Data"];
DataGrid1.DataBind();
}
#endregion
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
/// <summary>
/// 导出文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button1_Click(object sender, System.EventArgs e)
{
if(this.TextBox1.Text.Trim()!=string.Empty)
{
if(Cache["Data"]==null)
{
CreatCache();
}
System.Data.DataTable DT = (System.Data.DataTable)Cache["Data"];
OWC.SpreadsheetClass xlsheet = new OWC.SpreadsheetClass();
int row=1;
foreach(System.Data.DataRow DR in DT.Rows)
{
for(int i=0;i<3;i++)
{
xlsheet.ActiveSheet.Cells[row,i+1] = DR[i].ToString();
}
row++;
}
DT.Clear();
DT.Dispose();
xlsheet.ActiveSheet.Export(Server.MapPath(".")+"//"+this.TextBox1.Text.Trim()+".XLS",OWC.SheetExportActionEnum.ssExportActionNone);
}
}
}
}