【发布时间】:2014-01-22 07:13:22
【问题描述】:
我是在 asp.net 中使用 MVC 3 和 linq to SQL 的新手,我正在尝试将我的 webgrid 数据导出到 excel 工作表,但它会引发错误:
'System.Web.Helpers.WebGrid' does not contain a definition for 'RenderControl' and no extension method 'RenderControl' accepting a first argument of type 'System.Web.Helpers.WebGrid' could be found (are you missing a using directive or an assembly reference?)
我正在发布代码,所以你们可以帮助我。 代码:
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Helpers;
using EmployeeAttendance_app.Models;
using System.IO;
using System.Web.UI.WebControls;
using System.Web.Mvc.Html;
namespace EmployeeAttendance_app.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Precise Technology Consultants";
var DataContext = new EmployeeAtdDataContext();
//var EmployeeAtd = DataContext.GetAttendance_Sp();
IEnumerable<GetAtdRecord_SpResult> EmployeeAtd = DataContext.GetAtdRecord_Sp(null).ToList();
return View(EmployeeAtd);
}
public ActionResult About()
{
return View();
}
public ActionResult ViewData()
{
return View();
}
public ActionResult ToExcel()
{
var DataContext = new EmployeeAtdDataContext();
IEnumerable<GetAtdRecord_SpResult> EmployeeAtd = DataContext.GetAtdRecord_Sp(null).ToList();
var grid = new WebGrid(EmployeeAtd, defaultSort: "EmplID");
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=AttendanceSheet.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
grid.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return RedirectToAction("Index");
}
}
}
这里是应该显示数据的视图,然后单击导出应该导出文件 查看:
@using EmployeeAttendance_app.Models
<div>
@{
var grid = new WebGrid(ViewData.Model, defaultSort: "EmplID");
}
@grid.GetHtml()
@using (Html.BeginForm("ToExcel","Home",FormMethod.Post))
{
<button type="submit" >To Excel</button>
}
</div>
【问题讨论】:
标签: asp.net asp.net-mvc c#-4.0 linq-to-sql export-to-excel