【发布时间】:2009-12-29 10:06:00
【问题描述】:
我将数据表导出为 word,当我传递文件名时,它似乎没有在打开/保存对话框中获取文件名。
这就是我正在做的事情
public static void Convertword(DataTable dt, HttpResponse Response,string filename)
{
try
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".doc");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
System.Web.UI.WebControls.GridView dg = new System.Web.UI.WebControls.GridView();
dg.DataSource = dt;
dg.DataBind();
dg.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
catch(Exception err)
{
throw err;
}
}
当我传递文件名 "report(" + System.DateTime.Now.ToString("dd/MM/yyyy");
+ ")" 时,它不会将值设为 dd/MM/YYYY,而是将文件名显示为 dd_MM_YYYY
【问题讨论】:
-
所以问题是文件保存对话框显示“dd_MM_yyyy”而不是“dd/MM/yyyy”,或者问题是它显示了其他内容(如 mypage.aspx ...)?如果你想发送一个实际的 Word 文件,你应该使用一些库来编写 Word 文件并写入响应输出流(即发送字节)
标签: asp.net filenames export-to-word