BS导出Excel实例
来源:互联网 发布:剑雨江湖进阶数据 编辑:程序博客网 时间:2024/06/14 08:32
1、 public void GridViewToExcel(System.Web.UI.WebControls.GridView gv, string FileType, string FileName)
{
for (int i = 0; i < gv.Rows.Count; i++)
{
foreach (System.Web.UI.WebControls.TableCell tc in gv.Rows[i].Cells)
{
foreach (System.Web.UI.Control c in tc.Controls)
{
if (c.GetType() == typeof(System.Web.UI.WebControls.TextBox))
{
tc.Controls.Clear();
tc.Text = ((System.Web.UI.WebControls.TextBox)c).Text;
}
}
}
}
System.Web.HttpContext.Current.Response.Charset = "utf-8";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename="
+ System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
System.Web.HttpContext.Current.Response.ContentType = FileType;
//this.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
gv.RenderControl(hw);
System.Web.HttpContext.Current.Response.Write(tw.ToString());
System.Web.HttpContext.Current.Response.End();
}
public override void VerifyRenderingInServerForm(Control control) { }
用法:GridViewToExcel(GridView1, "application/ms-Excel", "文件名.xls");
2、 public void ToExcel(System.Web.UI.Control ctl)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset ="UTF-8";
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctl.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
用法:ToExcel(GridView1);
3、 private static void ExportToExcel(GridView gvw,string title)
{
string fileName;
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
fileName = string.Format("Export-File {0:yyyy-MM-dd_HH_mm}.xls", DateTime.Now);
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
gvw.RenderControl(hw);
if (!string.IsNullOrEmpty(title))
{
HttpContext.Current.Response.Write("<b><center><font size=3 face=Verdana color=#0000FF>" +title +"</font></center></b>");
}
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.Response.End();
gvw.Dispose();
tw.Dispose();
hw.Dispose();
gvw = null;
tw = null;
hw = null;
}
用法:ExportToExcel(GridView1, "bgfd");
- BS导出Excel实例
- BS导出Excel
- 导出到excel 实例
- poi导出excel实例
- poi--excel --导出实例
- ssh 导出Excel实例
- POI导出Excel实例
- POI导出Excel实例
- Java 导出Excel实例
- GridView导出Excel DataTable导出Excel 实例
- jxl导出excel报表实例
- VC++ 导出Excel表格实例
- Java导出Excel文件实例
- java导出excel简单实例
- excel导入和导出实例
- JXLS-----JXLS导出Excel实例
- POI-----POI导出Excel实例
- php导入导出excel实例
- BS导出Excel
- asp.net中上传大文件
- The request failed with HTTP status 417: Expectation failed.
- WPF and Silverlight 学习笔记(二十):WPF数据绑定概述
- dll接口中使用string问题
- BS导出Excel实例
- 应聘Java,jsp,j2ee软件工程师笔试中可能出现的问题
- js对各种页面控件的验证
- SQL备份语句
- asp.net页面生命周期 各时期事件
- 定时开关机——不用修改BIOS
- dbms_random用法
- 加密web.config连接字符串
- Flex3 Effect效果