Excel to PDF without office

纵然是瞬间 提交于 2019-12-13 14:25:15

问题


I need to generate PDF reports in a windows application I'm working on, and I do this by exporting an .xlsx file to pdf. I'm currently using interop for this, however:

  • I need to do this without requiring the users to buy software, so no microsoft office excel.
  • I also can't use any heavy dependencies (like open/libre office).
  • the application is in .NET winforms and is local (not dependent on an internet connection).

Things I tried:

  • I have tried iTextSharp but this gets really complicated with things like overflowing columns.
  • Tried closedXML but there couldn't convert to PDF.
  • (As mentioned earlier) Tried interop but couldn't find a way to make it independent on office.

Help would be appreciated, thank you in advance :)

edits:

iTextSharp

I would use it except I need to export a big DataTable that has a variable number of columns (up to 30 columns), and if there are many columns then It gets really complicated to handle that column overflow, which was easy in interop.

Aspose

Appears to be too expensive, since I work for a small company that is currently very limited in resources.


回答1:


Instead of using Excel to layout your reports, use HTML. It is much more flexible, and pretty much the easiest tool for laying out data for end users. Then look up one of the many HTML to PDF solutions (wkhtmltopdf, installing a print to pdf driver, etc.)




回答2:


Another good option is to use Spire.XLS. Though it will display evaluation warning at the top but you can get rid of it using FreeSpire.XLS. Below is the link for it https://www.nuget.org/packages/FreeSpire.XLS/

And below is the code snippet taken from

https://forums.asp.net/t/2087645.aspx?Saving+xlsx+to+pdf

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Spire.Xls; 

namespace ConvertExcelToPdf 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 

            Workbook workbook = new Workbook(); 
            workbook.LoadFromFile(@"..\..\sample2.xlsx"); 
            workbook.ConverterSetting.SheetFitToPage = true; 
            workbook.SaveToFile(@"..\..\sample.pdf", FileFormat.PDF); 
            System.Diagnostics.Process.Start(@"..\..\sample.pdf"); 
        } 
    } 
} 



回答3:


An alternative solution for generating PDF from excel using OpenOffice and C#:

Generate PDF using C#



来源:https://stackoverflow.com/questions/15321113/excel-to-pdf-without-office

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!