上图是导出的EXCEL截图,导出的EXCEL和设定的EXCEL名字不符合,网页永远导出EXCEL名字为exprotName.xlsx
可是我输出到网页的文件类型为xlsx 而且名字也不是exprotName 很纳闷
后台也经过HttpUtility.UrlEncode(fileName, Encoding.UTF8)) 编码了
你可能需要在后台加入如下代码尝试一下:
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();
導出的程式貼出來看看吧
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");
}