Quick Start Guide | |
---|---|
What You Will Need |
Visual Studio (This example uses VS2022) .NET 8 NuGet Packages: |
Controls Referenced |
Spread.NET - .NET/C# Spreadsheet Component Document Solutions for Excel, .NET Edition - .NET/C# Excel API Library |
Tutorial Concept | .NET C# Excel Import and Export - Learn how to import and export XLSX files from .NET applications from both the client-side and the server-side. |
Importing and exporting Excel files is a crucial functionality for many .NET applications, enabling seamless data manipulation and sharing. In this tutorial, you'll learn how to handle XLSX files in .NET C# from both the desktop/client side in a WinForms application and alternatively programmatically import, modify, and save Excel files on the server.
This Blog Discusses C# Excel Import and Export Options for .NET Applications.
Desktop Client-Side C# Excel XLSX Import & Export
- Create a .NET 8 WinForms App
- Add the .NET C# Workbook Component to the App
- Desktop Client-Side Import & Export Options in C# Apps
Server-Side C# Excel XLSX Import & Export
- Create a .NET 8 Console App
- Initialize a Server-Side C# Workbook Instance
- Invoke the Open Method to Read Existing Excel Files
- Invoke the Save Method to Write Excel Files
Client-Side C# Excel XLSX Import & Export
The client-side aspect of this piece focuses on two efficient solutions for importing and exporting XLSX files in .NET apps utilizing Spread WinForms. Learn how implementing a user-friendly button to access a Spreadsheet Designer App can improve user experience, or consider the benefits of incorporating a .NET Spreadsheet Ribbon Bar Control for streamlined data manipulation directly within the form. Join us as we explore these options, leveraging this .NET spreadsheet component to optimize client-side C# Excel import/export functionalities. Spread.NET also offers a WPF control - check out this other blog to learn how to implement import/export capabilities in a WPF application.
Download a Trial of this .NET Spreadsheet UI Control Today!
Create a .NET 8 WinForms App
In Visual Studio 2022, create a new project and select C# Windows Forms App (.NET Framework).
Download a fully functional 30-day trial version of Spread.NET directly from our website. You can also install the Spread.NET NuGet package. In the Solution Explorer, right-click Dependencies and select Manage NuGet Packages. In NuGet Package Manager, select NuGet.org as the Package source. Search for 'Spread.WinForms,' select GrapeCity.Spread.WinForms, and click Install.
Add the Client-Side C# Workbook UI to the App
From the VS Toolbox, drag the FpSpread component and drop it onto the form. Adjust the spreadsheet as needed.
Check out the Adding a Component to a Project documentation for additional information.
Client-Side Import & Export Options in C# .NET Apps
Invoke the Spreadsheet Designer During Runtime
Spread’s Designer App can be launched during the .NET app's runtime to allow end-users to import or export Excel XLSX files. Developers can optionally add a button that, when clicked, invokes the ShowDialog method which will display the Spread Designer as a modal dialog box for the specific .NET spreadsheet instance.
To accomplish this, first drag and drop the FpSpreadDesigner component in the form in addition to the FpSpread component.
In the Form1.cs file, invoke the ShowDialog method within the button click event like so:
private void button1_Click(object sender, EventArgs e)
{
fpSpreadDesigner1.ShowDialog(fpSpread1);
}
The Spread Designer App can now be launched during the WinForms App's runtime, allowing end-users to easily import/export Excel files.
Add a .NET Spreadsheet Ribbon Bar Control
Spread WinForms includes a spreadsheet Ribbon Bar control that can easily be added to the app by dragging and dropping the RibbonBar component from the VS Toolbox in the form.
In the Form1.cs file, add the following code to generate the default RibbonBar items and behaviors, and attach the control to the existing spreadsheet component.
using GrapeCity.Spreadsheet.WinForms.Ribbon;
namespace SPNET_Import_Export_Excel_2024
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ribbonBar1.GenerateDefaultItems();
// Attach ribbonBar control to FpSpread control
ribbonBar1.Attach(fpSpread1);
}
}
}
The Ribbon Bar now provides a familiar UI directly in the form, consisting of default tabs, groups, and group items. This includes a File tab that allows users to easily choose to Open or Save XLSX files.
To learn more, check out the How to Add a .NET Spreadsheet Ribbon Bar Control using C# blog.
Server-Side C# Excel XLSX Import & Export
In the server-side portion of this blog, we will discuss how to utilize the C# Excel library, Document Solutions for Excel (DsExcel .NET), to enhance data processing. Learn how to efficiently handle Excel files by initializing the .NET Excel API Workbook, reading existing XLSX files, and saving or writing new ones. Join us as we demonstrate the capabilities of .NET Excel API libraries in improving server-side Excel import/export functions, ultimately transforming how data is managed within your applications.
Download a Trial of this Server-side .NET Excel API Library Today!
Create a .NET 8 Console App
In Visual Studio 2022, create a new Visual C# Console App (.NET Core).
Add the DsExcel .NET references to the project. In the Solution Explorer, right-click Dependencies and select Manage NuGet Packages. In NuGet Package Manager, select NuGet.org as the Package source. Search for 'ds.documents,' select DS.Documents.Excel, and click Install.
Initialize a C# Excel Workbook Instance
In the Program.cs file, initialize a new C# Excel workbook using the Workbook class.
using GrapeCity.Documents.Excel;
// Create a new workbook
Workbook workbook = new Workbook();
Invoke the Open Method to Read Existing Excel Files
DsExcel’s API offers an Open method to read existing Excel files in the server-side workbook.
// Open XLSX file
workbook.Open("Profit loss statement.xlsx");
Invoke the Save Method to Write Excel Files
The C# Excel library also offers a Save method to write the server-side workbook to an external XLSX file.
// Modify the XLSX file
workbook.Worksheets[0].Range["C1"].Value = "2024";
workbook.Worksheets[0].Range["C1"].Formula = "=Now()";
workbook.Worksheets[0].Range["C1"].NumberFormat = "m/d/yy";
// Save workbook as XLSX file
workbook.Save("Profit loss statement 2024.xlsx");
For more information, see the DsExcel .NET documentation on opening and saving workbooks, as well as the online demos for saving to Excel and importing Excel files.
Check out our other blogs that discuss Importing & Exporting Excel files using different development frameworks: WPF | JavaScript | React | Angular | Vue
Learn More About These .NET Spreadsheet Solutions
This article only scratches the surface of the full capabilities of Spread.NET and DsExcel .NET. To learn more about the .NET client-side spreadsheet component, see Spread.NET’s demo explorers and online documentation. To learn more about the C# Excel library, see DsExcel .NET’s online demo explorer and documentation.