首页 新闻 会员 周边 捐助

c# 导出EXCEL文件名不对

0
悬赏园豆:10 [已解决问题] 解决于 2019-01-17 17:38

上图是导出的EXCEL截图,导出的EXCEL和设定的EXCEL名字不符合,网页永远导出EXCEL名字为exprotName.xlsx
可是我输出到网页的文件类型为xlsx 而且名字也不是exprotName 很纳闷
后台也经过HttpUtility.UrlEncode(fileName, Encoding.UTF8)) 编码了

又是没有bug的一天的主页 又是没有bug的一天 | 初学一级 | 园豆:149
提问于:2018-12-11 13:37
< >
分享
最佳答案
0

你可能需要在后台加入如下代码尝试一下:
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
if (HttpContext.Request.Browser.Browser == "IE")
{
fileName = Url.Encode(fileName);
}
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.AddHeader("Content-Length", fileinfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.TransmitFile(filePath);
Response.End();

收获园豆:10
一个单人旅途 | 初学一级 |园豆:53 | 2018-12-12 11:02
其他回答(2)
0

導出的程式貼出來看看吧

RosonJ | 园豆:4910 (老鸟四级) | 2018-12-11 15:00
0

public ActionResult ExportRechargeRecords(int size = 10, int offset = 0, DateTime? startDate = null, DateTime? endDate = null, string userName = null, int? status = null, int? minAmount = null, int? maxAmount = null)
{
int total = 0;
List<RechargeInfo> list = BLL.AdminAccountManager.GetRecharges(1000000, offset, status, out total, userName, startDate, endDate, minAmount, maxAmount);
System.IO.MemoryStream output = new System.IO.MemoryStream();
System.IO.StreamWriter writer = new System.IO.StreamWriter(output, System.Text.Encoding.UTF8);

        writer.Write("用户名,充值时间,充值金额");
        writer.WriteLine();
        string statusname = string.Empty;
        foreach (var item in list)
        {
            writer.Write(item.UserName + ",");
            writer.Write(item.CreateTime + ",");
            writer.Write(item.RechargeAmount + ",");
                         writer.WriteLine();
        }

        writer.Flush();
        output.Position = 0;
        return File(output, "text/comma-separated-values", "充值记录" + DateTime.Now.ToString("yyyy-MM-dd") + ".csv");
    }
小小熊熊 | 园豆:226 (菜鸟二级) | 2018-12-12 20:04
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册