将会话导出为Excel文件

时间:2019-05-31 15:40:26

标签: c# asp.net asp.net-mvc entity-framework model-view-controller

我正在尝试将会话导出为Excel文件。我搜索数据并将其存储为会话,然后将其导出为ex​​cel文件

。我的错误“参数字典包含方法'System.Web.Mvc.ActionResult索引(System.String,System.DateTime,System.DateTime,System的非空类型'System.DateTime'的参数'dateFrom'的空条目“ .String)”(位于“ HRG.Controllers.KIRDataController”中)。可选参数必须是引用类型,可为空的类型,或者必须声明为可选参数。 参数名称:parameters“

public class KIRDataController : Controller
    {

        // GET: KIRData
        public ActionResult Index(string accountNo, DateTime dateFrom, DateTime dateTo, string productType)
        {
            DataModel db = new DataModel();
            var kIRDates = from m in db.KIRDates
                          select m;

            if (!String.IsNullOrEmpty(accountNo.ToString()) && !String.IsNullOrEmpty(dateFrom.ToString()) && !String.IsNullOrEmpty(dateTo.ToString()) && !String.IsNullOrEmpty(productType))
                   {
                kIRDates = kIRDates.Where(n => n.Verbund.ToString().Contains(accountNo)).Where(n => n.HinDat.ToString().Contains(dateFrom.ToString())).Where(n => n.RueckDat.ToString().Contains(dateTo.ToString())).Where(n => n.Sparte.Contains(productType));
                   }

            Session["kIRDates"] = kIRDates.ToList<KIRDate>();

            return View(kIRDates);

        }

        public ActionResult ExportToExcel()
        {
            var kIRDates = (List<KIRDate>)Session["kIRDates"];

            GridView gv = new GridView();
            gv.DataSource = kIRDates;
            gv.DataBind();

            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("content-disposition", "attachment;filename=Report.xls");
            Response.Charset = "";

            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            gv.RenderControl(htw);

            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();

            return RedirectToAction("Index");
        }
    }
}

0 个答案:

没有答案