XLSX to CSV Conversion Tutorial - C# and VB.NET - ByteScout

XLSX to CSV Conversion Tutorial – C# and VB.NET

  • Home
  • /
  • Articles
  • /
  • XLSX to CSV Conversion Tutorial – C# and VB.NET

XLSX to CSV conversion tutorial shows how to convert XLSX Excel files to CSV in C# or Visual Basic .NET using Bytescout Spreadsheed SDK. Use CSharp or VB.NET source code below to convert XLSX to CSV files.

Program samples demonstrated in this article uses features from the “Bytescout.Spreadsheet” assembly. If you want to code along, then you can get your free SDK trial from this link.

  1. C# Source Code Sample
  2. VB.NET Source Code Sample
  3. Create an Instance of Spreadsheet Class
  4. Load the Input File
  5. Generate and Save Output
  6. Close the Document

C# Source Code Sample

Spreadsheet document = new Spreadsheet())

document.LoadFromFile("SimpleReport.xlsx");
string csvFile = Path.GetTempPath() + "SimpleReport.csv";

// Save the document as CSV file
document.Workbook.Worksheets[0].SaveAsCSV(csvFile);
document.Close();

Following is the VB.NET program for the same.

VB.NET Source Code Sample

   ' Load XLS document
 	Dim document As New Spreadsheet()
   document.LoadFromFile("SimpleReport.xlsx")
   Dim csvFile As String = Path.GetTempPath() & "SimpleReport.csv"

   ' Save the document as CSV file
   document.Workbook.Worksheets(0).SaveAsCSV(csvFile)
   document.Close()

The program output is as follows:

Though the program is quite simple and straightforward, let’s analyze it.

Create an Instance of Spreadsheet Class

Creating an instance of Spreadsheet class which is under the “Bytescout.Spreadsheet” assembly.

Spreadsheet document = new Spreadsheet())

Load the Input File

Next, we are loading the input file to be processed by utilizing the “LoadFromFile” method. Spreadsheet class also provides various versions of the same method as loading password-protected input file. Also, if we have an input file in-stream format, we can utilize the “LoadFromStream” method.

document.LoadFromFile("SimpleReport.xlsx");

Generate and Save Output

The next step is generating output and saving it at the desired location. Here we are considering the first worksheet to parse as a CSV file.

// Save the document as CSV file

document.Workbook.Worksheets[0].SaveAsCSV(csvFile);

Close the Document

And finally, we’re closing the document, which will essentially clean up memory.

document.Close();

 

That’s all guys,

Happy Coding!

Tutorials:

prev
next