asp.net导出excel
来源:互联网 发布:最新mac pro安装win10 编辑:程序博客网 时间:2024/04/29 00:41
protected void DataTableToExcel(System.Data.DataTable dtData)
{
System.Web.UI.WebControls.GridView gvExport = null;
// 当前对话
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
// IO用于导出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
if (dtData != null)
{
// 设置编码和附件格式
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
curContext.Response.Charset = "utf-8";
// 导出excel文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
// 为了解决gvData中可能进行了分页的情况,需要重新定义一个无分页的GridView
gvExport = new System.Web.UI.WebControls.GridView();
gvExport.DataSource = dtData.DefaultView;
gvExport.AllowPaging = false;
gvExport.DataBind();
// 返回客户端
gvExport.RenderControl(htmlWriter);
curContext.Response.Write("<meta http-equiv=/"Content-Type/" content=/"text/html; charset=gb2312/" />" + strWriter.ToString());
curContext.Response.End();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls; public static class ToExcel
{
//将GridView数据导出至Excel中
public static string GridViewToExcel(GridView gd, string strPath, string strFileName)
{
new Help().CreateFolder(@"C:/reportforms");
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gd.RenderControl(htw);
string strHtml = sw.ToString().Trim();
string ExcelFileName = strFileName;
string FilePhysicialPathName = strPath; //Request.PhysicalApplicationPath;
//生成的Excel文件名
string objectExcelFileName = Path.Combine(FilePhysicialPathName, ExcelFileName);
if (File.Exists(objectExcelFileName))
{
File.Delete(objectExcelFileName);
}
FileStream fs = new FileStream(objectExcelFileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs, Encoding.GetEncoding("GB18030"));
bw.Write(strHtml);
bw.Close();
fs.Close();
return "";
}
}
- asp.net导出excel
- asp.net导出Excel
- asp.net导出excel
- asp.net导出Excel
- Asp.net 导出Excel
- asp.net导出excel
- asp.net 导出excel
- asp.net 导出Excel
- asp.net 导出 excel
- asp.net导出Excel
- ASP.NET Excel导出
- asp.net导出EXCEL
- ASP.NET Excel导出
- asp.net 导出Excel
- asp.net 导出Excel
- Asp.net导出Excel
- asp.net导出excel
- Asp.net 导出Excel
- 大家可以稍微喘一口气了,墨西哥航班AM098上所有乘客均未发现异常情况
- 重装Windows,只用53款全免费软件
- 努力吧,同志们
- log4j 使用笔记
- (ZT)全勇先评《南京,南京》
- asp.net导出excel
- mssql视图使用
- 最近的麻烦事
- Web Security Testing Cookbook: Systematic Techniques to Find Problems Fast
- 网站载入测试:Firefox 3.5 Beta 4完胜IE8
- Manage Your Life with Outlook For Dummies
- Non-Binary Error Control Coding for Wireless Communication and Data Storage
- (ZT)出版业改革:该死的死,该生的生
- C#中用Treeview空间显示XML文件