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

推荐订阅源

WordPress大学
WordPress大学
L
LangChain Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
罗磊的独立博客
J
Java Code Geeks
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 叶小钗
小众软件
小众软件
博客园 - Franky
D
Docker
Google DeepMind News
Google DeepMind News
Microsoft Azure Blog
Microsoft Azure Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
宝玉的分享
宝玉的分享
C
Check Point Blog
B
Blog
V
V2EX
博客园 - 三生石上(FineUI控件)
MyScale Blog
MyScale Blog
The Cloudflare Blog
博客园 - 聂微东
博客园_首页
Engineering at Meta
Engineering at Meta

博客园 - EA_Games

ASP.NET常用代码 - EA_Games - 博客园 Oracle中的OMS的代理服务启动不了的问题 .Net术语大全 NHibernate 进阶 用Nhibernate怎么实现数据的添加、删除、修改简单程序 每个开发人员现在应该下载的十种必备工具 nhibernate入门系列: one-to-one映射 nhibernate入门系列: one-to-many映射 NHibernate快速指南 nhibernate入门系列: 对象持久化操作 ASP.NET 安全认证(三) -- 一点补充(或者叫优化) ASP.NET 安全认证(一) ASP.NET 安全认证(二) Winform如何存入、读取SQL SERVER数据库图片 无法运行ASP.NET Web应用程序 选择VB.Net还是C# ASP.NET中在线用户统计 在asp.net中访问sql server - EA_Games - 博客园 ASP.NET数据库中图片存储及读取
DataGrid中的数据导入到Word和Excel_C#
EA_Games · 2005-11-27 · via 博客园 - EA_Games

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;
using System.Data.SqlClient ;
using System.Text;

namespace DataGrid_import_WordExcel
{
/// <summary>
/// 马亚红制作 2004-12-12
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button BtnImportWord;
protected System.Web.UI.WebControls.Button Btn_Import_Excel;
protected System.Web.UI.WebControls.DataGrid DataGrid1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
CreateDataSet();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.BtnImportWord.Click += new System.EventHandler(this.BtnImportWord_Click);
this.Btn_Import_Excel.Click += new System.EventHandler(this.Btn_Import_Excel_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void ExportDataGrid(string FileType, string FileName) //从DataGrid导出
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

Response.AppendHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState =false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw =new HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
private void Btn_Import_Excel_Click(object sender, System.EventArgs e)
{
ExportDataGrid("application/ms-excel", "指数列表.xls"); //导到Excel
}

private void CreateDataSet() //建立DataSet
{
DataSet myDataSet = new DataSet("aNewDataSet");
DataTable table1 = MakeTable("ID", "Name");
myDataSet.Tables.Add(table1);
DataGrid1.DataSource=myDataSet;
DataGrid1.DataBind();
}

private DataTable MakeTable(String c1Name,String c2Name) //建表
{
int i;
DataTable myTable=new DataTable();
DataColumn myColumn;
// Add two DataColumns
myColumn = new DataColumn(c1Name,typeof(Int32)); //增加字段及设置类型
myTable.Columns.Add(myColumn);
myColumn = new DataColumn(c2Name,typeof(string));
myTable.Columns.Add(myColumn);
DataRow Dr;
for(i=1;i<11;i++) //表中增加数据
{
Dr=myTable.NewRow();
Dr[0]=i;
Dr[1]="Name" + i.ToString();
myTable.Rows.Add(Dr);
}
return myTable;
}

private void BtnImportWord_Click(object sender, System.EventArgs e) //导到Word
{
ExportDataGrid("application/ms-word", "指数列表.doc");
}
}
}