asp.net GridviewÖеÄÄÚÈݵ¼³öµ½Excel
1£ºGridviewÖеÄÄÚÈݵ¼³öµ½Excel¡¡
¡¡Asp.net 2.0ÖÐÐÂÔöµÄgridview¿Ø¼þ£¬ÊÇÊ®·ÖÇ¿´óµÄÊý¾Ýչʾ¿Ø¼þ£¬ÔÚÇ°ÃæµÄϵÁÐÎÄÕÂÀ·Ö±ðչʾÁËÆäÖкܶàµÄ»ù±¾Ó÷¨ºÍ¼¼ÇÉ¡£ÔÚ±¾ÎÄÖУ¬½«¼ÌÐøÌ½ÌÖÓйصļ¼ÇÉ¡£
¡¡¡¡Ò»¡¢GridviewÖеÄÄÚÈݵ¼³öµ½Excel
¡¡¡¡ÔÚÈÕ³£¹¤×÷ÖУ¬¾³£Òª½«gridviewÖеÄÄÚÈݵ¼³öµ½excel±¨±íÖÐÈ¥£¬ÔÚasp.net 2.0ÖУ¬Í¬Ñù¿ÉÒԺܷ½±ãµØÊµÏÖ½«Õû¸ögridviewÖеÄÄÚÈݵ¼³öµ½excel±¨±íÖÐÈ¥£¬ÏÂÃæ½éÉÜÆä¾ßÌå×ö·¨£º
¡¡¡¡Ê×ÏÈ£¬½¨Á¢»ù±¾µÄÒ³Ãædefault.aspx
£¼form id="form1" runat="server"£¾
£¼div£¾
£¼asp:GridView ID="GridView1" runat="server"£¾
£¼/asp:GridView£¾
£¼/div£¾
£¼br/£¾
£¼asp:Button ID="BtnExport" runat="server" OnClick="BtnExport_Click"
Text="Export to Excel" /£¾
£¼/form£¾
¡¡¡¡ÔÚdefault.aspx.csÖУ¬Ð´ÈëÈçÏ´úÂ룺
protected void Page_Load(object sender, EventArgs e)
{
¡¡if (!Page.IsPostBack)
¡¡{
¡¡¡¡BindData();
¡¡}
}
private void BindData()
{
¡¡string query = "SELECT * from customers";
¡¡SqlConnection myConnection = new SqlConnection(ConnectionString);
¡¡SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
¡¡DataSet ds = new DataSet();
¡¡ad.Fill(ds, "customers");
¡¡GridView1.DataSource = ds;
¡¡GridView1.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
¡¡// Confirms that an HtmlForm control is rendered for
}
protected void Button1_Click(object sender, EventArgs e)
{
¡¡Response.Clear();
¡¡Response.AddHeader("content-disposition","attachment;filename=FileName.xls");
¡¡Response.Charset = "gb2312";
¡¡Response.ContentType = "application/vnd.xls";
¡¡System.IO.StringWriter stringWrite = new System.IO.StringWriter();
¡¡System.Web.UI.HtmlTextWriter htmlWrite =new HtmlTextWriter(stringWrite);
¡¡GridView1.AllowPaging = false;
¡¡BindData();
¡¡GridView1.RenderControl(htmlWrite);
¡¡Response.Write(stringWrite.ToString());
¡¡Response.End();
¡¡GridView1.AllowPaging = true;
¡¡BindData();
}
protected void paging(object sender,GridViewPageEventArgs e)
{
¡¡GridView1.PageIndex = e.NewPageIndex;
¡¡BindData();
}
¡¡¡¡ÔÚÉÏÃæ
Ïà¹ØÎĵµ£º
ASP.NETµÄ¹¤×÷ÔÀí
ÔÚ¶àÊý³¡ºÏÏ£¬¿ÉÒÔ½«ASP.NETÒ³Ãæ¼òµ¥µØ¿´³ÉÒ»°ãµÄHTMLÒ³Ãæ£¬Ò³Ãæ°üº¬±ê¼ÇÓÐÌØÊâ´¦Àí·½Ê½µÄһЩ´úÂë¶Î¡£µ±°²×°.NETʱ£¬±¾µØµÄIIS Web·þÎñÆ÷×Ô¶¯ÅäÖóɲéÕÒÀ©Õ¹ÃûΪ.aspxµÄÎļþ£¬ÇÒÓÃASP.NETÄ£¿é(ÃûΪaspnet_isapi.dllµÄÎļþ)´¦ÀíÕâЩÎļþ¡£
´Ó¼¼ÊõÉϽ²£¬ASP.NETÄ£¿é·ÖÎöASPXÎļþµÄÄÚÈÝ£¬²¢½«ÎļþÄÚÈ ......
±È½ÏºÃµÄÊý¾Ý¿â·ÃÎÊÀà
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data;
using System.Configuration;
namespace PublicClass
{
/// <summary> ///
&n ......
ASP.NETÎļþÏÂÔØº¯ÊýʹÓÃÊÇʲôÇé¿öÄØ£¿ÔÚÄãµÄPage_LoadÖÐÌí¼ÓÕâÑùµÄ´úÂë:
Page.Response.Clear();
bool success = ResponseFile(Page.Request, Page.Response, "Ä¿µÄÎļþÃû³Æ", @"Ô´Îļþ·¾¶", 1024000);
if (!success) Response.Write("ÏÂÔØÎļþ³ö´í£¡"); Page.Response.End();
ASP.NETÎļþÏÂÔØº¯Êý´úÂëΪ:
publi ......
string filepath = FileUpload1.FileName; &nb ......
ÓÐʱºòÐèÒª¶¯Ì¬µÄÉèÖà WebService µÄÖ·£¬ÕâÑù·¢²¼µ½²»Í¬µÄ·þÎñÆ÷ʱ¾ÍÒªÖØÐÂÉú³É£¬Îª´ËÎÒÃÇÐèÒªÔÚweb.configÖж¯Ì¬ÅäÖÃWebServiceµÄµØÖ·£¬ÔÚÍøÉϲéÁ˺ܶà×ÊÁÏ£¬ÆäÖÐÕâÖÖ·½·¨¸Ð¾õºÜºÃÓÃÒ²ºÜºÃʵÏÖ£¬ÔÎÄVB.NETʵÏÖ¡£±¾ÈËÒѸÄΪC#°æ
¡¡¡¡Ê×ÏÈÊÖ¶¯µÄÌí¼ÓÒ»¸öWebÒýÓã¨Õâ¸ö¾Í²»ÓÃ˵Á˰ɣ©
¡¡¡¡È»ºóÐ޸ı¾µØµÄ´úÀíÀࣨÌí¼ÓÒ»¸öÐ ......