MVCGrid.Net
MVCGrid.Net copied to clipboard
PDF and Excel Export support
Hi,
How I can implement a PDF,Excel export engine for MVCGrid?
Best Regards
Hi, i have solution but its only "html Excel". I create my own GridRenderingEngine like http://mvcgrid.net/demo/customexport
` using System; using System.IO; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using MVCGrid.Interfaces; using MVCGrid.Models;
namespace ZdravotniVykonyMVC { public class ExcelRenderingEngine : IMVCGridRenderingEngine { public void PrepareResponse(HttpResponse response) { response.Clear(); response.ClearHeaders(); response.ClearContent(); response.AddHeader("content-disposition", "attachment; filename=Export" + DateTime.Now.ToFileTime() + ".xls"); response.AddHeader("Content-Type", "application/Excel"); response.ContentType = "application/vnd.xls"; response.BufferOutput = false; }
public bool AllowsPaging => false;
public void RenderContainer(ContainerRenderingModel model, TextWriter outputStream)
{
}
public void Render(RenderingModel model, GridContext gridContext, TextWriter outputStream)
{
var htmlWriter = new HtmlTextWriter(outputStream);
Table table = new Table();
table.GridLines = GridLines.Both;
//head
TableRow hlavickaRow = new TableRow();
foreach (var col in model.Columns)
{
TableCell bunka = new TableCell();
bunka.Text = col.HeaderText;
hlavickaRow.Cells.Add(bunka);
}
table.Rows.Add(hlavickaRow);
//rows
foreach (var item in model.Rows)
{
TableRow radek = new TableRow();
foreach (var col in model.Columns)
{
TableCell bunka = new TableCell();
bunka.Text = item.Cells[col.Name].PlainText.StripTagsCharArray();
if (bunka.Text.Contains("..."))
bunka.Text = bunka.Text.After("...");
radek.Cells.Add(bunka);
}
table.Rows.Add(radek);
}
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
table.RenderControl(hw);
htmlWriter.Write(sb.ToString());
}
}
}`
Thanks for response Joe.But I am not looking for export option. I want to know if I can check anywhere in controller if Grid already exists and if yes can I call Retrieve methods
On Jul 20, 2016 2:57 AM, "JardaMaly" [email protected] wrote:
Hi, i have solution but its only "html Excel". I create my own GridRenderingEngine like http://mvcgrid.net/demo/customexport http://url
` using System; using System.IO; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using MVCGrid.Interfaces; using MVCGrid.Models;
namespace ZdravotniVykonyMVC { public class ExcelRenderingEngine : IMVCGridRenderingEngine { public void PrepareResponse(HttpResponse response) { response.Clear(); response.ClearHeaders(); response.ClearContent(); response.AddHeader("content-disposition", "attachment; filename=Export" + DateTime.Now.ToFileTime() + ".xls"); response.AddHeader("Content-Type", "application/Excel"); response.ContentType = "application/vnd.xls"; response.BufferOutput = false; }
public bool AllowsPaging => false; public void RenderContainer(ContainerRenderingModel model, TextWriter outputStream) { } public void Render(RenderingModel model, GridContext gridContext, TextWriter outputStream) { var htmlWriter = new HtmlTextWriter(outputStream); Table table = new Table(); table.GridLines = GridLines.Both; //head TableRow hlavickaRow = new TableRow(); foreach (var col in model.Columns) { TableCell bunka = new TableCell(); bunka.Text = col.HeaderText; hlavickaRow.Cells.Add(bunka); } table.Rows.Add(hlavickaRow); //rows foreach (var item in model.Rows) { TableRow radek = new TableRow(); foreach (var col in model.Columns) { TableCell bunka = new TableCell(); bunka.Text = item.Cells[col.Name].PlainText.StripTagsCharArray(); if (bunka.Text.Contains("...")) bunka.Text = bunka.Text.After("..."); radek.Cells.Add(bunka); } table.Rows.Add(radek); } StringBuilder sb = new StringBuilder(); StringWriter tw = new StringWriter(sb); HtmlTextWriter hw = new HtmlTextWriter(tw); table.RenderControl(hw); htmlWriter.Write(sb.ToString()); }
}
}`
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/joeharrison714/MVCGrid.Net/issues/60#issuecomment-233873358, or mute the thread https://github.com/notifications/unsubscribe-auth/ATBD4_HPgJOTUYc6kT5tyv59hrHpcW9nks5qXdT3gaJpZM4JN_Ny .
I tried the same code piece but its not working, i need to export to xslx format