文件格式和扩展名与excel asp.net mvc不匹配

时间:2016-02-04 13:22:06

标签: c# asp.net xml asp.net-mvc excel

我可以创建excel文件并使用数据库中的源代码下载它。一切都好,直到我打开它。它确切地说明了我的标题。我用Google搜索并在stackoverflow上发现我必须添加

Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=Additions.xls");
Response.ContentType = "application/ms-excel";

所以我在代码中添加了它们但结果是一样的。我做错了什么?

这是我的整个控制器

        [Download]
        [Autorization(RoleTypes.AdminAndViewer)]
        public ActionResult DownloadAdditivesHistoryReport(DateTime from, DateTime to, int groupId)
        {
            try
            {
                //correct search datetime
                from = CorrectFormat.FDateTime.GetFromDate(from);
                to = CorrectFormat.FDateTime.GetToDate(to);


                float? rate = Settings.Core.Web.UI.Reports.Rate;
                float? netAmount = Settings.Core.Web.UI.Reports.NetAmount;

                List<AdditionReport_Result> AdditionReports = Uow.AdditionReport(to, groupId, rate, netAmount);

                //total results
                int totalResults = AdditionReports.Count();



                //fill model
                AdditivesHistoryItemModel[] model = AdditionReports
                        .Select(x => new AdditivesHistoryItemModel()
                        {
                            //Id = x.additionUserId,
                            GroupName = x.Branch,
                            RegistratorDisplayName = x.RegistratorUserName,
                            RegistratorName = x.Registrator,
                            Registrations = x.Registrations,
                            Fee = x.Fee,
                            NetAmount = x.NetAmount
                        }).ToArray();

                DataSet ds = AdditionHistoryForm.GetData(model);
                XPathDocument input = AdditionHistoryForm.GetDocument(ds);

                if (System.IO.File.Exists(Server.MapPath("~/Core/Files/Additions.xls")))
                    System.IO.File.Delete(Server.MapPath("~/Core/Files/Additions.xls"));

                using (FileStream output = new FileStream(Server.MapPath("~/Core/Files/Additions.xls"), FileMode.CreateNew))
                {
                    Response.ClearContent();
                    Response.AddHeader("content-disposition", "attachment; filename=Additions.xls");
                    Response.ContentType = "application/ms-excel";

                    XslCompiledTransform xslt = new XslCompiledTransform();
                    xslt.Load(Server.MapPath("~/Core/Files/AdditionHistory.xslt"));
                    xslt.Transform(input, null, output);


                }


                //create xlsx file name
                string fileName = String.Format("{0}-{1}", Guid.NewGuid(), CurrentUser.Id);



                return File(System.IO.File.ReadAllBytes((Server.MapPath("~/Core/Files/Additions.xls"))), "application/vnd.ms-excel", String.Format("{0}.xml", fileName));               
            }
            catch (Exception ex)
            {
                //log error
                EventLogger.Logger.WriteEcxeption(ex);

                return null;
            }
        }

0 个答案:

没有答案