【问题标题】:ASP.net saving Gridview to xls error thread abortedASP.net 将 Gridview 保存到 xls 错误线程中止
【发布时间】:2016-08-13 14:14:25
【问题描述】:

将我的 Gridview 保存到文件然后下载到浏览器时遇到问题。

我的代码在本地主机上运行,​​即使我得到一个异常抛出:线程被中止。

当我将我的网站发布到我的服务器时,我收到错误消息: 不支持给定路径的格式。

protected void btn_SaveToFile_Click(object sender, EventArgs e) {
    string path = Server.MapPath("~/Sheets/");
    if (!Directory.Exists(path)) {
        Directory.CreateDirectory(path);
    }
    try {
        using (StringWriter sw = new StringWriter()) {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw)) {
                StreamWriter writer = File.AppendText(path + DateTime.Now +".xls");
                GridView1.RenderControl(hw);
                writer.WriteLine(sw.ToString());
                writer.Close();
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "attachment; filename="+DateTime.Now+ ".xls");
                Response.TransmitFile(Server.MapPath("~/Sheets/"+DateTime.Now+".xls"));
                Response.End();
            }
        }
    }
    catch (Exception ex){
        Debug.WriteLine("Error saving to file: " + ex.Message);
    }
}

【问题讨论】:

  • 如果可以直接将 .xls 下载到浏览器,而无需先保存到服务器,那将是首选。
  • 网站用户账户是否有权创建Sheets目录并向其添加文件?
  • 创建了 Sheets 文件夹,但我的 FTP 文件夹中没有文件。但是代码一定有问题,因为我得到 Thread 被中止,即使在 localhost 上也是如此。
  • 不支持给定路径的格式。似乎是我的文件没有创建的问题

标签: c# asp.net excel gridview


【解决方案1】:

有一大堆错误。我不确定它是否会起作用,但请尝试以下方法:

//edited version
protected void btn_SaveToFile_Click(object sender, EventArgs e) {
    //string path = Server.MapPath("~/Sheets/");
    //if (!Directory.Exists(path)) {
    //    Directory.CreateDirectory(path);
    //}
    string fname=DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")+".xls";
    try {
        using (StringWriter sw = new StringWriter()) {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw)) {
                //StreamWriter writer = File.AppendText(path + fname);
                GridView1.RenderControl(hw);
                //writer.WriteLine(sw.ToString());
                //writer.Close();
                Response.Clear();
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "attachment; filename="+fname);
                //Response.TransmitFile(path+fname));
            Response.Write(sw.ToString());
            Response.End();
            }
        }
    }
    catch (Exception ex){
        Debug.WriteLine("Error saving to file: " + ex.Message);
    }
}

同样,如前所述,最好将响应直接发送到写入响应的浏览器。

编辑:这不会产生错误,但下载的文件将仅扩展名为 .xls。更好的方法是每行读取 GrdView 并将所需的任何内容放入 CSV。内容类型将为“text/csv”。我不想重写应用程序。你需要自己做。

【讨论】:

  • 它通过替换 : 和 / 但仍然有一个线程被中止
【解决方案2】:

我使用的虚拟主机有不同的发帖方式DateTime.Now

Visual Studio 将我的文件保存为 dd.mm.yyy hh.mm.ss 但我的虚拟主机显示 m/dd/yy h:mm:ss / 和 : 不支持作为文件名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多