tags:

views:

229

answers:

7

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?

A: 

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

Gregoire
A: 

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!

minus4
+1  A: 

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

Mauricio Scheffer
+1  A: 

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.

cdonner
A: 

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).

erikkallen
A: 
  • 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.
Dmytrii Nagirniak
A: 

Hi, does anyone have a sample of the code using the Entity Framework instead of LINQ?

Jacques