⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 createexcel.aspx.cs

📁 This is not a very mean things
💻 CS
字号:
//-----------------------------------------------------------------------
//
// WebExpert.NET 1.0 
//
// (c) 2003, www.AspCool.com. All rights reserved.
// ASP酷技术网 版权所有
//
// 该源码下载自:http://www.51aspx.com
// 邮箱:tim@aspcool.com
//
// 版权声明:本程序仅供学习使用,你也可以修改后在网站上使用,但使用时必
// 须保留ASP酷技术网(www.AspCool.com)的版权信息和链接。本程序随《ASP.NET
// 网站建设专家》一书赠送,未经作者同意,不得随意修改、传播。
//
// 描述:
//   此文件包含下面的类:
//       CreateExcel
//
// 作者:     王保健
// 时间:     2005-01-15
//
//------------------------------------------------------------------------
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.IO;

namespace AspCool.WebExpert.Excel
{
	/// <summary>
	/// CreateExcel 的摘要说明。
	/// </summary>
	public class CreateExcel : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
		protected System.Web.UI.WebControls.TextBox txtFile;
		protected System.Web.UI.WebControls.Button btnCreateExcel;
		protected System.Web.UI.WebControls.Button btnExport;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!this.IsPostBack)
			{
				GuestBooksDB GuestBook = new GuestBooksDB();
				DataSet ds = GuestBook.GetGuestBooks();
				DataGrid1.DataSource = ds;
				DataGrid1.DataBind();
			}
		}

		#region Web 窗体设计器生成的代码
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnExport.Click += new System.EventHandler(this.btnExport_Click);
			this.btnCreateExcel.Click += new System.EventHandler(this.btnCreateExcel_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnExport_Click(object sender, System.EventArgs e)
		{
			CreataExcelByOWC();
		}
		private void CreateExcelByHTW()
		{
            StringWriter sw=new StringWriter(); 
			HtmlTextWriter htw=new HtmlTextWriter(sw); 
			DataGrid1.RenderControl(htw); 
			Response.ContentType ="application/vnd.ms-excel";
			Response.Write (sw.ToString()); 
			Response.End();
		}
		
		private void CreataExcelByOWC()
		{
			GuestBooksDB GuestBook = new GuestBooksDB();
			// 定义和获取DataSet。
			DataSet ds = GuestBook.GetGuestBooks();

			// 定义OWC.SpreadsheetClass类
			OWC.SpreadsheetClass sheet = new OWC.SpreadsheetClass();

			// 定义要导入数据的初始行和列的位置
			int row = 1;
			int col = 1;

			// 遍历table中的每一行
			foreach (DataRow dr in ds.Tables[0].Rows)
			{
				// 在遍历table列开始之前设置初始列的位置
				col = 1;
				// 遍历table中的每一列
				foreach (DataColumn dc in ds.Tables[0].Columns)
				{
					// 填写内容
					sheet.ActiveSheet.Cells[row,col] = dr[dc].ToString();
					// 移到下一列
					col++;
				}
				// 移到下一行
				row++;
			}

			// 导出到Excel文件中
			sheet.ActiveSheet.Export(Server.MapPath(".")+"\\"+txtFile.Text ,OWC.SheetExportActionEnum.ssExportActionNone);
			
			// 显示导入数据的Excel文件
			Response.Redirect(txtFile.Text);
		
		}

		private void btnCreateExcel_Click(object sender, System.EventArgs e)
		{
			CreateExcelByHTW(); 
		}
	
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -