Maurinostroza
Maurinostroza

Reputation: 115

How can I pass an Excel created with XML to PDF MVC

I need to create a PDF from an Excel created with XML. My application is based on MVC 4.0. Searching I found ITextSharp but I can't find examples of code or something like that.

How can I export to PDF from Excel using ITextSharp?

Upvotes: 0

Views: 3863

Answers (1)

Chris Haas
Chris Haas

Reputation: 55447

First, separate the program from the file format and the API. Excel is a desktop spreadsheet program. XLS and XLSX are binary file formats for storing spreadsheet information. Open XML is a broad subject but usually refers to either the zipped XML file format Microsoft created for recent versions of Office or the .Net APIs for interacting with those files.

If you want to save an Excel document to a PDF file then you need to automate Excel.

If you want to convert an XLS/XLSX file to PDF then you'll need a translation layer and iTextSharp has zero knowledge of the XLS or XLSX file formats. The XLS spec is 1,185 pages, the XLSX spec is 299 pages plus references for generic Open XML concepts and the PDF 1.7 spec (and you should probably get ISO 32000 instead) is 1,310 pages so I'd recommend exploring 3rd part libraries for this instead (and I don't have a recommendation for any either).

If you are programmatically walking structured data in C#, however, I'd recommend just creating your PDF at the same time as your XLSX file. iTextSharp has a very, very nice table API that can auto-format column widths and heights so you don't have to worry about that (unless you want to). You seriously should be able to create a PDF with iTextSharp with the same or probably even less code than your XLSX code. A quick search for iTextSharp table should give you many options. If your structured data is a GridView or something similar you could even use ASP.Net to create HTML for you and have iTextSharp parse that into PDF commands.

Upvotes: 1

Related Questions