HTTP响应 - Excel文件 - 保存到磁盘

时间:2017-04-10 20:20:43

标签: c# excel httpresponse

我正在使用Excel Helper类来生成excel表,其中数据集中的每个数据表都是excel中的工作表。

我可以使用多个工作表获取excel,但有没有办法直接从代码中将其保存到某个位置。

当我运行代码时直接下载excel,我必须手动保存它。请告知。

  public static void ToExcel(DataSet dsInput, string filename, HttpResponse response)
    {
        var excelXml = GetExcelXml(dsInput, filename);
        response.Clear();
        response.AppendHeader("Content-Type", "application/vnd.ms-excel");
        response.AppendHeader("Content-disposition", "attachment; filename=" + filename);
        response.Write(excelXml);
         response.Flush();
        response.End();
    }


    Using the helper class to genrate Dataset into multiple tabs //  
        try
        {

             ds = obj.Get_Tables();
        }
      ExcelHelper.ToExcel(ds, "AlertDetails.xls", Page.Response);

1 个答案:

答案 0 :(得分:0)

如果设置response.ContentType = "application/octet-stream",浏览器很可能会提供保存文件。浏览器可能会检查文件的开头并确定它是一个Excel文件,并且根据用户选择的操作,无论如何都要打开它。

此外,您应该用双引号括起文件名。

无法让浏览器保存到特定位置 - 这样可能会被滥用。