How to Load Existing Spreadsheets
CSV (Comma-Separated Values) file format is for tabular data where values are separated by commas, commonly used for data exchange. On the other hand, TSV (Tab-Separated Values) uses tabs to separate values, preferred when data contains commas.
The DataSet
class in Microsoft's .NET is a part of the ADO.NET (ActiveX Data Objects for .NET) technology. It's often used in database-related applications and allows you to work with data from various sources like databases, XML, and more.
Data contained in Excel file formats such as XLSX, XLS, XLSM, XLTX, CSV, and TSV as well as a DataSet object can be loaded into an Excel spreadsheet using IronXL.
How to Load Existing Spreadsheets
- Download the C# library to load spreadsheet
- Prepare the file or data object to be loaded into the spreadsheet
- Use the
Load
method to load the data into a workbook object - Use the
LoadWorkSheetsFromDataSet
method to load data from a DataSet object - Export the workbook as an Excel file
Get started with IronXL
Start using IronXL in your project today with a free trial.
Load Spreadsheet Example
Use the static method Load
to load an existing Excel workbook. The method supports XLSX, XLS, XLSM, XLTX, CSV, and TSV file formats. In cases where the workbook is protected with a password, you can pass the password as the second parameter to the method. The method also accepts workbook data in the form of a byte array or a stream, where the dedicated FromByteArray
and FromStream
methods can be used, respectively.
:path=/static-assets/excel/content-code-examples/how-to/load-spreadsheet-load-spreadsheet.cs
using IronXL; // Import the IronXL library for working with Excel files.
// Instead of loading the workbook without error handling, wrap it in a try-catch block for safety.
try
{
// Load an Excel workbook from a file. The IronXL library supports various file formats including XLSX, XLS, XLSM, XLTX, CSV, and TSV.
WorkBook workBook = WorkBook.Load("sample.xlsx");
// At this point, 'workBook' is an instance of the loaded workbook.
// You can now proceed to manipulate the workbook, such as accessing worksheets, reading data, etc.
// Example: Accessing the first worksheet
WorkSheet sheet = workBook.WorkSheets.First();
// You might want to perform operations like iterating over rows, reading cell values, etc.
// Example: Reading a value from a specific cell
string cellValue = sheet["A1"].Text;
// Optionally, output the value from the specified cell to confirm reading was successful
Console.WriteLine($"The value in cell A1 is: {cellValue}");
}
catch (Exception ex)
{
// It's a good practice to handle exceptions in case the workbook file doesn't exist or there's an issue with reading it.
Console.WriteLine($"An error occurred while loading or processing the workbook: {ex.Message}");
}
Load CSV file
While the Load
method can read all available file formats, it is recommended to use the LoadCSV
method specifically for CSV file formats for optimal handling.
:path=/static-assets/excel/content-code-examples/how-to/load-spreadsheet-load-csv.cs
using IronXL;
// This code demonstrates how to load a CSV file using the IronXL library.
// IronXL must be installed via NuGet package manager in your project to work with WorkBook class.
// Load a CSV file into a WorkBook object. The path to the CSV file is specified as "sample.csv".
// Ensure "sample.csv" is present in the project directory or provide the correct path to the file.
WorkBook workBook = WorkBook.LoadCSV("sample.csv");
// At this point, the CSV file is loaded into the `workBook` object.
// You can now perform various operations on the loaded data, such as reading or modifying cell values.
Load DataSet
The DataSet
class in Microsoft .NET is used for managing and working with data in a disconnected, in-memory representation. This DataSet
can also be loaded into the workbook using the LoadWorkSheetsFromDataSet
method. In the code example below, an empty DataSet
is created; however, it's more common to instantiate the DataSet
from a database query.
:path=/static-assets/excel/content-code-examples/how-to/load-spreadsheet-load-dataset.cs
using IronXL;
using System.Data;
// This code demonstrates how to create an Excel workbook from a DataSet using IronXL,
// a .NET library for working with Excel files.
// Create a new DataSet to hold data tables, which provides in-memory data storage.
DataSet dataSet = new DataSet();
// Example of adding a DataTable to the DataSet for demonstration.
// In an actual implementation, populate the DataSet with real data tables.
DataTable table = new DataTable("SampleTable");
dataSet.Tables.Add(table);
// Create a new WorkBook using IronXL. This will represent an Excel workbook.
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
// Load worksheets into the WorkBook from the DataSet.
// Each DataTable in the DataSet will result in a new worksheet in the WorkBook.
workBook.LoadWorkSheetsFromDataSet(dataSet);
// Add comments to clarify:
// - If the DataSet is empty, the WorkBook will be created without any worksheets.
// - Ensure that the DataSet is populated with one or more tables to have data reflected in the workbook.
Frequently Asked Questions
What file formats can be loaded using IronXL?
IronXL supports loading XLSX, XLS, XLSM, XLTX, CSV, and TSV file formats.
How can I load a password-protected Excel workbook using IronXL?
To load a password-protected Excel workbook, use the Load method and pass the password as the second parameter.
Can IronXL load data directly from a byte array or stream?
Yes, IronXL can load workbook data from a byte array using the LoadFromByteArray method and from a stream using the LoadFromStream method.
What is the recommended method for loading CSV files in IronXL?
While the Load method can read CSV files, it is recommended to use the LoadCSV method for optimal handling of CSV file formats.
How does IronXL handle loading data from a DataSet object?
IronXL can load data from a DataSet object using the LoadWorkSheetsFromDataSet method, creating one worksheet per DataTable in the DataSet.
What is the purpose of the DataSet class in .NET?
The DataSet class in .NET is used for managing and working with data in a disconnected, in-memory representation, typically filled from database queries.
How can I get started with using IronXL to load spreadsheets?
To get started with IronXL, download the C# library from NuGet, prepare your file or data object, and use the Load method to load the data into a workbook object.