/// <summary>
/// 导出Excel,导出所有列
/// </summary>
/// <param name="Response"></param>
/// <param name="strFileName"></param>
/// <param name="strTitle"></param>
/// <param name="gv"></param>
public static void ExportToExcel(HttpResponse Response, GridView gv, string strFileName, string strTitle)
{
for (int j = 1; j <= gv.Rows.Count; j++)
{
if (1 == j % 2)
{
gv.Rows[j - 1].BackColor = System.Drawing.Color.FromName("#E7E7FF");
gv.Rows[j - 1].ForeColor = System.Drawing.Color.FromName("#4A3C8C");
}
else
{
gv.Rows[j - 1].BackColor = System.Drawing.Color.FromName("#E7E7FF");
gv.Rows[j - 1].ForeColor = System.Drawing.Color.FromName("#4A3C8C");
}
} gv.AllowPaging = false;
gv.AllowSorting = false; gv.BorderColor = System.Drawing.Color.FromName("#6600CC");
gv.HeaderRow.BackColor = System.Drawing.Color.FromName("#6600CC"); gv.HeaderRow.ForeColor = System.Drawing.Color.White; // 清楚缓冲区流中内容 Response.Clear();
// 设为缓冲输出
Response.Buffer = true;
Response.Charset = "GB2312";
string strtime = DateTime.Now.ToString("yyyyMMdd");
//strtime = strtime.Replace("/", "").Replace("-", "");
//strtime = strtime.Substring(0, strtime.IndexOf(" "));
string str = string.Format("attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(strFileName.Replace(" ", ""), System.Text.Encoding.UTF8)
+ "[{0}].xls", strtime);
Response.AppendHeader("Content-Disposition", str);
// 导出Excel格式,其中filename为Excel文件的名字,可根据要求定义
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
// 设置输出流为默认。注意:设置为简体中文utf8有时还好出错
Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); // 设置输出文件类型为excel文件。
Response.ContentType = "application/ms-excel";
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
gv.Caption = strTitle;
gv.AllowPaging = false;
gv.HeaderStyle.Font.Bold = true;
gv.HeaderStyle.Font.Size = 10;
gv.Font.Size = 10;
gv.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString()); // 刷新设计器加载程序的挂起更改
Response.Flush();
Response.End();
}
/// 导出Excel,导出所有列
/// </summary>
/// <param name="Response"></param>
/// <param name="strFileName"></param>
/// <param name="strTitle"></param>
/// <param name="gv"></param>
public static void ExportToExcel(HttpResponse Response, GridView gv, string strFileName, string strTitle)
{
for (int j = 1; j <= gv.Rows.Count; j++)
{
if (1 == j % 2)
{
gv.Rows[j - 1].BackColor = System.Drawing.Color.FromName("#E7E7FF");
gv.Rows[j - 1].ForeColor = System.Drawing.Color.FromName("#4A3C8C");
}
else
{
gv.Rows[j - 1].BackColor = System.Drawing.Color.FromName("#E7E7FF");
gv.Rows[j - 1].ForeColor = System.Drawing.Color.FromName("#4A3C8C");
}
} gv.AllowPaging = false;
gv.AllowSorting = false; gv.BorderColor = System.Drawing.Color.FromName("#6600CC");
gv.HeaderRow.BackColor = System.Drawing.Color.FromName("#6600CC"); gv.HeaderRow.ForeColor = System.Drawing.Color.White; // 清楚缓冲区流中内容 Response.Clear();
// 设为缓冲输出
Response.Buffer = true;
Response.Charset = "GB2312";
string strtime = DateTime.Now.ToString("yyyyMMdd");
//strtime = strtime.Replace("/", "").Replace("-", "");
//strtime = strtime.Substring(0, strtime.IndexOf(" "));
string str = string.Format("attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(strFileName.Replace(" ", ""), System.Text.Encoding.UTF8)
+ "[{0}].xls", strtime);
Response.AppendHeader("Content-Disposition", str);
// 导出Excel格式,其中filename为Excel文件的名字,可根据要求定义
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
// 设置输出流为默认。注意:设置为简体中文utf8有时还好出错
Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); // 设置输出文件类型为excel文件。
Response.ContentType = "application/ms-excel";
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
gv.Caption = strTitle;
gv.AllowPaging = false;
gv.HeaderStyle.Font.Bold = true;
gv.HeaderStyle.Font.Size = 10;
gv.Font.Size = 10;
gv.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString()); // 刷新设计器加载程序的挂起更改
Response.Flush();
Response.End();
}
解决方案 »
- checkbox列的全选
- 图片大小修改后 图片上的控件相对坐标移动问题 类似与地图上的热点 在放大缩小地图时 热点的坐标改变到相对的位置
- 如何获取XML文件节点相关列表?....
- 自动触发事件里的参数该怎么写?
- aspx里只有<%@ Page %>是什么意思?
- 求C# 根据汽车所在经纬度,怎么在地图上面添加图片
- VC的DLL中的、自定义结构的、引用的、数据类型C#怎么调用
- 求一个winform里DataGrid绑定主子表的代码!!
- 如何在GridView中,为自动生成的删除按钮添加删除提示
- 如何在WINDOWS98下分发有水晶报表 ( crystal report ) 的程序
- 访问服务器报Access to the path is denied
- 求个正则,多谢
这是文件下载header,关闭正常。
string str = string.Format("attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(strFileName.Replace(" ", ""), System.Text.Encoding.UTF8) + "[{0}].xls", strtime); Response.AppendHeader("Content-Disposition", str);