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 |
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:
- Configure a .NET WinForms Spreadsheet Application
- Import a CSV File into a .NET Spreadsheet
- Convert a CSV File to an Excel XLSX File using C# or VB.NET
- Export a .NET Spreadsheet Instance to a CSV File
- Handle CSV Files with a UI Spreadsheet Ribbon Toolbar
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.
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.
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).
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.
Next, on the Solution Explorer, expand the Solution SpreadWinFormsCSVIO and Project SpreadWinFormsCSVIO; then, right-click Dependencies and select Manage NuGet Packages…
Search for and install the GrapeCity.Spread.WinForms NuGet package.
Drag an FpSpread control into the bottom pane (Panel2) of SplitContainer1 and set its Dock property to Fill.
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.
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.
Run the application and notice that the NameBox and FormulaTextBar display the associated .NET spreadsheet data from the selected cell.
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#:
VB:
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#:
VB:
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#:
VB:
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.
Then, within the File - Design menu strip options event handler code, invoke the ShowDialog method.
C#:
VB:
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.
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.