Let’s say you have an Excel file and you want to convert it to different formats like JSON/PDF/CSV etc. One way is to get data out of it and manually perform saving data to a particular format, you need to make sure you are syntactically correct and follow all format related specifications. Another way to use some third-party libraries for this task.
In this article, we’ll be using Bytescout Spreadsheet SDK for the same task. Bytescout Spreadsheet SDK provides method endpoints that takes a heavy load off, and makes conversion smooth; even reduces it to one line of code. Cool.
We’ll be reviewing three programs in this article.
Please note, as I am using ByteScout Spreadsheet SDK for these programs, if you want to perform these programs in your machine you need to install ByteScout SDKs in your machine. You can get the installation package from this link.
In the first two programs, I’ll be using C# console applications, with added reference to ByteScout Spreadsheet library as shown in the following image.
In this program, we’ll observe how to convert Excel documents to JSON. We’ll also see how easy it is to convert to PDF. Here’s the program to convert the XLS document to JSON.
static void Main(string[] args) { using (Spreadsheet spreadsheet = new Spreadsheet()) { // Load document spreadsheet.LoadFromFile("Table.xls"); // Export first worksheet to JSON format string jsonString = spreadsheet.ExportToJSON(0); // Write JSON string to file File.WriteAllText("exported.json.txt", jsonString); // Open the result file in default associated application Process.Start("exported.json.txt"); } }
And the output will be as follows.
Let’s analyze this program.
using (Spreadsheet spreadsheet = new Spreadsheet())
{
// Load document
spreadsheet.LoadFromFile("Table.xls");
...
}
// Export first worksheet to JSON format
string jsonString = spreadsheet.ExportToJSON(0);
// Write JSON string to file
File.WriteAllText("exported.json.txt", jsonString);
Now if we were to convert the same XLS file to PDF then we can do it with just a small change as shown by the following code snippet. We have used the method “SaveAsPDF” with two arguments. One to specify the output file name and another to specify whether we want to enable auto-size PDF.
// Save as PDF
bool autosize = false;
spreadsheet.SaveAsPDF("Output.pdf", autosize);
In this program, we’ll see how we can convert XLS documents to different documents like CSV, TXT, XLSX, and XML. The program is as follows where we are converting to CSV.
static void Main(string[] args) { // Load document Spreadsheet document = new Spreadsheet(); document.LoadFromFile("SimpleReport.xls"); // Specify worksheet to be exported as csv document.Workbook.Worksheets[0].SaveAsCSV("SimpleReport.csv"); // Clean up document.Close(); // open output document in default viewer Process.Start("SimpleReport.csv"); }
And the output is as follows.
Though the program is very small and easy to understand – let’s analyze it.
// Load document
Spreadsheet document = new Spreadsheet();
document.LoadFromFile("SimpleReport.xls");
// Specify worksheet to be exported as csv
document.Workbook.Worksheets[0].SaveAsCSV("SimpleReport.csv");
In case we needed to convert Spreadsheet to a Text file, we can achieve it by just different methods “SaveAsTXT” and passing file name which is “SimpleReport.txt” in this case. We can also get output in-memory stream format by passing a stream object as an input.
// save into TXT
document.Workbook.Worksheets[0].SaveAsTXT("SimpleReport.txt");
For saving Spreadsheet to XML document we have to use the “SaveAsXML” method and pass file name which is “SimpleReport.xml” in this case. We can also save the output to a memory stream by passing a memory stream object as an argument.
// save as xml
document.Workbook.Worksheets[0].SaveAsXML("SimpleReport.xml");
In order to save the XLS format spreadsheet to XLSX type, we can use the SaveAs method just like the following code snippet.
// Save to xlsx document
document.SaveAs("Output.xlsx");
In the above program we have already seen how to convert excel to PDF with C#, but for a change let’s see how to do that in a different language. In the following example, we’ll be using one of the legacy languages VBScript to perform the same task.
The program is as follows.
Set document = CreateObject("Bytescout.Spreadsheet.Spreadsheet") document.RegistrationName = "demo" document.RegistrationKey = "demo" ' Add new worksheet Set worksheet = document.Workbook.Worksheets.Add("HelloWorld") ' Add a cell Set cell = worksheet.Cell(1, 1) cell.Value = "Testing, Testing, Testing, Testing..." ' Save spreadsheet as PDF file: ' Save PDF with automatic page size... document.SaveAsPDF "autosized.pdf" ' ... or save PDF with fixed page size document.SaveAsPDF_2 "fixedsize.pdf", False ' the second parameter disables autosizing ' close Spreadsheet Set document = Nothing
The output is as follows.
Let’s analyze the code.
Set document = CreateObject("Bytescout.Spreadsheet.Spreadsheet")
document.RegistrationName = "demo"
document.RegistrationKey = "demo"
' Add new worksheet
Set worksheet = document.Workbook.Worksheets.Add("HelloWorld")
' Add a cell
Set cell = worksheet.Cell(1, 1)
cell.Value = "Testing, Testing, Testing, Testing..."
' Save spreadsheet as PDF file:
' Save PDF with automatic page size...
document.SaveAsPDF "autosized.pdf"
' ... or save PDF with fixed page size
document.SaveAsPDF_2 "fixedsize.pdf", False ' the second parameter disables auto-sizing
That’s all guys.
Happy Coding 🙂