asp.net-mvc - 在.Net C# MVC中打开下载的excel文件后文件名发生变化

标签 asp.net-mvc asp.net-mvc-3 excel asp.net-mvc-4 export-to-excel

我正在尝试在 C#.Net MVC 应用程序中将数据导出为 Excel。我在 Actionresult 中使用了 return file() 。文件返回并下载成功。

但是打开文件时出现错误,并且打开文件时文件名发生更改。 下载的文件名为 ExportFilterCRMdoctorRequest.xls,但打开后会更改为 Book1。 导出文件的代码:

public ActionResult ExportFilterCRMdoctorRequest() 
        {
            var stream = new MemoryStream();
            var serializer = new XmlSerializer(typeof(List<CDRFilterCRMDoctorRequest>));

            //We load the data
            List<CDRFilterCRMDoctorRequest> data = (List<CDRFilterCRMDoctorRequest>)Session["filterCRMRequestList"]; //Retriving data from Session

            //We turn it into an XML and save it in the memory
            serializer.Serialize(stream, data);
            stream.Position = 0;

            //We return the XML from the memory as a .xls file
            return File(stream, "application/vnd.ms-excel", "ExportFilterCRMdoctorRequest.xls");
        }

enter image description here

enter image description here

The file name got changed to "Book1"

最佳答案

这称为扩展强化。执行步骤以避免此错误。

  1. Open your Registry (Start -> Run -> regedit.exe) Navigate to
  2. HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\OFFICE\12.0\EXCEL\SECURITY
  3. Right click in the right window and choose New -> DWORD Type
  4. “ExtensionHardening” as the name (without the quotes)
  5. Verify that the data has the value “0″

注意

There is one thing that has to be borne in mind when serializing in XML. XML is not Excel’s standard format and it has to open the file as XML data. This means that when opening the file it will issue a couple of warnings which are more of a nuisance than anything else.

返回您的原始查询:复制您的问题,下面是修复方法

示例类

public class StudentModel
{
    public string Name { get; set; }
    public string Address { get; set; }
    public string Class { get; set; }
    public string Section { get; set; }
}

示例数据

private List<StudentModel> StudentData()
{
    List<StudentModel> objstudentmodel = new List<StudentModel>();
    objstudentmodel.Add(new StudentModel { Name = "Name1", Class = "1", 
                                         Address = "Address1", Section = "A" });
    objstudentmodel.Add(new StudentModel { Name = "Name2", Class = "2", 
                                         Address = "Address2", Section = "A" });
    return objstudentmodel;
}

操作方法

public ActionResult Index()
{
    List<StudentModel> objstudent = new List<StudentModel>();
    objstudent = StudentData();
    StringBuilder sb = new StringBuilder();
    sb.Append("<table border='" + "1px" + "'b>");
    //code section for creating header column
    sb.Append("<tr>");
    sb.Append("<td><b><font size=2>NAME</font></b></td>");
    sb.Append("<td><b><font size=2>CLASS</font></b></td>");
    sb.Append("<td><b><font size=2>ADDRESS</font></b></td>");
    sb.Append("<td><b><font size=2>SECTION</font></b></td>");
    sb.Append("</tr>");

    //code for creating excel data
    foreach (StudentModel item in objstudent)
    {
        sb.Append("<tr>");
        sb.Append("<td><font>" + item.Name.ToString() + "</font></td>");
        sb.Append("<td><font>" + item.Class.ToString() + "</font></td>");
        sb.Append("<td><font>" + item.Address.ToString() + "</font></td>");
        sb.Append("<td><font>" + item.Section.ToString() + "</font></td>");
        sb.Append("</tr>");
    }
    sb.Append("</table>");
    HttpContext.Response.AddHeader("content-disposition", 
                                   "attachment; filename=student_" + 
                                   DateTime.Now.Year.ToString() + ".xls");
    this.Response.ContentType = "application/vnd.ms-excel";

    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
    return File(buffer, "application/vnd.ms-excel");
}

关于asp.net-mvc - 在.Net C# MVC中打开下载的excel文件后文件名发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18566422/

相关文章:

asp.net-mvc - 在 ASP.NET MVC 中查看计数器

c# - 如何将这些 LINQ 结果加载到我的 ViewModel 类中?

c# - Windows 10 IOT Raspberry PI 上的 ASP.NET MVC Web 应用程序

jquery - 在asp.net mvc中使用jQuery datepicker的问题

c# - MVC3 比较属性和嵌套对象属性

VBA Dir 函数不适用于 Excel 2010

arrays - 将值插入数组时 VBA 函数失败

c# - 如何在 Action 中调用和返回另一个 Controller Action 的输出?

asp.net-mvc-3 - 如何将Request.QueryString传递给Url.Action?

java - JXL(Java)更改为1904日期系统