GridView导出为Excel
protected void btExcel_Click(object sender, EventArgs e)
??? {
??????? DataSet ds = new DataSet();
??????? ds = Session["myDataSet"] as DataSet;
??????? DataTable DT = ds.Tables[0];
??????? //生成將要存放結果的Excel文件的名稱
??????? string NewFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
??????? //轉換為物理路徑
??????? NewFileName = Server.MapPath("~/Temp/" + NewFileName);
??????? //根據模板正式生成該Excel文件
??????? File.Copy(Server.MapPath("~/Temp/Module01.xls"), NewFileName, true);
??????? //建立指向該Excel文件的數據庫連接
??????? string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + NewFileName + ";Extended Properties='Excel 8.0;'";
??????? OleDbConnection Conn = new OleDbConnection(strConn);
??????? //打開連接,為操作該文件做準備
??????? Conn.Open();
??????? OleDbCommand Cmd = new OleDbCommand("", Conn);
??????? foreach (DataRow DR in DT.Rows)
??????? {
??????????? string XSqlString = "insert into [Sheet1$]";
??????????? XSqlString += "([舉辦年度],[名稱],[主題],[舉辦時間],[舉辦地點],[承辦單位],[預算],[實際規模],[評價]) values(";
??????????? XSqlString += "'" + DR["Year"] + "',";
??????????? XSqlString += "'" + DR["CampName"] + "',";
??????????? XSqlString += "'" + DR["Topic"] + "',";
??????????? XSqlString += "'" + DR["HeldTime"] + "',";
??????????? XSqlString += "'" + DR["HeldPlace"] + "',";
??????????? XSqlString += "'" + DR["UndertakeDep"] + "',";
??????????? XSqlString += "'" + DR["Budget"] + "',";
??????????? XSqlString += "'" + DR["ActualScale"] + "',";
??????????? XSqlString += "'" + DR["Remarks"] + "')";
??????????? Cmd.CommandText = XSqlString;
??????????? Cmd.ExecuteNonQuery();
??????? }
??????? //操作結束,關閉連接
??????? Conn.Close();
??????? //打開要下載的文件,并把該文件存放在FileStream中
??????? System.IO.FileStream Reader = System.IO.File.OpenRead(NewFileName);
??????? //文件傳送的剩余字節數:初始值為文件的總大小
??????? long Length = Reader.Length;
??????? Response.Buffer = false;
??????? Response.AddHeader("Connection", "Keep-Alive");
??????? Response.ContentType = "application/octet-stream";
??????? Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode("夏令營信息.xls"));
??????? Response.AddHeader("Content-Length", Length.ToString());
??????? byte[] Buffer = new Byte[10000];??//存放欲發送數據的緩沖區
??????? int ByteToRead;???????????//每次實際讀取的字節數
??????? while (Length > 0)
??????? {
??????????? //剩余字節數不為零,繼續傳送
??????????? if (Response.IsClientConnected)
??????????? {
??????????????? //客戶端瀏覽器還打開著,繼續傳送
??????????????? ByteToRead = Reader.Read(Buffer, 0, 10000);?????//往緩沖區讀入數據
??????????????? Response.OutputStream.Write(Buffer, 0, ByteToRead);?//把緩沖區的數據寫入客戶端瀏覽器
??????????????? Response.Flush();??????????????????//立即寫入客戶端
??????????????? Length -= ByteToRead;????????????????//剩余字節數減少
??????????? }
??????????? else
??????????? {
??????????????? //客戶端瀏覽器已經斷開,阻止繼續循環
??????????????? Length = -1;
??????????? }
??????? }
??????? //關閉該文件
??????? Reader.Close();
??????? //刪除該Excel文件
??????? File.Delete(NewFileName);
?????????? }
方法2:
?
? private void Export(GridView gvUser, string Style, string FileType, string FileName)
??? {
??????? Response.ClearContent();
??????? Response.Charset = "GB2312";
??????? Response.ContentEncoding = System.Text.Encoding.UTF8;
??????? Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
??????? Response.ContentType = FileType;
??????? //page.Response.ContentType = "application/ms-excel";
??????? Page.EnableViewState = false;
??????? StringWriter sw = new StringWriter();
??????? HtmlTextWriter hw = new HtmlTextWriter(sw);
??????? //turn off paging
??????? gvUser.AllowPaging = false;
??????? BindGrid(gvUser);
??????? gvUser.RenderControl(hw);
??????? Response.Write(Style);
??????? Response.Write(sw.ToString());
??????? //page.Response.Flush();
??????? Response.End();
??????? //turn the paging on again
??????? gvUser.AllowPaging = true;
??????? BindGrid(gvUser);
??? }
??? private void BindGrid(GridView gvUser)
??? {
??????? DataSet ds = new DataSet();
??????? ds = Session["myDataSet"] as DataSet;
??????? gvUser.DataSource = ds;
??????? gvUser.DataBind();
??? }
??? public override void VerifyRenderingInServerForm(Control control)
??? {
??? }
??? protected void GridViewCamp_RowDataBound(object sender, GridViewRowEventArgs e)
??? {
??????? LinkButton deleteLB = (LinkButton)e.Row.FindControl("lbDelete");
??????? if (deleteLB != null)
??????? {
??????????? deleteLB.Attributes.Add("onclick", "return confirm('您確實要刪除所選擇的數據項嗎?');");
??????? }
??????? if (e.Row.RowType == DataControlRowType.DataRow)
??????? {
????????? e.Row.Cells[0].Attributes["class"] = "text";
??????? }
??? }
?protected void btExcel_Click(object sender, EventArgs e)
??? {
??? string strStyle = @"<style> .text { mso-number-format:\@; }</style>";
??????? //string strStyle = "";
??????? Export(GridViewCamp,strStyle,"application/ms-excel", "myExcel.xls");
??? }
前臺界面設置:EnableEventValidation="false"
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeFile="CampInfo.aspx.cs" Inherits="Default3" %>
轉載于:https://www.cnblogs.com/longlong434/archive/2008/09/21/1295285.html
總結
以上是生活随笔為你收集整理的GridView导出为Excel的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言课程结束,【计算机】程序设计——C
- 下一篇: struct and union