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.
using Spire.Xls;
using Spire.Pdf;
using Spire.Xls.Converter;
In the button click event you need to add the following code.
C# Code
private void button1_Click(object sender, EventArgs e)
{
// load Excel file
Workbook workbook = new Workbook();
workbook.LoadFromFile("D:\\MyExcel.xlsx");
// Set PDF template
PdfDocument pdfDocument = new PdfDocument();
pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape;
pdfDocument.PageSettings.Width = 970;
pdfDocument.PageSettings.Height = 850;
//Convert Excel to PDF using the template above
PdfConverter pdfConverter = new PdfConverter(workbook);
PdfConverterSettings settings = new PdfConverterSettings();
settings.TemplateDocument = pdfDocument;
pdfDocument = pdfConverter.Convert(settings);
// Save and preview PDF
pdfDocument.SaveToFile("MyPDF.pdf");
System.Diagnostics.Process.Start("MyPDF.pdf");
}
VB.NET Code
'load Excel file
Dim workbook As New Workbook()
workbook.LoadFromFile("D:\MyExcel.xlsx")
' Set PDF template
Dim pdfDocument As New PdfDocument()
pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape
pdfDocument.PageSettings.Width = 970
pdfDocument.PageSettings.Height = 850
'Convert Excel to PDF using the template above
Dim pdfConverter As New PdfConverter(workbook)
Dim settings As New PdfConverterSettings()
settings.TemplateDocument = pdfDocument
pdfDocument = pdfConverter.Convert(settings)
' Save and preview PDF
pdfDocument.SaveToFile("MyPdf.pdf")
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
private void button2_Click(object sender, EventArgs e)
{
//load Excel file
Workbook workbook = new Workbook();
workbook.LoadFromFile("D:\\MyExcel.xlsx");
//convert Excel to HTML
Worksheet sheet = workbook.Worksheets[0];
sheet.SaveToHtml("MyHTML.html");
//Preview HTML
System.Diagnostics.Process.Start("MyHTML.html");
}
VB.NET Code
Private Shared Sub Main(args As String())
'load Excel file
Dim workbook As New Workbook()
workbook.LoadFromFile("D:\\MyExcel.xlsx")
'convert Excel to HTML
Dim sheet As Worksheet = workbook.Worksheets(0)
sheet.SaveToHtml("MyHTML.html")
'Preview HTML
System.Diagnostics.Process.Start("MyHTML.html")
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
private void button3_Click(object sender, EventArgs e)
{
Workbook workbook = new Workbook();
workbook.LoadFromFile("D:\\MyExcel.xlsx");
Worksheet sheet = workbook.Worksheets[0];
sheet.SaveToImage("MyImage.jpg");
}
VB.NET Code
Shared Sub Main(ByVal args() As String)
Dim workbook As New Workbook()
workbook.LoadFromFile("D:\\MyExcel.xlsx")
Dim sheet As Worksheet = workbook.Worksheets(0)
sheet.SaveToImage("MyImage.jpg")
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
private void button3_Click(object sender, EventArgs e)
{
Workbook workbook = new Workbook();
workbook.LoadFromFile("D:\\MyExcel.xlsx");
Worksheet sheet = workbook.Worksheets[0];
sheet.SaveToFile("MyCSV.csv", ",", Encoding.UTF8);
}
VB.NET Code
Shared Sub Main(ByVal args() As String)
Dim workbook As New Workbook()
workbook.LoadFromFile("D:\\MyExcel.xlsx")
Dim sheet As Worksheet = workbook.Worksheets(0)
sheet.SaveToFile("MyCSV.csv", ",", Encoding.UTF8)
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.