Available in the Full Version
ASP.NET MVC Word Library - Create Word Table
This sample demonstrates exporting the JQuery grid’s data to a Microsoft Word document.
This sample uses CTP (Community Technical Preview) features. The API and behavior may change when these features are released with full support.
This sample is designed for a larger screen size.
On mobile, try rotating your screen, view full size, or email to another device.
The Infragistics Word library offers a variety of different export formats and styling options for the exported document.
Code View
Copy to Clipboard
@using Infragistics.Web.Mvc @using IgniteUI.SamplesBrowser.Models @model IQueryable<IgniteUI.SamplesBrowser.Models.Northwind.Order> <!DOCTYPE html> <html> <head> <title></title> <!-- Ignite UI for jQuery Required Combined CSS Files --> <link href="http://cdn-na.infragistics.com/igniteui/2024.1/latest/css/themes/infragistics/infragistics.theme.css" rel="stylesheet" /> <link href="http://cdn-na.infragistics.com/igniteui/2024.1/latest/css/structure/infragistics.css" rel="stylesheet" /> <script src="http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.8.3.js"></script> <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script> <script src="http://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script> <!-- Ignite UI for jQuery Required Combined JavaScript Files --> <script src="http://cdn-na.infragistics.com/igniteui/2024.1/latest/js/infragistics.core.js"></script> <script src="http://cdn-na.infragistics.com/igniteui/2024.1/latest/js/infragistics.lob.js"></script> <style type="text/css"> .export-word-sample fieldset { margin: 5px; margin-bottom: 10px; padding: 10px; border: 1px solid #CCCCCC; } .export-word-sample fieldset label { display: block; margin-bottom: 3px; } .export-word-sample fieldset input { margin-bottom: 5px; float: left; clear: left; } .export-word-sample fieldset label { float: left; width: 160px; padding-top: 2px; } .export-word-sample fieldset fieldset { display: inline-block; vertical-align: middle; width: auto; } #grid1_container { margin: 15px auto; } .export-word-sample .button-container { position: relative; display: inline-block; vertical-align: middle; height: auto; } </style> </head> <body> @using(Html.BeginForm()) { <div class="export-word-sample"> <fieldset> <legend>Export Options</legend> <fieldset> <legend>Export Type</legend> <input type="radio" name="exportType" id="currentPage" value="false" checked="checked" /> <label for="currentPage">Current Page</label> <input type="radio" name="exportType" id="allPages" value="true" /> <label for="allPages">All Pages</label> </fieldset> <div class="button-container"> <input id="btnSubmit" type="submit" value="Export Button" /> </div> </fieldset> </div> <script type="text/javascript"> $(function () { var grid = $("#grid1"), pageSize = $("#pageSize"), pageNumber = $("#pageNumber"); pageNumber.val(0); pageSize.val(0); grid.on("iggridrendered", function (e, ui) { pageSize.val(grid.igGridPaging("option", "pageSize")); pageNumber.val(grid.igGridPaging("option", "currentPageIndex")); }); grid.on("iggridpagingpageindexchanged", function (e, ui) { pageNumber.val(ui.pageIndex); }); grid.on("iggridpagingpagesizechanged", function (e, ui) { pageSize.val(ui.pageSize); pageNumber.val(0); }); }); </script> @(Html.Infragistics().Grid(Model).ID("grid1") .PrimaryKey("OrderID") .AutoGenerateColumns(false) .Columns(column => { column.For(x => x.OrderID).HeaderText("Order ID").DataType("number"); column.For(x => x.ContactName).HeaderText("Contact Name"); column.For(x => x.ShipAddress).HeaderText("Shipping Address"); column.For(x => x.OrderDate).HeaderText("Order Date"); }).Features(features => { features.Paging(); }).Height("500").Width("100%").DataSourceUrl(Url.Action("PagingGetData")).DataBind().Render()) <input type="hidden" name="pageSize" id="pageSize" value="" /> <input type="hidden" name="pageNumber" id="pageNumber" value="" /> } </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; #region Word Exporting using System.IO; using System.Drawing; using Infragistics.Web.Mvc; using Infragistics.Documents.Word; using IgniteUI.SamplesBrowser.Models.Northwind; using IgniteUI.SamplesBrowser.Models.Repositories; #endregion namespace IgniteUI.SamplesBrowser.Controllers { public class InfragisticsWordController : Controller { private IEnumerable<Order> orders = null; // Orders data private IEnumerable<Order> Orders { get { try { if (orders == null) orders = RepositoryFactory.GetOrderRepository().Get().Take(100).AsQueryable(); return orders; } catch (Exception) { return new List<Order>().AsQueryable(); } } } // // GET: /InfragisticsWord/ [ActionName("create-word-table")] public ActionResult WordExporter() { return View("create-word-table", Orders); } [GridDataSourceAction] [ActionName("PagingGetData")] public ActionResult PagingGetData() { return View("create-word-table", Orders); } [HttpPost] [ActionName("create-word-table")] public void WordExporter(int pageNumber, int pageSize, bool exportType) { pageNumber++; List<Order> newOrders = Orders.ToList(); bool exportAllPages = exportType; if (exportAllPages) { newOrders = newOrders .Select(c => new Order { OrderID = c.OrderID, ContactName = c.ContactName, ShipAddress = c.ShipAddress, OrderDate = c.OrderDate }) .ToList(); } else { newOrders = newOrders .Skip<Order>(pageSize * (pageNumber - 1)) .Take<Order>(pageSize) .Select(c => new Order { OrderID = c.OrderID, ContactName = c.ContactName, ShipAddress = c.ShipAddress, OrderDate = c.OrderDate }) .ToList(); } WordExportingModel wordExporter = new WordExportingModel(); wordExporter.PopulateWordDocument(newOrders); SendForDownload(wordExporter.WordMemoryStream); } private void SendForDownload(MemoryStream ms) { Response.Clear(); Response.AppendHeader("content-disposition", "attachment; filename=word.doc"); Response.ContentType = "application/octet-stream"; ms.Seek(0, SeekOrigin.Begin); ms.CopyTo(Response.OutputStream); Response.End(); } #region Word Report Model class WordExportingModel { #region Members #region Private Members private WordDocumentWriter wordWriter; private MemoryStream memStream; #endregion #endregion #region Constructor public WordExportingModel() { memStream = new MemoryStream(); wordWriter = WordDocumentWriter.Create(memStream); } public WordExportingModel(MemoryStream ms) { memStream = ms; wordWriter = WordDocumentWriter.Create(memStream); } #endregion #region Properties public MemoryStream WordMemoryStream { get { return this.memStream; } } public WordDocumentWriter ExcelWorkbook { get { return this.wordWriter; } } #endregion #region Public Methods public void PopulateWordDocument(List<Order> data) { wordWriter.StartDocument(); wordWriter.StartParagraph(); wordWriter.AddTextRun("Congratulations! You have successfully exported igGrid data to Word!"); wordWriter.EndParagraph(); // Create the table properties TableProperties tableProps = wordWriter.CreateTableProperties(); tableProps.Alignment = ParagraphAlignment.Center; tableProps.BorderProperties.Color = Color.Black; tableProps.BorderProperties.Style = TableBorderStyle.Single; // Create the table header row properties TableRowProperties headerRowProps = wordWriter.CreateTableRowProperties(); headerRowProps.IsHeaderRow = true; // Create the table header cell properties TableCellProperties headerCellProps = wordWriter.CreateTableCellProperties(); headerCellProps.BackColor = Color.DarkGray; headerCellProps.TextDirection = TableCellTextDirection.LeftToRightTopToBottom; // Start a table wordWriter.StartTable(2, tableProps); // Start a row and apply it the header row properties wordWriter.StartTableRow(headerRowProps); // Create header row AddTableCell(wordWriter, headerCellProps, "Order ID"); AddTableCell(wordWriter, headerCellProps, "Contact Name"); AddTableCell(wordWriter, headerCellProps, "Shipping Address"); AddTableCell(wordWriter, headerCellProps, "Order Date"); // End the header row wordWriter.EndTableRow(); // Create the table's content cell properties TableCellProperties contentCellProps = wordWriter.CreateTableCellProperties(); contentCellProps.BackColor = Color.FromArgb(235, 235, 235); contentCellProps.PreferredWidthAsPercentage = 0.5f; // Iterate through the grid rows to extract the content data int i = 0; foreach (Order order in data) { // Alternating row style if (i % 2 == 1) contentCellProps.BackColor = Color.White; else contentCellProps.BackColor = Color.FromArgb(235, 235, 235); wordWriter.StartTableRow(); AddTableCell(wordWriter, contentCellProps, order.OrderID.ToString()); AddTableCell(wordWriter, contentCellProps, order.ContactName); AddTableCell(wordWriter, contentCellProps, order.ShipAddress); AddTableCell(wordWriter, contentCellProps, order.OrderDate != null ? string.Format("{0:d}", order.OrderDate) : ""); wordWriter.EndTableRow(); i++; } // End the table wordWriter.EndTable(); // End the document wordWriter.EndDocument(); wordWriter.Close(); } #endregion #region Private Methods private void AddTableCell(WordDocumentWriter documentWriter, TableCellProperties cellProperties, string cellText) { // Start a Cell documentWriter.StartTableCell(cellProperties); // Start a Paragraph and add a text run to the cell documentWriter.StartParagraph(); documentWriter.AddTextRun(cellText); documentWriter.EndParagraph(); // End the Cell documentWriter.EndTableCell(); } #endregion } #endregion } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace IgniteUI.SamplesBrowser.Models.Northwind { public class Order { public int OrderID { get; set; } public string CustomerID { get; set; } public Nullable<int> EmployeeID { get; set; } public Nullable<System.DateTime> OrderDate { get; set; } public Nullable<System.DateTime> RequiredDate { get; set; } public Nullable<System.DateTime> ShippedDate { get; set; } public Nullable<int> ShipVia { get; set; } public Nullable<decimal> Freight { get; set; } public string ShipName { get; set; } public string ShipAddress { get; set; } public string ShipCity { get; set; } public string ShipRegion { get; set; } public string ShipPostalCode { get; set; } public string ShipCountry { get; set; } public string ContactName { get; set; } public string EmployeeName { get; set; } public int ShipperID { get; set; } public string ShipperName { get; set; } public decimal TotalPrice { get; set; } public int TotalItems { get; set; } } }