Convert World/Excel to PDF

Asked

Viewed 1,566 times

7

Does anyone know of an open source api for converting world and excel documents to pdf? I am currently using windows Interop however it only works if I install the office on the server. I need to remove this installation. On my system I save the File Extension which can be (doc, docx, xls, xlsx) and the bytes. And when the user wants to view the file I show the document on the screen to it. This is all a web system. It is currently working, however using windows Interop, I need to find another open source alternative that does not depend on installed office. Does anyone know? I tried openxml more by I saw it does not Convert to pdf only handles files.

  • It doesn’t necessarily have to be in c# the conversation, it can be another language, in this case I make an introduction between the language.

  • I hope my answer has solved your doubts, anything I’m available.

  • Api free doesn’t exist yet, has paid api that does exactly what you want to do: https://www.convertapi.com/docx-to-pdf

3 answers

3

There are several Frameworks for this solution, what I most used was the Eeplus (which is only one DLL and does not need to be installed), but there are many others like:

As I said what I most used and had a better experience was the Eeplus.

See how easy it is to use it :

Create Tab

private static ExcelWorksheet CreateSheet(ExcelPackage p, string sheetName)
{
    p.Workbook.Worksheets.Add(sheetName);
    ExcelWorksheet ws = p.Workbook.Worksheets[1];
    ws.Name = sheetName; //Setting Sheet's name
    ws.Cells.Style.Font.Size = 11; //Default font size for whole sheet
    ws.Cells.Style.Font.Name = "Calibri"; //Default Font name for whole sheet

    return ws;
}

Join columns

//Merging cells and create a center heading for out table
ws.Cells[1, 1].Value = "Sample DataTable Export"; // Heading Name
ws.Cells[1, 1, 1, dt.Columns.Count].Merge = true; //Merge columns start and end range
ws.Cells[1, 1, 1, dt.Columns.Count].Style.Font.Bold = true; //Font should be bold
ws.Cells[1, 1, 1, dt.Columns.Count].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; // Aligmnet is center

Add Stilo to the cell

//Setting the background color of header cells to Gray
var fill = cell.Style.Fill;
fill.PatternType = ExcelFillStyle.Solid;
fill.BackgroundColor.SetColor(Color.Gray);

Add edge style

//Setting Top/left,right/bottom borders.
var border = cell.Style.Border;
border.Bottom.Style = border.Top.Style = border.Left.Style = border.Right.Style = ExcelBorderStyle.Thin;

Add formula

//Setting Sum Formula
cell.Formula = "Sum(" + ws.Cells[3, colIndex].Address + ":" + ws.Cells[rowIndex - 1, colIndex].Address + ")";

Image

private static void AddImage(ExcelWorksheet ws, int columnIndex, int rowIndex, string filePath)
{
    //How to Add a Image using EP Plus
    Bitmap image = new Bitmap(filePath);
    ExcelPicture picture = null;
    if (image != null)
    {
        picture = ws.Drawings.AddPicture("pic" + rowIndex.ToString() + columnIndex.ToString(), image);
        picture.From.Column = columnIndex;
        picture.From.Row = rowIndex;
        picture.From.ColumnOff = Pixel2MTU(2); //Two pixel space for better alignment
        picture.From.RowOff = Pixel2MTU(2);//Two pixel space for better alignment
        picture.SetSize(100, 100);
    }
}

Convert to PDF

Workbook workbook = new Workbook();  
//Load excel file  
workbook.LoadFromFile(info.Name);  
//Save excel file to pdf file.  
workbook.SaveToFile("result.pdf", Spire.Xls.FileFormat.PDF); 

Links of tutorials :

http://www.c-sharpcorner.com/UploadFile/48c038/create-excel-and-convert-it-to-pdf-by-free-api/

http://zeeshanumardotnet.blogspot.com.br/2011/06/creating-reports-in-excel-2007-using.html

http://www.codeproject.com/Articles/680421/Create-Read-Edit-Advance-Excel-Report-in

http://www.jimmycollins.org/blog/? p=547

  • Your answer has nothing to do with the guy’s question, he just wants to convert word for pdf and not generate file.

0

I use the library Eeplus. The library Epplus can also be downloaded by Nuget.

The following is an example of a code that generates a file Excel from a Datatable:

Private Sub CreateAndFormatExcel(ByVal dt As Data.DataTable)
    Dim fileName As String = "exemplo.xlsx"

    Using pck As New OfficeOpenXml.ExcelPackage()
        'Excel Properties
        pck.Workbook.Properties.Author = "Nome do Autor"
        pck.Workbook.Properties.Title = "Titulo do Excel"
        pck.Workbook.Properties.Company = "Nome da Empresa"
        pck.Workbook.Properties.Subject = "Subject"
        pck.Workbook.Properties.Comments = "Comentarios"
        pck.Workbook.Properties.Manager = "Manager"
        pck.Workbook.Properties.HyperlinkBase = New Uri("http://google.com")

        'Create the worksheet
        Dim ws As OfficeOpenXml.ExcelWorksheet = pck.Workbook.Worksheets.Add(Resources.Standard.ListaFaltas)

        'Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
        ws.Cells("A1").LoadFromDataTable(dt, True)

        Dim rangeCell As String = "A1:F1"

        Using rng As OfficeOpenXml.ExcelRange = ws.Cells(rangeCell)
            rng.Style.Font.Bold = True
            rng.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid
            'Set Pattern for the background to Solid
            rng.Style.Fill.BackgroundColor.SetColor(Drawing.Color.Red)
            'Set color to dark blue
            rng.Style.Font.Color.SetColor(Drawing.Color.White)
        End Using

        ws.Cells(rangeCell).AutoFilter = True

        'Congela painel
        ws.View.FreezePanes(2, 2)

        'Write it back to the client
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        Response.AddHeader("content-disposition", "attachment;  filename=" & fileName & "")
        Response.BinaryWrite(pck.GetAsByteArray())
        Response.End()
    End Using

End Sub
  • need convert to pdf and do not generate file, know if it is possible?

  • can you tell me ?

0

Try using Epplus and Spire.xls. I didn’t use it, but I searched for no need to install Office on the server.

  • Spire.xls works the problem it poses a message stating that it was generated by this tool.

  • Epplus can convert to PDF?

  • Yes, it is possible to see my answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.