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
Few remarks about your code:
dd/MM/YYYY
is not a valid filename because of the /
character. try/catch
block if in the catch
statement you are only doing throw 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 the Dispose
method is invoked in all cases. 您应该使用类似的文件名
String.Format("report{0:ddMMyyyy}.doc", DateTime.Now);
文件名不能包含“ /”。
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,因此在某些时候会替换斜杠?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.