joeharrison714 / MVCGrid.Net

http://mvcgrid.net
MIT License
74 stars 55 forks source link

PDF and Excel Export support #60

Open projectsphonegapdev1 opened 8 years ago

projectsphonegapdev1 commented 8 years ago

Hi,

How I can implement a PDF,Excel export engine for MVCGrid?

Best Regards

JardaMaly commented 8 years ago

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());
    }
}

}`

shefaliA commented 8 years ago

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" notifications@github.com 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 .

tapan8 commented 6 years ago

I tried the same code piece but its not working, i need to export to xslx format