Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    85
    Rep Power
    0

    VB.NET : Convert .xls to .pdf


    Hi,

    I want to convert excel file to pdf in my system using vb.net and i'm felt disappointed because I couldn't found any solution or guide for this problem. Most of the solution given is I need to use third party installer.

    So, i had tried this code that is used to convert .txt to pdf. However, the result is messy.

    Code:
    Imports System.Data
    Imports com.lowagie.text
    Imports com.lowagie.text.pdf
    Imports System.IO
    Imports com.lowagie.text.Paragraph
    Imports com.lowagie.text.Image
    Imports com.lowagie.text.Document
    Imports com.lowagie.text.PageSize
    Imports com.lowagie.text.pdf.PdfWriter
    Imports java.io
    
    Partial Class pdf
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
    
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Try 'try statement 
    
            Dim document As New Document()
            PdfWriter.getInstance(document, New FileStream("C:\MyTemplate.pdf", FileMode.Create))
            document.open()
    
    
            ' Dim oFile As System.IO.File
            Dim oRead As System.IO.StreamReader
            oRead = IO.File.OpenText("C:\MyTemplate.xls")
    
            While (oRead.Peek <> -1)
    
                document.add(New Paragraph(oRead.ReadLine))
    
            End While
    
            document.close()
            document = Nothing
    
            'Catch ex As Exception
    
            'End Try
            '***********************************************************************************
    
        End Sub
    End Class
    I really hope that anyone could help me.

    Thanks in advance.
  2. #2
  3. I <3 ASCII
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Posts
    2,400
    Rep Power
    1233
    yes you will pretty much need to use a third party installer. You could potentially do it using open office, but with .NET you need something third party, or you need to develop it from scratch using the PDF xml.

    -MBirchmeier
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0

    with java


    it is possible with Java:

    import officetools.OfficeFile; // available at dancrintea.ro/doc-to-pdf/
    ...
    FileInputStream fis = new FileInputStream(new File("test.doc")); // works with xls also
    FileOutputStream fos = new FileOutputStream(new File("test.pdf"));
    OfficeFile f = new OfficeFile(fis,"localhost","8100", true);
    f.convert(fos,"pdf");

    All possible conversions:
    doc --> pdf, html, txt, rtf
    xls --> pdf, html, csv
    ppt --> pdf, swf
    html --> pdf
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Convert .xls to .pdf


    Hi Zett,

    I am quiet new to .net. i have used the code provided by you in one of my project, but the same is giving me some error. I am using Microsoft Visual studio 2008 Express addition. Can please guide me the exact steps in Visual studio to make this code working. If you possible give me a example.


    Thanks for your help in advance.
  8. #5
  9. aCc-csU™
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Pavia, Philippines
    Posts
    259
    Rep Power
    54
    If you're developing from scratch, you can try iTextSharp http://itextsharp.sourceforge.net/

    another quick thought is get our xls data and place them in crystal reports, then export as pdf...
    Software Development,
    IsleNet Consultants

    www.wyldkard.net
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Smile


    Hi wyldKard,

    Thanks a lot dear, i really appreciate you for this response but as i have been working on Visual Basic 6.0 for a long time, Visual Basic 2008 is quiet new for me. Is it possible for you to provide me with a sample code for Visual basic 2008 where i can select a excel file and convert it into pdf format.

    Thanks for your help in advance.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Convert .xls to .pdf in VB.Net


    Hi!

    There is a special .Net library to convert .xls to .pdf in VB.Net and C#, it names Excel to PDF .Net.

    You asked about sample for VS2008/VB, for example, if you want to convert file to file:

    Code:
    Dim xtp As New SautinSoft.XlsToPdf
    Dim result As Integer = xtp.ConvertFile(xlsFile, pdfFile)
    If result = 0 Then
        System.Console.WriteLine("Converted successfully!")
        System.Diagnostics.Process.Start(pdfFile)
    Else
        System.Console.WriteLine("Converting Error!")
    End If
    This is code to convert Excel workbook to PDF in memory:

    Code:
    Dim xtp As New SautinSoft.XlsToPdf
    Dim xlsBytes() As Byte = Nothing
    Dim pdfBytes() As Byte = Nothing
    ReadFromFile(xlsFile, xlsBytes)
    Dim result As Integer = xtp.ConvertBytes(xlsBytes, pdfBytes)
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2011
    Posts
    1
    Rep Power
    0
    Hi
    Try to use UseOffice. Net library.

    UseOffice .Net is a robust .Net component to convert between DOC, DOCX, XLS, RTF, PPT, PDF and HTML formats with tables, images, fonts, colors etc.
    This component is just a class library, it requires Microsoft Office installed.

    //Convert DOC file to PDF in .Net
    SautinSoft.UseOffice u = new SautinSoft.UseOffice();
    u.ConvertFile(@"d:\Word.doc", @"d:\Result.pdf", SautinSoft.UseOffice.eDirection.DOC_to_PDF);
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0
    Hello,

    I meet same problem, I use Spire.XLS to convert xls to pdf, aslo, xlsx support yet.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0
    I have used the code provided by you in one of my project, but the same is giving me some error. I am using Microsoft Visual studio 2008 Express addition.
  20. #11
  21. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Mar 2005
    Location
    Tír na nÓg
    Posts
    1,200
    Rep Power
    178
    This is one of the most peculair posts I've ever seen.
    You want to convert xls to pdf in VB.Net yet you post a java code snippet?!
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2011
    Location
    New Zealand
    Posts
    1
    Rep Power
    0
    Hello,

    Code:
    Private Function Convert(ByVal sourcePath As String, ByVal targetPath As String, ByVal targetType As XlFixedFormatType) As Boolean
    	Dim result As Boolean
    	Dim missing As Object = Type.Missing
    	Dim application As ApplicationClass = Nothing
    	Dim workBook As Workbook = Nothing
    	Try
    		application = New ApplicationClass()
    		Dim target As Object = targetPath
    		Dim type As Object = targetType
    		workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
    		workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, True, False, missing, missing, missing, missing)
    		result = True
    	Catch
    		result = False
    	Finally
    		If workBook IsNot Nothing Then
    			workBook.Close(True, missing, missing)
    			workBook = Nothing
    		End If
    		If application IsNot Nothing Then
    			application.Quit()
    			application = Nothing
    		End If
    		GC.Collect()
    		GC.WaitForPendingFinalizers()
    		GC.Collect()
    		GC.WaitForPendingFinalizers()
    	End Try
    	Return result
    
    End Function
    The above code is from an article in C-sharpcorner.
    Please click here:http://www.c-sharpcorner.com/uploadfile/d2dcfc/methods-for-converting-office-documents-to-pdf-or-xps-with-C-Sharp/ to get details.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    8
    Rep Power
    0

    Try this code in C# and VB.NET


    I have found the code to convert xls or xlsx file to pdf in c# and vb.net from Aspose.Cells for .Net technical section and it works fine.

    [C#]


    Code:
    using System;
    using System.IO;
    usingSystem.Windows.Forms;
    usingAspose.Cells;
    
    namespace AsposeExcel2PdfConverter
    {
        /// <summary>
        /// AsposeExcel2PdfConverter
        /// Use Aspose.Cells to perform the task
        /// </summary>
    class Class1
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
    static void Main(string[] args)
            {
    try
                {
                    // Uncomment the code below when you have purchased
                    // license for Aspose.Cells. You need to
                    // deploy the license in the same folder as your
                    // executable, alternatively you can add the license
                    // file as an embedded resource to your project.
                    // // Set license for Aspose.Cells
                    // Aspose.Cells.LicensecellsLicense = new
                    // Aspose.Cells.License();
                    // cellsLicense.SetLicense("Aspose.Cells.lic");
    
                    //Get the path of the Application folder.
    string path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Application.ExecutablePath), "..\\..");
                    //Get the template excel file path.
    stringdesignerFile = path + @"\Files\PurchasingReport.xls";
                    //Specify the pdf file path.
    stringpdfFile = path + @"\Files\PurchasingReport.pdf";
                    //Create a new Workbook.
                    //Open the template excel file which you have to
    Aspose.Cells.Workbookwb = new Aspose.Cells.Workbook(designerFile);
                    //Save the pdf file.
    wb.Save(pdfFile, SaveFormat.Pdf);
    
                }
    catch (Exception e)
                {
    Console.WriteLine(e.Message);
    Console.ReadLine();
    
                }
            }
        }
    }
    [VB]


    Code:
    Imports System
    Imports System.IO
    Imports System.Windows.Forms
    Imports Aspose.Cells
    
    'AsposeExcel2PdfConverter
    'Use Aspose.Cells to perform the task
    Module Module1
    
        Sub Main()
    
            Try
    
    ' Uncomment the code below when you have purchased license
    ' forAspose.Cells. You need to deploy the
    ' license in the same folder as your executable, alternatively
    ' you can add the license file as an embedded resource to your
    ' project.
    ' ' Set license for Aspose.Cells
    ' DimcellsLicense As Aspose.Cells.License = New
    ' Aspose.Cells.License()
    ' cellsLicense.SetLicense("Aspose.Cells.lic")
    
                'Get the path of the Application folder.
                Dim path As String = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Application.ExecutablePath), "..\")
                'Get the template excel file path.
                Dim designerFile As String = path + "\Files\PurchasingReport.xls"
                'Specify the pdf file path.
                Dim pdfFile As String = path + "\Files\PurchasingReport.pdf"
                'Create a new Workbook.
                'Open the template excel file which you have to convert
                Dim wb As Aspose.Cells.Workbook = New Aspose.Cells.Workbook(designerFile)
                'Save the pdf file.
    wb.Save(pdfFile, SaveFormat.Pdf)
    
    
            Catch e As Exception
    Console.WriteLine(e.Message)
    Console.ReadLine()
    
            End Try
    
        End Sub
    
    End Module
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    USA
    Posts
    1
    Rep Power
    0
    Use Online Software for Convert .xls to .pdf
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0
    Hey im also looking to convert but this wont work can someone help?
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo