怎样将GridView导出到EXCEL,?可以赐小弟代码吗?谢谢,时间来不及了

ljping3000 2006-10-25 07:22:48
怎样将GridView导出到EXCEL,?可以赐小弟代码吗?谢谢,时间来不及了
...全文
138 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
andybogard 2006-10-25
  • 打赏
  • 举报
回复
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;

private void Page_Load(object sender, System.EventArgs e)
{
DataSet objDataset = new DataSet();
SqlConnection objConn = new SqlConnection();
objConn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
objConn.Open();
SqlDataAdapter objAdapter = new SqlDataAdapter("Select top 5 * 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;
if(Request.QueryString["bExcel"] == "1")
{
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();
}

}

#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.Redirect("excel.aspx?bExcel=1");
}

}
}
pp1885 2006-10-25
  • 打赏
  • 举报
回复
<%@ Page language="c#" Src="datagrid2.aspx.cs" AutoEventWireup="false" Inherits="DataGrid_import_WordExcel.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>OutPutExcel</title>
</HEAD>
<link rel="stylesheet" href="Style.css" type="text/css">
<body>
<form width="120%" id="Form1" method="post" runat="server">
<table width="120%"><tr><td>
<asp:datagrid
id="DataGrid1"
bgcolor="#efefef"
HeaderStyle-BackColor="#718BD6"
HeaderStyle-ForeColor="#FFFF66"
AlternatingItemStyle-BackColor="#FFFFFF"
itemstyle-backcolor="#FFFFFF"
runat="server">
<ItemStyle HorizontalAlign="center" Height="20"></ItemStyle>
<Columns>

</Columns>
</asp:datagrid>
</td></tr></table>
<P>
<asp:button id="Btn_Import_Excel" runat="server" Text="轉到EXCEL"></asp:button></P>
</form>
</body>
</HTML>

----------------

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;
using System.Configuration;

namespace DataGrid_import_WordExcel
{

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;
public DataRow dr;
private DataSet myDS =new DataSet();

private void Page_Load(object sender, System.EventArgs e)
{
//CreateDataSet();
Data_Load();
if(!IsPostBack)
{
//DataBind();
}
}

#region Web 敦极扢數?汜傖腔測鎢
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
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", "Excel.xls"); //絳善Excel
}

private void Data_Load()
{

SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["data"]);
SqlCommand cmd=new SqlCommand("excel",myConnection);
cmd.CommandType=CommandType.StoredProcedure;

myConnection.Open();

DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=cmd;
da.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}





}
}
xcz1943 2006-10-25
  • 打赏
  • 举报
回复
http://gridviewguy.com/ArticleDetails.aspx?articleID=197
gridview的一般问题都能够得到解决,里面还有视频教程和源代码。
冰宇枫 2006-10-25
  • 打赏
  • 举报
回复
http://dotnet.aspx.cc/article/700BD3FA-A17F-41DC-B258-0DC572625700/read.aspx

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧