GridView导出到excel
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /// <summary>
2 /// 导出到Excel
3 /// </summary>
4 /// <param name="sender"></param>
5 /// <param name="e"></param>
6 protected void ButtonInport_Click(object sender, EventArgs e)
7 {
8 DataTable dtExcel = ((DataTable)Session["dtExcel"]).Copy();
9 GridViewAsset.AllowPaging = false;
10 GridViewAsset.DataSource = dtExcel;
11 GridViewAsset.DataBind();
12 string fileName = ecelHelp.GetExportFileName("NoMatchList.xls", this.GridViewAsset, Server.MapPath(""));
13 HttpResponse httpResponse = System.Web.HttpContext.Current.Response;
14 FileInfo fileinfo = new FileInfo(Server.MapPath("") + @"\" + fileName);
15 httpResponse.Clear();
16 httpResponse.ClearHeaders();
17 httpResponse.Buffer = false;
18 httpResponse.ContentType = "application/octet-stream";
19 httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileinfo.Name, System.Text.Encoding.UTF8));
20 httpResponse.WriteFile(fileinfo.FullName);
21 httpResponse.End();
22 httpResponse.Flush();
23 httpResponse.Close();
24 GridViewAsset.AllowPaging = true;
25
26 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /// <summary>
2 /// 把 GridView 呈现的内容导出到 Excel
3 /// </summary>
4 /// <param name="fileName"></param>
5 /// <param name="gv"></param>
6 public string GetExportFileName(string fileName, GridView gv, string filepath)
7 {
8 try
9 {
10
11 using (StringWriter sw = new StringWriter())
12 {
13
14 using (HtmlTextWriter htw = new HtmlTextWriter(sw))
15 {
16 // Create a form to contain the grid
17 Table table = new Table();
18 table.Style.Add("border-top", "solid 1px #cdcdcd");
19 table.Style.Add("border-left", "solid 1px #cdcdcd");
20
21 // add the header row to the table
22 if (gv.HeaderRow != null)
23 {
24 PrepareControlForExport(gv.HeaderRow);
25 table.Rows.Add(gv.HeaderRow);
26 }
27
28 // add each of the data rows to the table
29 foreach (GridViewRow row in gv.Rows)
30 {
31 PrepareControlForExport(row);
32 table.Rows.Add(row);
33 }
34
35 // add the footer row to the table
36 if (gv.FooterRow != null)
37 {
38 PrepareControlForExport(gv.FooterRow);
39 table.Rows.Add(gv.FooterRow);
40 }
41
42 // render the table into the htmlwriter
43 table.RenderControl(htw);
44 string date = System.DateTime.Now.ToString("MMddyyyy");
45 //string outFileName = "ExportExcel_" + date + "_" + DateTime.Now.Ticks.ToString() + "_" + fileName;
46 string outFileName = fileName;
47 string comPath = "Export/" + outFileName;
48 string path = filepath;// +"\\Export";
49
50 if (Directory.Exists(path) == false)
51 {
52 System.IO.Directory.CreateDirectory(path);
53 }
54 path = path + "\\" + outFileName;
55 System.Text.Encoding encode = System.Text.Encoding.Unicode;
56 StreamWriter stream = new StreamWriter(path, false, encode);
57 stream.Write(sw.ToString());
58 stream.Close();
59
60 return comPath;
61
62 }
63 }
64 }
65 catch (Exception e)
66 {
67 throw e;
68 }
69 }
将以上代码改进成公共方法:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /// <summary>
2 /// 将网格数据导出到Excel
3 /// </summary>
4 /// <param name="ctrl">网格名称(如GridView1)</param>
5 /// <param name="FileType">要导出的文件类型(Excel:application/ms-excel)</param>
6 /// <param name="FileName">要保存的文件名</param>
7 public static void GridViewToExcel(Control ctrl, string FileType, string FileName)
8 {
9 HttpContext.Current.Response.Charset = "GB2312";
10 HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;//注意编码
11 HttpContext.Current.Response.AppendHeader("Content-Disposition",
12 "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
13 HttpContext.Current.Response.ContentType = FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
14 ctrl.Page.EnableViewState = false;
15 StringWriter tw = new StringWriter();
16 HtmlTextWriter hw = new HtmlTextWriter(tw);
17 ctrl.RenderControl(hw);
18 HttpContext.Current.Response.Write(tw.ToString());
19 HttpContext.Current.Response.End();
20 }