DataGrid数据导入excel或word
2008-03-05 13:38
295 查看
以下的C#代码:
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection con=new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sda=new SqlDataAdapter();
sda.SelectCommand=new SqlCommand("select * from txtInsert",con);
DataSet ds=new DataSet();
sda.Fill(ds,"emp");
this.DgSource.DataSource=ds.Tables["emp"];
this.DgSource.DataBind();
con.Close();
}
/**//// <summary>
/// 导出Datagrid里所有数据到Office
/// </summary>
/// <param name="grdTemp">要导出的Datagrid</param>
/// <param name="dsTemp">Datagrid的数据源</param>
public void DataGridToExcel(DataGrid grdTemp,DataSet dsTemp)
{
grdTemp.AllowPaging=false; //设置不能分页
grdTemp.DataSource=dsTemp; //重新绑定数据源
grdTemp.DataBind();
//常规导出方法
System.IO.StringWriter SW = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter HTW=new System.Web.UI.HtmlTextWriter(SW);
grdTemp.RenderControl(HTW);
//Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以
Response.Buffer=true;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/vnd.ms-excel";
//Response.ContentType是输出流的 HTTP MIME 类型
//Response.ContentType --- word文件
//application/vnd.ms-excel --- excel文件
//
Response.Charset="utf-8";
Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");
Response.AddHeader("Content-Disposition", "attachment;filename=aaa.xls");
//attachment --- 作为附件下载
//inline --- 在线打开
//filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)
//进行进行编码,以解决文件名乱码的问题
Response.Write(SW.ToString());
Response.Flush();
Response.Close();
}
Web 窗体设计器生成的代码#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.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection con=new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sda=new SqlDataAdapter();
sda.SelectCommand=new SqlCommand("select * from txtInsert",con);
DataSet ds=new DataSet();
sda.Fill(ds,"emp");
this.DgSource.DataSource=ds.Tables["emp"];
this.DataGridToExcel(this.DgSource,ds);
con.Close();
}
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection con=new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sda=new SqlDataAdapter();
sda.SelectCommand=new SqlCommand("select * from txtInsert",con);
DataSet ds=new DataSet();
sda.Fill(ds,"emp");
this.DgSource.DataSource=ds.Tables["emp"];
this.DgSource.DataBind();
con.Close();
}
/**//// <summary>
/// 导出Datagrid里所有数据到Office
/// </summary>
/// <param name="grdTemp">要导出的Datagrid</param>
/// <param name="dsTemp">Datagrid的数据源</param>
public void DataGridToExcel(DataGrid grdTemp,DataSet dsTemp)
{
grdTemp.AllowPaging=false; //设置不能分页
grdTemp.DataSource=dsTemp; //重新绑定数据源
grdTemp.DataBind();
//常规导出方法
System.IO.StringWriter SW = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter HTW=new System.Web.UI.HtmlTextWriter(SW);
grdTemp.RenderControl(HTW);
//Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以
Response.Buffer=true;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/vnd.ms-excel";
//Response.ContentType是输出流的 HTTP MIME 类型
//Response.ContentType --- word文件
//application/vnd.ms-excel --- excel文件
//
Response.Charset="utf-8";
Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");
Response.AddHeader("Content-Disposition", "attachment;filename=aaa.xls");
//attachment --- 作为附件下载
//inline --- 在线打开
//filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)
//进行进行编码,以解决文件名乱码的问题
Response.Write(SW.ToString());
Response.Flush();
Response.Close();
}
Web 窗体设计器生成的代码#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.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection con=new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
con.Open();
SqlDataAdapter sda=new SqlDataAdapter();
sda.SelectCommand=new SqlCommand("select * from txtInsert",con);
DataSet ds=new DataSet();
sda.Fill(ds,"emp");
this.DgSource.DataSource=ds.Tables["emp"];
this.DataGridToExcel(this.DgSource,ds);
con.Close();
}
相关文章推荐
- DataGrid中的数据导入到Word和Excel_C#
- DataGrid中的数据导入到Word和Excel_VB
- DataGrid中的数据导入到Word和Excel_C#
- DataGrid中的数据导入到Word和Excel_C#
- DataGrid数据导入excel或word
- 将DataGrid中的数据完全导入到EXCEL中
- Excel数据导入DataGrid中
- 如何实现数据从 txt/word 自动导入到excel中
- 将DataGrid中的数据完全导入到EXCEL中
- DataGrid数据导入Excel
- 怎样实现excel的数据导入到datagrid中?
- 将EXCEL 数据导入到 DataGrid中
- excel数据导入到datagrid
- 学习笔记—将数据导入Word或Excel
- 将datagrid数据导入到excel的方法
- 将数据导入到Excel和Word中
- excel数据导入到datagrid
- 使用VBA将Excel数据导入到Word的表格中
- DataGrid数据导入Excel
- ASP.NET 把DataGrid数据导入到Excel中