Convert Excel file using Spire.XLS

Introduction

Hi. I hope you all are fine. Today we will see a new product Spire.XLS that helps us to create, manipulate and convert Excel files to other formats and many more. This product has been introduced by the company E-Iceblue. I hope you have read my article of Spire.Doc. If you have not read it, I recommend you read it at Using Spire.Doc.

Please read this article in my blog Using Spire.XLS.

Download source code

Using Spire XLS With Visual Studio

Background

Always, managing Excel files using code is a tough job for me. If you agree then I strongly recommend the product Spire.XLX from E-Iceblue. It made the task easier than ever. This article provides a demo of converting Excel files to other formats. I hope you like it.

Download the files

You can always get the necessary files from Download Spire.XLS.

Install Spire.XLS

I am using evaluation version with one month temporary license. There are free versions also available for spire.xls with some limitation. You can try that. Now click on the exe file after you extract the downloaded file. The installation will then start.


So shall we start?

Once you have installed it, you are ready to go. We will start with a “Simple Windows Forms” application. Before starting, please install Spire.XLs and Visual Studio 2008 or above. I am using Visual Studio 2015 RC. I hope everything is set. 

Open your Visual Studio, click on "File" -> "New" -> "Project..." then select Visual C# (if you are good in C# or select Visual Basic) then in the Project seelct Windows -> Windows Forms Application then name your project (I am naming it "Using SpireXLs").


Now create a group box and a button in your form and name them :). Later, click on the button.


Now right-click on your project and click Add reference. Then in the browse tab determine the folder in which you have installed Spire xls. Usually it will be in the C:\Program Files\e-iceblue\Spire.Xls. Now just find your framework version from the BIN folder and add Spire.xls.DLL.



Now we have added the reference too. So shall we start coding?

Using the code

To start with the coding you need to add the necessary namespaces as follows.

  1. using Spire.Xls;  
  2. using Spire.Pdf;  
  3. using Spire.Xls.Converter;  

In the button click event you need to add the following code.

C# Code 

  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.    // load Excel file  
  4.    Workbook workbook = new Workbook();  
  5.    workbook.LoadFromFile("D:\\MyExcel.xlsx");  
  6.    // Set PDF template  
  7.    PdfDocument pdfDocument = new PdfDocument();  
  8.    pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape;  
  9.    pdfDocument.PageSettings.Width = 970;  
  10.    pdfDocument.PageSettings.Height = 850;  
  11.    //Convert Excel to PDF using the template above  
  12.    PdfConverter pdfConverter = new PdfConverter(workbook);  
  13.    PdfConverterSettings settings = new PdfConverterSettings();  
  14.    settings.TemplateDocument = pdfDocument;  
  15.    pdfDocument = pdfConverter.Convert(settings);  
  16.    // Save and preview PDF  
  17.    pdfDocument.SaveToFile("MyPDF.pdf");  
  18.    System.Diagnostics.Process.Start("MyPDF.pdf");  
  19. }   
VB.NET Code
  1. 'load Excel file  
  2. Dim workbook As New Workbook()  
  3. workbook.LoadFromFile("D:\MyExcel.xlsx")  
  4. ' Set PDF template  
  5. Dim pdfDocument As New PdfDocument()  
  6. pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape  
  7. pdfDocument.PageSettings.Width = 970  
  8. pdfDocument.PageSettings.Height = 850  
  9. 'Convert Excel to PDF using the template above  
  10. Dim pdfConverter As New PdfConverter(workbook)  
  11. Dim settings As New PdfConverterSettings()  
  12. settings.TemplateDocument = pdfDocument  
  13. pdfDocument = pdfConverter.Convert(settings)  
  14. ' Save and preview PDF  
  15. pdfDocument.SaveToFile("MyPdf.pdf")  
  16. System.Diagnostics.Process.Start("MyPdf.pdf")  

In the preceding code, we are loading an Excel file MyExcel.xlsx from my drive. The following is the contents of the Excel file.


Now if you run your project and click our button, you will get a PDF file as follows.


Cool! Very simple, right? Now we will go to other conversions as well.

Excel to HTML

To convert our Excel file to HTML, you need to create a button in our form and paste the following code to the button click event.

C# Code 

  1. private void button2_Click(object sender, EventArgs e)  
  2. {  
  3.    //load Excel file  
  4.    Workbook workbook = new Workbook();  
  5.    workbook.LoadFromFile("D:\\MyExcel.xlsx");  
  6.    //convert Excel to HTML  
  7.    Worksheet sheet = workbook.Worksheets[0];  
  8.    sheet.SaveToHtml("MyHTML.html");  
  9.    //Preview HTML  
  10.    System.Diagnostics.Process.Start("MyHTML.html");  
  11. }   
VB.NET Code
  1. Private Shared Sub Main(args As String())  
  2. 'load Excel file  
  3. Dim workbook As New Workbook()  
  4. workbook.LoadFromFile("D:\\MyExcel.xlsx")  
  5. 'convert Excel to HTML  
  6. Dim sheet As Worksheet = workbook.Worksheets(0)  
  7. sheet.SaveToHtml("MyHTML.html")  
  8. 'Preview HTML  
  9. System.Diagnostics.Process.Start("MyHTML.html")  
  10. End Sub  

Now if you run the code, you can see an HTML file as follows.


Excel To Image

To convert our Excel file to an image, you need to create a button in our form and paste the following code to the button click event.

C# Code

  1. private void button3_Click(object sender, EventArgs e)  
  2. {  
  3.    Workbook workbook = new Workbook();  
  4.    workbook.LoadFromFile("D:\\MyExcel.xlsx");  
  5.    Worksheet sheet = workbook.Worksheets[0];  
  6.    sheet.SaveToImage("MyImage.jpg");  
  7. }  

VB.NET Code 

  1. Shared Sub Main(ByVal args() As String)  
  2. Dim workbook As New Workbook()  
  3. workbook.LoadFromFile("D:\\MyExcel.xlsx")  
  4. Dim sheet As Worksheet = workbook.Worksheets(0)  
  5. sheet.SaveToImage("MyImage.jpg")  
  6. End Sub   
Now if you run the code, you can see an image as follows.



Excel to CSV

To convert our Excel file to an image, you need to create a button in our form and paste the following code to the button click event.

C# Code 

  1. private void button3_Click(object sender, EventArgs e)  
  2. {  
  3.    Workbook workbook = new Workbook();  
  4.    workbook.LoadFromFile("D:\\MyExcel.xlsx");  
  5.    Worksheet sheet = workbook.Worksheets[0];  
  6.    sheet.SaveToFile("MyCSV.csv"",", Encoding.UTF8);  
  7. }   
VB.NET Code
  1. Shared Sub Main(ByVal args() As String)  
  2. Dim workbook As New Workbook()  
  3. workbook.LoadFromFile("D:\\MyExcel.xlsx")  
  4. Dim sheet As Worksheet = workbook.Worksheets(0)  
  5. sheet.SaveToFile("MyCSV.csv"",", Encoding.UTF8)  
  6. End Sub  
Now if you run the code, you can see a CSV file as follows.


Please note that you can convert your Excel data to any other file format, there are plenty of options available. Please try that too. I have given only three options that I use always.

Conclusion

Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.

Your turn. What do you think?

A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.

Up Next
    Ebook Download
    View all
    Learn
    View all