我对 mvc 和剑道库都比较陌生。在我的项目中,我需要一种将数据从 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 来映射数据,然后将其传递给视图中的模型。
不幸的是,我不完全理解示例中的代码,也无法弄清楚如何更改我的代码。
例子:
控制器:
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>
如果我理解正确,Read 函数用于将数据从 Products 数据库传递到网格。DataSource 请求对象是用 javascript 创建的?
如果我没有像 Products 这样的数据库模型,我该如何更改我的代码?
在我的项目中,我使用存储库来获取 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; }
}
控制器:
公共类表控制器:控制器{
CzasPracyMaszynRepository _repositoryCzasPracyMaszyn;
public TabeleController()
{
_repositoryCzasPracyMaszyn = new CzasPracyMaszynRepository();
}
public ActionResult CzasPracyMaszynTabela()
{
IList<CzasPracyMaszyn> CzasPracyMaszyn = _repositoryCzasPracyMaszyn.GetCzasPracyMaszyn();
return View(CzasPracyMaszyn);
}
}
我知道所有这些都非常模糊,但我尽力描述了我的问题。