使用文件名导出到 word 似乎不起作用

发布于 2024-08-16 10:07:36 字数 1120 浏览 3 评论 0原文

我将数据表导出到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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怕倦 2024-08-23 10:07:36

关于您的代码的几点说明:

  1. 您正在将内容类型标头设置为 Word 文档,但实际上您是通过渲染 GridView 来发送 HTML 内容
  2. dd/MM/YYYY 不是有效的文件名,因为 / 字符。
  3. 则不需要 try/catch
  4. 如果在 catch 语句中您只执行 throw err调用 Response.End<, /code> 末尾是不必要的。
  5. 在处理一次性对象(例如流和读取器/写入器)时,始终使用 using 语句,以确保在所有情况下都调用 Dispose 方法。

Few remarks about your code:

  1. You are setting the content type header to a word document but you are actually sending HTML contents by rendering a GridView
  2. dd/MM/YYYY is not a valid filename because of the / character.
  3. You don't need a try/catch block if in the catch statement you are only doing throw err
  4. Calling Response.End at the end is not necessary.
  5. Always use using statement when dealing with disposable objects such as streams and readers/writers to ensure that the Dispose method is invoked in all cases.
草莓味的萝莉 2024-08-23 10:07:36

你应该使用像这样的文件名

String.Format("report{0:ddMMyyyy}.doc", DateTime.Now);

You should to use a filename like

String.Format("report{0:ddMMyyyy}.doc", DateTime.Now);
故事和酒 2024-08-23 10:07:36

文件名不能有“/”。

A filename cannot have "/".

眉黛浅 2024-08-23 10:07:36

这很可能是因为 / 不是文件名的有效字符。您的名字必须符合一定的标准,请确保不要使用任何

<代码>*。 " / \ [ ] : ; | = ,

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

* . " / \ [ ] : ; | = ,

那请放手 2024-08-23 10:07:36

如果文件名中有正斜杠,我会认为这会破坏文件的 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文