我对mvc和kendo库都比较新。在我的项目中,我需要一种方法将数据从Kendo Grid(http://demos.kendoui.com/web/grid/index.html)导出为PDF。
我实际上找到了一个很好的例子,但是在其中实体框架用于将数据传递给网格。以下是示例:http://www.kendoui.com/code-library/mvc/grid/export-grid-to-pdf.aspx。
在我的项目中,我使用NHibernate来映射数据,然后将其传递给View中的模型。
不幸的是,我不完全理解示例中的代码,但我无法弄清楚如何更改代码。
示例:
控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.UI;
using KendoMVCWrappers.Models;
using Kendo.Mvc.Extensions;
using System.Collections;
using System.IO;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace KendoMVCWrappers.Controllers
{
public class HomeController : Controller
{
private NwEntities db = new NwEntities();
public ActionResult Index()
{
ViewBag.Message = "Modify this template to kick-start your ASP.NET MVC application.";
return View();
}
public ActionResult Read([DataSourceRequest] DataSourceRequest request)
{
// DataSourceRequest i = request;
return Json(db.Products.ToDataSourceResult(request));
}
public FileResult Export([DataSourceRequest]DataSourceRequest request)
{
IEnumerable products = db.Products.ToDataSourceResult(request).Data;
// step 1: creation of a document-object
var document = new Document(PageSize.A4, 10, 10, 10, 10);
//step 2: we create a memory stream that listens to the document
var output = new MemoryStream();
PdfWriter.GetInstance(document, output);
//step 3: we open the document
document.Open();
//step 4: we add content to the document
var numOfColumns = 4;
var dataTable = new PdfPTable(numOfColumns);
dataTable.DefaultCell.Padding = 3;
dataTable.DefaultCell.BorderWidth = 2;
dataTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
// Adding headers
dataTable.AddCell("Product ID");
dataTable.AddCell("Product Name");
dataTable.AddCell("Unit Price");
dataTable.AddCell("Quantity Per Unit");
dataTable.HeaderRows = 1;
dataTable.DefaultCell.BorderWidth = 1;
foreach (Product prod in products)
{
dataTable.AddCell(prod.ProductID.ToString());
dataTable.AddCell(prod.ProductName);
dataTable.AddCell(prod.UnitPrice.ToString());
dataTable.AddCell(prod.QuantityPerUnit);
}
// Add table to the document
document.Add(dataTable);
//This is important don't forget to close the document
document.Close();
// send the memory stream as File
return File(output.ToArray(), "application/pdf", "Products.pdf");
}
}
}
查看:
@{
ViewBag.Title = "Home Page";
}
@model IEnumerable<KendoMVCWrappers.Models.Product>
@(
Html.Kendo().Grid(Model).Name("Grid")
.DataSource(ds => ds.Ajax()
.Model(m =>
{
m.Id(p=>p.ProductID);
})
.Read(r => r.Action("Read", "Home"))
)
.ToolBar(toolBar =>
toolBar.Custom()
.Text("Export To PDF")
.HtmlAttributes(new { id = "export" })
.Url(Url.Action("Export", "Home", new { page = 1, pageSize = "~", filter = "~", sort = "~" }))
)
.Columns(columns =>
{
columns.Bound(p => p.ProductID);
columns.Bound(p => p.ProductName);
columns.Bound(p => p.UnitPrice).Format("{0:c}");
columns.Bound(p => p.QuantityPerUnit);
})
.Events(ev => ev.DataBound("onDataBound"))
.Pageable(paging => paging
.Input(false)
.Numeric(true)
.Info(true)
.PreviousNext(true)
.PageSizes(new int[]{6, 12, 100 })
.Refresh(true)
)
.Sortable()
.Scrollable(scr=>scr.Height(330))
.Filterable()
.Resizable(resize => resize.Columns(true))
)
<script type="text/javascript">
function onDataBound(e) {
var grid = $('#Grid').data('kendoGrid');
// ask the parameterMap to create the request object for you
var requestObject = (new kendo.data.transports["aspnetmvc-server"]({ prefix: "" }))
.options.parameterMap({
page: grid.dataSource.page(),
sort: grid.dataSource.sort(),
filter: grid.dataSource.filter()
});
// Get the export link as jQuery object
var $exportLink = $('#export');
// Get its 'href' attribute - the URL where it would navigate to
var href = $exportLink.attr('href');
// Update the 'page' parameter with the grid's current page
href = href.replace(/page=([^&]*)/, 'page=' + requestObject.page || '~');
// Update the 'sort' parameter with the grid's current sort descriptor
href = href.replace(/sort=([^&]*)/, 'sort=' + requestObject.sort || '~');
// Update the 'pageSize' parameter with the grid's current pageSize
href = href.replace(/pageSize=([^&]*)/, 'pageSize=' + grid.dataSource._pageSize);
//update filter descriptor with the filters applied
href = href.replace(/filter=([^&]*)/, 'filter=' + (requestObject.filter || '~'));
// Update the 'href' attribute
$exportLink.attr('href', href);
}
</script>
如果我理解正确读取功能用于将数据从Products数据库传递到网格。使用javascript创建DataSource请求对象?
如果我没有像Products这样的数据库模型,我怎么能改变我的代码?
在我的项目中,我使用Repository来获取IList所需的所有数据。如何使用上面的示例执行类似的操作:
IEnumerable products = db.Products.ToDataSourceResult(request).Data;
在我的代码下面?
回购:
公共类CzasPracyMaszynRepository {
public IList<CzasPracyMaszyn> GetCzasPracyMaszyn()
{
using (ISession session = NHibernateHelper.OpenSession())
{
var myList = session.Query<CzasPracyMaszyn>();
return myList.ToList();
}
}
}
型号:
public class CzasPracyMaszyn
{
public virtual int ID { get; set; }
public virtual string STANOWISKO { get; set; }
public virtual int CZASPRACY { get; set; }
public virtual int CZASAWARII { get; set; }
public virtual int AWARIAFORMY { get; set; }
public virtual int AWARIAROBOTA { get; set; }
public virtual int AWARIAWTRYSKARKI { get; set; }
public virtual int MOKRYSUROWIEC { get; set; }
public virtual int PUSTE { get; set; }
}
控制器:
public class TabeleController:Controller {
CzasPracyMaszynRepository _repositoryCzasPracyMaszyn;
public TabeleController()
{
_repositoryCzasPracyMaszyn = new CzasPracyMaszynRepository();
}
public ActionResult CzasPracyMaszynTabela()
{
IList<CzasPracyMaszyn> CzasPracyMaszyn = _repositoryCzasPracyMaszyn.GetCzasPracyMaszyn();
return View(CzasPracyMaszyn);
}
}
我知道所有这些都很模糊,但我尽力描述我的问题。