简体   繁体   English

从App_Data下载现有的xls文件,文件未下载

[英]Download existing xls file from App_Data, File doesnt get downloaded

I want to create an excel file and load it into the App_Data folder to hold it in the server, fill it with data from the tables "Tours" and make it a downloadable file. 我想创建一个excel文件并将其加载到App_Data文件夹中,以将其保存在服务器中,并用“ Tours”表中的数据填充它,并使其成为可下载的文件。 When I click the button in the view I wand to download this file from the App_Data folder to download. 当我单击视图中的按钮时,我要从App_Data文件夹下载此文件进行下载。

public FileResult ExcelExport(string startTimeDD, string startTimeMM, string startTimeYYYY, string endTimeDD, string endTimeMM, string endTimeYYYY)
{
    DateTime startingTime = new DateTime(Convert.ToInt32(startTimeYYYY), Convert.ToInt32(startTimeMM), Convert.ToInt32(startTimeDD));
    DateTime endingTime = new DateTime(Convert.ToIn`enter code here`t32(endTimeYYYY), Convert.ToInt32(endTimeMM), Convert.ToInt32(endTimeDD));

    var exportDetails = (from a in db.Rides
                         join veh in db.Vehicles
                         on a.VehicleID equals veh.VehicleID
                         join tou in db.Tours
                         on a.TourID equals tou.TourID
                         where a.StartTime > startingTime && a.EndTime < endingTime
                         join emp in db.Employees
                         on a.EmployeeID equals emp.EmployeeID
                         select new { TourID = tou.TourID, VehicleCosts = veh.Cost, EmployeeCosts = emp.Cost }).ToList();

    string fileName = String.Format("{0:yyyy-MM-dd}.xls", DateTime.Now);
    string path = System.Web.HttpContext.Current.Server.MapPath("/App_Data/");


    var fullPath = Path.Combine(path, fileName);


    using (var sWriter = new StreamWriter(System.IO.File.Create(fullPath)))
    {
        var csvHelper = new CsvHelper.CsvWriter(sWriter);
        foreach( var p in exportDetails)
        {
            csvHelper.WriteField(p.TourID);
            csvHelper.WriteField(p.EmployeeCosts);
            csvHelper.WriteField(p.VehicleCosts);
            csvHelper.NextRecord();
        }

        csvHelper.Dispose();
    }

    string[] filePaths = Directory.GetFiles(System.Web.HttpContext.Current.Server.MapPath("/App_Data/"));

    foreach(string s in filePaths)
    {
        if(s.Contains(fileName))
        {
            byte[] fileBytes = System.IO.File.ReadAllBytes(s);
            return File(fileBytes, "application/ms-excel");

        }

    }


    return null;
}

That is the part of the view: 这是视图的一部分:

@using (Ajax.BeginForm("ExcelExport", "Controlling", new AjaxOptions
{
    HttpMethod = "Get",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "Test"
}))
Just in case someone has the same problem:

public ActionResult ExcelExport(string startTimeDD, string startTimeMM, string startTimeYYYY, string endTimeDD, string endTimeMM, string endTimeYYYY)
        {
            DateTime startingTime = new DateTime(Convert.ToInt32(startTimeYYYY), Convert.ToInt32(startTimeMM), Convert.ToInt32(startTimeDD));
            DateTime endingTime = new DateTime(Convert.ToInt32(endTimeYYYY), Convert.ToInt32(endTimeMM), Convert.ToInt32(endTimeDD));

            var exportDetails = (from a in db.Rides
                                 join veh in db.Vehicles
                                 on a.VehicleID equals veh.VehicleID
                                 join tou in db.Tours
                                 on a.TourID equals tou.TourID
                                 where a.StartTime > startingTime && a.EndTime < endingTime
                                 join emp in db.Employees
                                 on a.EmployeeID equals emp.EmployeeID
                                 select new { TourID = tou.TourID, VehicleCosts = veh.Cost, EmployeeCosts = emp.Cost }).ToList();

            string fileName = String.Format("{0:yyyy-MM-dd}.xlsx", DateTime.Now);
            string path = System.Web.HttpContext.Current.Server.MapPath("/TempFiles/");


            var fullPath = Path.Combine(path, fileName);


            using (var sWriter = new StreamWriter(System.IO.File.Create(fullPath)))
            {
                var csvHelper = new CsvHelper.CsvWriter(sWriter);
                foreach (var p in exportDetails)
                {
                    csvHelper.WriteField(p.TourID);
                    csvHelper.WriteField(p.EmployeeCosts);
                    csvHelper.WriteField(p.VehicleCosts);
                    csvHelper.NextRecord();
                }

                csvHelper.Dispose();
            }

            Download(fileName, fullPath);

            return Content("n");
        }
        public ActionResult Download(string fileName, string fullPath)
        {
            System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
            response.ClearContent();
            response.Clear();
            response.ContentType = "text/plain";
            response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ";");
            response.TransmitFile(fullPath);
            response.Flush();
            response.End();

            return Content("Generated");
        }

and in the view: 

         @using (Html.BeginForm("ExcelExport", "Controlling", new AjaxOptions
                {
                    HttpMethod = "Post",
                    UpdateTargetId = "Test"
                }))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM