导出xls怎么老是下载.aspx页面了啊

七爷 2011-08-31 01:45:00

public class Exportxls
{
public Exportxls(){ }

public static DataTable XLSDataTable = null;

public static void ExportToxls(string[] hiddenTexts,string[] headerTexts)
{
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" +
HttpUtility.UrlEncode(DateTime.Now.Ticks.ToString() + ".xls", System.Text.Encoding.UTF8).ToString());
HttpContext.Current.Response.ContentType = "application/ms-excel";
//page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);

PrintGridViewSetting(hw, hiddenTexts, headerTexts); //设置GridView结构,然后进行打印操作

HttpContext.Current.Response.Output.Write(tw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}

//打印Excel的结构设置
public static void PrintGridViewSetting(HtmlTextWriter hw, string[] hiddenTexts, string[] headerTexts)
{
DataTable dtTemp = Exportxls.XLSDataTable.Copy();
//---------------------隐藏字段设置-------------
//移除不需要显示的字段
foreach (string columnName in hiddenTexts)
{
dtTemp.Columns.Remove(columnName);
}

GridView GV = new GridView(); //一个无分页的GridView
GV.DataSource = dtTemp;
GV.AllowPaging = false;
GV.DataBind();

//---------------------头部标题设置-------------
GV.HeaderRow.Cells.Clear();
foreach (string headerText in headerTexts)
{
TableCell cell = new TableCell();
cell.Text = headerText;
GV.HeaderRow.Cells.Add(cell);
}
//----------------------------------------------
GV.RenderControl(hw); //输出结构

}
}


调用

protected void btnExport_Click(object sender, EventArgs e)
{
//BindData()是查询返回DataView
Exportxls.XLSDataTable = BindData().ToTable();
string[] hiddenTexts = new string[]
{
"Id","CustomId","SpecialDate","Anniversary","OfficeTel","LinkNo"
};
string[] headerTexts = new string[]
{
"姓名","称呼","职务","相关客户","部门","电子邮件"
};
ExportToxls( hiddenTexts, headerTexts);
}
...全文
392 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
cm_boy 2011-12-07
  • 打赏
  • 举报
回复
我碰到了跟你一样的情况,请问,你是怎么解决的?QQ229486911
cm_boy 2011-12-07
  • 打赏
  • 举报
回复
我碰到了跟你一样的情况,请问,你是怎么解决的?QQ229486911
LuRose 2011-08-31
  • 打赏
  • 举报
回复
导出文件还写这么多代码做什么。
直接用个超链接,指定名称和格式 就自动下载了嘛
a82344626 2011-08-31
  • 打赏
  • 举报
回复
因为你是用迅雷下了
如果想迅雷下不是ASPX页面就先保存在服务器下完之后再删除他!
net5354 2011-08-31
  • 打赏
  • 举报
回复
1、关闭下载软件与浏览器的关联
2、导出到服务器端(网站所在目录)的EXCELL模板中,再下载~!!
YnSky 2011-08-31
  • 打赏
  • 举报
回复
这样导出毕竟不是真正的excel你可以查找导出真正的excel代码!
风2013 2011-08-31
  • 打赏
  • 举报
回复
可能是你机器设置的问题,和你的程序没有关系,
换台电脑试试 可能会好。

我也遇到了这个问题 ,也还没有解决,
我们公司 似乎只有我一个人的电脑导出的时候 是.aspx 文件 其他人的都是 .xls

顶一下吧,我也很想知道怎么解决啊
C5662601 2011-08-31
  • 打赏
  • 举报
回复

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;

namespace webtest
{
public partial class download : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string myname = Request["url"];
string FileName = System.IO.Path.GetFileName(myname);
int Size = Convert.ToInt32(new System.IO.FileInfo(myname).Length);
System.IO.Stream fileStream = new System.IO.FileStream(myname, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);
byte[] Content = new byte[Size];
int Status = fileStream.Read(Content, 0, Size);
fileStream.Close();
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment;filename=" + ToHexString(FileName));
Response.ContentType = System.IO.Path.GetExtension(FileName);
Response.BinaryWrite(Content);
Response.End();
}

/// <summary>
/// 为字符串中的非英文字符编码
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string ToHexString(string s)
{
char[] chars = s.ToCharArray();
StringBuilder builder = new StringBuilder();
for (int index = 0; index < chars.Length; index++)
{
bool needToEncode = NeedToEncode(chars[index]);
if (needToEncode)
{
string encodedString = ToHexString(chars[index]);
builder.Append(encodedString);
}
else
{
builder.Append(chars[index]);
}
}

return builder.ToString();
}

/// <summary>
///指定 一个字符是否应该被编码
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static bool NeedToEncode(char chr)
{
string reservedChars = "$-_.+!*'(),@=&";

if (chr > 127)
return true;
if (char.IsLetterOrDigit(chr) || reservedChars.IndexOf(chr) >= 0)
return false;

return true;
}

/// <summary>
/// 为非英文字符串编码
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static string ToHexString(char chr)
{
UTF8Encoding utf8 = new UTF8Encoding();
byte[] encodedBytes = utf8.GetBytes(chr.ToString());
StringBuilder builder = new StringBuilder();
for (int index = 0; index < encodedBytes.Length; index++)
{
builder.AppendFormat("%{0}", Convert.ToString(encodedBytes[index], 16));
}
return builder.ToString();
}
}
}
子夜__ 2011-08-31
  • 打赏
  • 举报
回复
那就屏蔽迅雷
参考
参考
Response.ContentType = "application/vnd.ms-access";//类型,我这里举的是access数据库类型
Response.AppendHeader("Content-Disposition", "attachment; filename=123.mdb");//文件名,你可以用个变量来存其值。
Response.TransmitFile(Server.MapPath("路径"));
Response.End();
ltcszk 2011-08-31
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 woainikeaibibi 的回复:]

引用 1 楼 liuchaolin 的回复:
HttpContext.Current.Response.ContentType = "application/ms-excel";
改成下边的试试
Response.ContentType = "application/octet-stream";

大哥,试过了,不行,但是在迅雷上他提示下载的是.aspx,还有个选项是使用IE打开,IE……
[/Quote]
业务表格用迅雷下载
难道不怕公司机密泄露吗?
迅雷会自动上传至服务器的啊
szjarvis 2011-08-31
  • 打赏
  • 举报
回复
mark
szjarvis 2011-08-31
  • 打赏
  • 举报
回复
是不是跟下载软件有关系,多用几部电脑试一下。
子夜__ 2011-08-31
  • 打赏
  • 举报
回复
添加Download.aspx页
FileStream f= new FileStream("", FileMode.Open);  
byte[] buffer = new byte[f.Length];
f.Read(buffer, 0, buffer.Length);
f.Close();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("", System.Text.Encoding.UTF8));
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();


参考
七爷 2011-08-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liuchaolin 的回复:]
HttpContext.Current.Response.ContentType = "application/ms-excel";
改成下边的试试
Response.ContentType = "application/octet-stream";
[/Quote]
大哥,试过了,不行,但是在迅雷上他提示下载的是.aspx,还有个选项是使用IE打开,IE开打的就行
LMAOhuaNL 2011-08-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 liuchaolin 的回复:]

HttpContext.Current.Response.ContentType = "application/ms-excel";
改成下边的试试
Response.ContentType = "application/octet-stream";
[/Quote]
up
md5e 2011-08-31
  • 打赏
  • 举报
回复
HttpContext.Current.Response.ContentType = "application/ms-excel";
改成下边的试试
Response.ContentType = "application/octet-stream";

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧