使用文件名导出到 word 似乎不起作用
我将数据表导出到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
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.
Here is what I am doing
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;
}
}
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
+ ")"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
关于您的代码的几点说明:
dd/MM/YYYY
不是有效的文件名,因为/
字符。try/catch
块catch
语句中您只执行throw err
调用Response.End<, /code> 末尾是不必要的。
using
语句,以确保在所有情况下都调用Dispose
方法。Few remarks about your code:
dd/MM/YYYY
is not a valid filename because of the/
character.try/catch
block if in thecatch
statement you are only doingthrow err
Response.End
at the end is not necessary.using
statement when dealing with disposable objects such as streams and readers/writers to ensure that theDispose
method is invoked in all cases.你应该使用像这样的文件名
You should to use a filename like
文件名不能有“/”。
A filename cannot have "/".
这很可能是因为
/
不是文件名的有效字符。您的名字必须符合一定的标准,请确保不要使用任何That's most likely because
/
isn't a valid character for filenames. Your name must fulfill certain criteria, be sure not to use any of如果文件名中有正斜杠,我会认为这会破坏文件的 URL,因此斜杠会在某个时刻被替换?
If you have forward slashes in the filename, I would assume that this would break the URL to the file and hence the slashes are being replaced at some point?