导出到Word中使用文件名似乎不工作 [英] Export to word with a filename doesn't seem to work
本文介绍了导出到Word中使用文件名似乎不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的DataTable导出到Word中,当我传递一个文件名,它似乎并没有在开放获取文件名/保存对话框。
I export a datatable to word, when I pass a file name it doesn't seem to get the file name in Open/Save dialog box.
下面是我在做什么。
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;
}
}
当我通过文件名报告(+ System.DateTime.Now.ToString(DD / MM / YYYY);
它没有考虑的价值为 DD / MM / YYYY 相反,它显示的文件名的 dd_MM_YYYY
+)
When I pass filename "report(" + System.DateTime.Now.ToString("dd/MM/yyyy");
+ ")"
it doesn't take the value as dd/MM/YYYY instead it shows file name as dd_MM_YYYY
推荐答案
你的code几句话:
- 您正在设置内容类型头一个word文档,但你被渲染的GridView实际发送的HTML内容
-
DD / MM / YYYY
不是因为/
字符的一个有效的文件名。 - 您不需要
的try / catch
块,如果在抓
语句,你只是做抛ERR
- 电话
到Response.End
末是没有必要的。 - 总是使用
可支配的对象,如流和读/写器打交道时使用
语句,以确保的Dispose
方法被调用于所有情况。
- You are setting the content type header to a word document but you are actually sending HTML contents by rendering a GridView
dd/MM/YYYY
is not a valid filename because of the/
character.- You don't need a
try/catch
block if in thecatch
statement you are only doingthrow err
- Calling
Response.End
at the end is not necessary. - Always use
using
statement when dealing with disposable objects such as streams and readers/writers to ensure that theDispose
method is invoked in all cases.
这篇关于导出到Word中使用文件名似乎不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文