Skip to main content Skip to footer

How to Import and Export CSV Files Using WinForms C# and VB.NET

  • 0 Comments
Quick Start Guide
Tutorial Concept

Learn how to create C#/VB.NET WinForms apps with seamless CSV importing, exporting, and XLSX conversion directly from the app's UI.

What You Will Need
Controls Referenced

Spread.NET - .NET Spreadsheet Component

Documentation | Demo Explorer

This blog explores how to integrate robust CSV (Comma Separate Values) file handling capabilities directly into .NET WinForms applications using a .NET spreadsheet component. In this example, we will be using Spread.NET. You’ll learn how to programmatically handle CSV importing, exporting, and converting to XLSX format using C# or VB.NET. Additionally, we’ll explore some ready-to-use ribbon toolbar options, giving users an intuitive interface for these file-handling tasks.

Download a 30-Day Trial of this Powerful .NET Spreadsheet Component!

Import, Export, and Convert CSV to XLSX in .NET WinForms Apps using C# or VB.NET:

Download a sample WinForms app to follow along with the blog.

Configure a .NET WinForms Spreadsheet Application

In Visual Studio 2022, create a new C# or VB WinForms project named SpreadWinFormsCSVIO. In this sample, we will use .NET 8.

Create a .NET 8 WinForms App in Visual Studio 2022

Add a MenuStrip from the Toolbox window under the area for Menus & Toolbars. Create menu items for File – Open, File – Save CSV, File – Save XLSX, File – Design, and File – Exit.

Add a Menu Strip to a .NET WinForms App

Drag a SplitContainer control from the Toolbox. Set its Orientation to Horizontal in the Properties window. Configure Panel1MinSize and SplitterDistance to 23. This will create a layout for the formula bar (top pane) and spreadsheet (bottom pane).

Configure WinForms App for .NET Spreadsheet Component

Drag another SplitContainer into the top pane (Panel1) of SplitContainer1. Set Panel1MinSize and SplitterDistance to 150. This splits the formula bar area for Spread.NET’s namebox and a formula text box.

Split containers in .NET WinForms app

Next, on the Solution Explorer, expand the Solution SpreadWinFormsCSVIO and Project SpreadWinFormsCSVIO; then, right-click Dependencies and select Manage NuGet Packages…

Manage NuGet Packages option in Visual Studio 2022

Search for and install the GrapeCity.Spread.WinForms NuGet package.

Install the .NET WinForms Spreadsheet Component, Spread.NET

Drag an FpSpread control into the bottom pane (Panel2) of SplitContainer1 and set its Dock property to Fill.

Add a .NET Spreadsheet to a WinForms Application

Drag a NameBox control into SplitContainer2.Panel1 (upper-left). Set Dock to Fill. Use the AttachTo property in the NameBox Tasks to bind it to FpSpread1.

Add a spreadsheet name box to a .NET WinForms app

Drag a FormulaTextBox control into the SplitContainer2.Panel2 (upper-right) and set its BorderStyle to FixedSingle and Dock to Fill; then, attach it to FpSpread1 using the AttachTo property in the FormulaTextBox Tasks.

Add Formula Textbox

Run the application and notice that the NameBox and FormulaTextBar display the associated .NET spreadsheet data from the selected cell.

Run .NET WinForms Spreadsheet Application – including a spreadsheet name box and formula bar


Import a CSV File into a .NET Spreadsheet using C# or VB.NET

To handle importing CSV files into a .NET app, implement the SheetView class’s LoadTextFile method. In this example, we invoke this method within the File - Open CSV menu strip options event handler code. Double-click on the file menu options in design time to generate the menu handle.

C#:

 private string mFileName = null;
 private void openCSVToolStripMenuItem_Click(object sender, EventArgs e)
 {
     OpenFileDialog ofd = new OpenFileDialog();
     ofd.Filter = "CSV file (*.csv)|*.csv| All Files (*.*)|*.*";
     ofd.FilterIndex = 0;
     if (ofd.ShowDialog() == DialogResult.OK)
     {
         mFileName = ofd.FileName;
         fpSpread1.Sheets[0].LoadTextFile(mFileName, FarPoint.Win.Spread.TextFileFlags.None, FarPoint.Win.Spread.Model.IncludeHeaders.None, "", ",", "");
     }
 }

VB:

Private mFileName As String = Nothing
Private Sub openCSVToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles openCSVToolStripMenuItem.Click
            Dim ofd As New OpenFileDialog()
            ofd.Filter = "CSV file (*.csv)|*.csv| All Files (*.*)|*.*"
            ofd.FilterIndex = 0
            If ofd.ShowDialog() = DialogResult.OK Then
                mFileName = ofd.FileName
                fpSpread1.Sheets(0).LoadTextFile(mFileName, FarPoint.Win.Spread.TextFileFlags.None, FarPoint.Win.Spread.Model.IncludeHeaders.None, "", ",", "")
            End If
End Sub

Import CSV Files to .NET WinForms Apps

Check out the Opening a Custom Text File documentation for additional information.


Convert a CSV File to an Excel XLSX File using C# or VB.NET

The imported CSV file can now be converted to an Excel XLSX file by saving the current .NET spreadsheet instance using the SaveExcel methods of the FpSpread class. To save data to Excel Workbook format, use the UseOOXMLFormat option. We will invoke this method within the File - Save XLSX menu strip options event handler code.

C#:

 private void saveXLSXToolStripMenuItem_Click(object sender, EventArgs e)
 {
     fpSpread1.SaveExcel("WorksheetToExcel.xlsx", FarPoint.Excel.ExcelSaveFlags.UseOOXMLFormat);
 }

VB:

Private Sub saveXLSXToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles saveXLSXToolStripMenuItem.Click
    fpSpread1.SaveExcel("WorksheetToExcel.xlsx", FarPoint.Excel.ExcelSaveFlags.UseOOXMLFormat)
End Sub

Convert CSV files to XLSX using C# or VB.NET in WinForms Apps

Check out the Saving to an Excel File documentation for additional information.


Export a .NET Spreadsheet Instance to a CSV File

Developers can easily save the .NET spreadsheet instance as a CSV file using the SaveTextFile method. For this sample, we will invoke the method within the File - Save CSV menu strip options event handler code.

C#:

 private void saveCSVToolStripMenuItem_Click(object sender, EventArgs e)
 {
     fpSpread1.Sheets[0].SaveTextFile("WorksheetToCSV.csv", FarPoint.Win.Spread.TextFileFlags.None, FarPoint.Win.Spread.Model.IncludeHeaders.None, "", ",", "");
 }

VB:

Private Sub saveCSVToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles saveCSVToolStripMenuItem.Click
    fpSpread1.Sheets(0).SaveTextFile("WorksheetToCSV.csv", FarPoint.Win.Spread.TextFileFlags.None, FarPoint.Win.Spread.Model.IncludeHeaders.None, "", ",", "")
End Sub

Export CSV files from WinForms Apps

Check out the Saving a Text File documentation for additional information.


Handle CSV Files with a UI Spreadsheet Ribbon Toolbar in a WinForms App

Spread.NET enables developers to easily pop out a Spreadsheet Designer during runtime or add a familiar Ribbon Toolbar Control to the WinForms application. Both the Spreadsheet Designer and the Ribbon Toolbar Control offer a File menu that enables general file operations, such as opening or saving a file, and printing.

In this example, we will invoke the Spread Designer tool during runtime using the ShowDialog method. Before we can add the code for this, we must drag and drop the FpSpreadDesigner component in the form. 

.NET Spreadsheet Designer Component in VS Toolbox

Then, within the File - Design menu strip options event handler code, invoke the ShowDialog method.

C#:

private void designToolStripMenuItem_Click(object sender, EventArgs e)
{
    fpSpreadDesigner1.ShowDialog(fpSpread1);
}

VB:

Private Sub designToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles designToolStripMenuItem.Click
    fpSpreadDesigner1.ShowDialog(fpSpread1)
End Sub

With this code, when running the application and selecting the File - Design option, the Spread Designer will pop up, allowing the user to open a CSV file, modify it, and then save the modified file to another file outside of the application or apply the changes back to the spreadsheet instance.

Use .NET Spreadsheet Designer to Import, Modify, and Save CSV Files in WinForms Apps

See the Using Spread Designer in Runtime documentation for additional information.

Download a 30-Day Trial of this Powerful .NET Spreadsheet Component!


.NET Spreadsheet Components

This article doesn’t even scratch the surface of the full capabilities of Spread.NET, the award-winning .NET spreadsheet component. Download a free 30-day trial, review the documentation to see some of the many available features, and download the demo explorer to see the features in action and interact with the sample code. Integrating a spreadsheet component into your applications allows you to customize your users' experience and provide them with familiar spreadsheet functionality without referring them to an external program. To learn more about Spread.NET and the latest features, check out our releases section.

Tags: