我正在尝试在 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");
}
最佳答案
这称为扩展强化。执行步骤以避免此错误。
- Open your Registry (Start -> Run -> regedit.exe) Navigate to
- HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\OFFICE\12.0\EXCEL\SECURITY
- Right click in the right window and choose New -> DWORD Type
- “ExtensionHardening” as the name (without the quotes)
- 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/