下面這種方法只能導出頁面的Html至Excel: 1 public static void GridExportExcel(ref System.Web.UI.WebControls.GridView dg, string fileName)
2 { 3 System.Web.HttpContext.Current.Response.Clear(); 4 System.Web.HttpContext.Current.Response.Buffer = true; 5 System.Web.HttpContext.Current.Response.Charset = "GB2312"; //設置了類型為中文防止亂碼的出現(xiàn) 6 System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls"); //定義輸出文件和文件名 7 System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//設置輸出流為簡體中文 8 System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";//設置輸出文件類型為excel文件。 9 dg.Page.EnableViewState = false; 10 System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true); 11 System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); 12 System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); 13 dg.RenderControl(oHtmlTextWriter); 14 System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString()); 15 System.Web.HttpContext.Current.Response.End(); 16 } 當要從DataSet導出至Excel,則用如下方法: 1 protected void btn_out_Click(object sender, EventArgs e)
2 { 3 DataSet ds; 4 if ((bool)ViewState["PageIndexChange"] == false) 5 { 6 Response.Write("<script>alert('對不起,沒有查詢到任何記錄,不能導出數(shù)據(jù)')</script>"); 7 return; 8 } 9 if ((int)Session["userTeam"] > 0) 10 { 11 if (ViewState["toolTip"].ToString().CompareTo("部門") == 0) 12 { 13 ds = rd.GetDeptAttenInfo((string)Session["enterId"], (string)ViewState["strValue"], 14 (DateTime)ViewState["FromTime"], (DateTime)ViewState["ToTime"], (int)ViewState["ShiftResult"], (int)ViewState["DeptChk"], (int)ViewState["AdminChk"]); 15 } 16 else 17 { 18 ds = rd.GetUserAttenInfo(int.Parse(ViewState["strValue"].ToString()), 19 (DateTime)ViewState["FromTime"], (DateTime)ViewState["ToTime"], (int)ViewState["ShiftResult"], (int)ViewState["DeptChk"], (int)ViewState["AdminChk"]); 20 } 21 } 22 else 23 { 24 ds = rd.GetUserAttenInfo(int.Parse(ViewState["strValue"].ToString()), 25 (DateTime)ViewState["FromTime"], (DateTime)ViewState["ToTime"], (int)ViewState["ShiftResult"], (int)ViewState["DeptChk"], (int)ViewState["AdminChk"]); 26 } 27 CreateExcel(ds, "1", "outExcel"); 28 //if (this.gvUser.Rows[0].Cells[0].Text == "" || this.gvUser.Rows[0].Cells[0].Text == "沒有查到數(shù)據(jù)") 29 //{ 30 // Response.Write("<script>alert('對不起,沒有查詢到任何記錄,不能導出數(shù)據(jù)')</script>"); 31 32 //} 33 //else 34 //{ 35 // GridExportExcel(ref this.gvUser, "刷卡記錄"); 36 //} 37 } 38 public void CreateExcel(DataSet ds, string typeid, string FileName) 39 { 40 HttpResponse resp; 41 resp = Page.Response; 42 resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); 43 resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName + ".xls"); 44 resp.ContentType = "application/ms-excel"; 45 string colHeaders = "", ls_item = ""; 46 47 //定義表對象與行對像,同時用DataSet對其值進行初始化 48 DataTable dt = ds.Tables[0]; 49 DataRow[] myRow = dt.Select(""); 50 // typeid=="1"時導出為EXCEL格式文件;typeid=="2"時導出為XML格式文件 51 if (typeid == "1") 52 { 53 //取得數(shù)據(jù)表各列標題,各標題之間以\t分割,最后一個列標題后加回車符 54 //for (i = 0; colHeaders += dt.Columns[i].Caption.ToString() + "\t"; 55 //colHeaders += dt.Columns[i].Caption.ToString() + "\n") 56 // //向HTTP輸出流中寫入取得的數(shù)據(jù)信息 57 // resp.Write(colHeaders); 58 //逐行處理數(shù)據(jù) 59 for (int i = 0; i < gvUser.Columns.Count; i++) 60 { 61 if (i != 0 && i != 13 && i != 14 && i != 15) 62 { 63 ls_item += this.gvUser.Columns[i].HeaderText + "\t"; 64 //ls_item += row[i].ToString() + "\n"; 65 //當前行數(shù)據(jù)寫入HTTP輸出流,并且置空ls_item以便下行數(shù)據(jù) 66 } 67 } 68 ls_item = ls_item.TrimEnd(new char[] { '\t' }); 69 ls_item += "\n"; 70 resp.Write(ls_item); 71 ls_item = ""; 72 foreach (DataRow row in myRow) 73 { 74 //在當前行中,逐列獲得數(shù)據(jù),數(shù)據(jù)之間以\t分割,結束時加回車符\n 75 for (int i = 0; i < gvUser.Columns.Count; i++) 76 { 77 if (i != 0 && i != 13 && i != 14 && i != 15) 78 { 79 if (i == 3) 80 { 81 ls_item += ((DateTime)row[((BoundField)this.gvUser.Columns[i]).DataField]).ToString("yyyy年MM月dd日") + "\t"; 82 } 83 else 84 { 85 ls_item += row[((BoundField)this.gvUser.Columns[i]).DataField].ToString() + "\t";//this.gvUser.Columns[i].HeaderText 86 } 87 } 88 } 89 ls_item = ls_item.TrimEnd(new char[] { '\t' }); 90 ls_item += "\n"; 91 resp.Write(ls_item); 92 ls_item = ""; 93 } 94 } 95 //else 96 //{ 97 // if (typeid == "2") 98 // { 99 // //從DataSet中直接導出XML數(shù)據(jù)并且寫到HTTP輸出流中 100 // resp.Write(ds.GetXml()); 101 // } 102 //} 103 //寫緩沖區(qū)中的數(shù)據(jù)到HTTP頭文件中 104 resp.End(); 105 } |
|