Tony Borf
Tony Borf

Reputation: 4660

What is the best way to generate a PDF and Excel file from ASP.Net MVC

I need to generate a PDF and an Excel file from my ASP.net MVC application. Any ideas on the best way to implement this?

Upvotes: 2

Views: 3443

Answers (6)

Dmytrii Nagirniak
Dmytrii Nagirniak

Reputation: 24078

  • PDF - use NFOP, PdfSharp or iSharpText
  • Excel - if you need plain data as a spreedshet, then render as plain-text CSV file. If you need formatting and all the whistles, then use GBSpreadsheet.

Upvotes: 0

erikkallen
erikkallen

Reputation: 34391

For PDF, you can use PdfSharp. For Excel, I think Aspose.Cells will be good (although I've never used it, I have used their Word component and it rocks).

Upvotes: 0

cdonner
cdonner

Reputation: 37648

I recommend writing a reporting services report (RDLC), deploying it with the web application, and using the report viewer control to render the output. Bringing reporting services in may seem like a steep learning curve, but it is not too bad. You get the added benefit of a solid solution that supports other formats. You don't need a report server for this deployment scenario - not even SQL Server.

Upvotes: 2

Mauricio Scheffer
Mauricio Scheffer

Reputation: 99720

Use iTextSharp to create the PDF and return it with a FileContentResult.

Upvotes: 2

davethecoder
davethecoder

Reputation: 3932

something like abcpdf is cool for creating the PDF, as for excel you just need to create a datatable from your dataset

       var grid = new System.Web.UI.WebControls.DataGrid();
        grid.HeaderStyle.Font.Bold = true;
        grid.DataSource = yourdatahere;
        grid.DataMember = yourdatahere.Stats.TableName;

        grid.DataBind();

        // render the DataGrid control to a file

        using (var sw = new StreamWriter("c:\\test.xls"))
        {
            using (var hw = new HtmlTextWriter(sw))
            {
                grid.RenderControl(hw);
            }
        }

as for doing it with a pdf, that would depend on what route you take!

Upvotes: 0

Gregoire
Gregoire

Reputation: 24832

Create a PDFActionResult and an ExcelActionResult

EDIT

Example for an excelactionresult: http://stephenwalther.com/blog/archive/2008/06/16/asp-net-mvc-tip-2-create-a-custom-action-result-that-returns-microsoft-excel-documents.aspx

Upvotes: 1

Related Questions