How to Load Existing Spreadsheets in C#
Load existing Excel, CSV, TSV files and DataSet objects into C# spreadsheets using IronXL's WorkBook.Load() method for full Excel support or LoadCSV() for CSV formats, enabling seamless data manipulation and extraction in .NET applications.
CSV (Comma-Separated Values) file format is for tabular data where values are separated by commas, commonly used for data exchange. TSV (Tab-Separated Values) uses tabs to separate values, preferred when data contains commas. Learn more about converting between file formats.
The DataSet class in Microsoft's .NET is 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. IronXL provides seamless integration for importing and exporting DataSet objects.
Data contained in Excel file formats such as XLSX, XLS, XLSM, XLTX, CSV, and TSV as well as DataSet objects can be loaded into an Excel spreadsheet using IronXL. This powerful capability makes IronXL an essential tool for developers working with spreadsheet data in business applications, data analysis projects, and automated reporting systems.
Quickstart: Load an Existing Spreadsheet
Using one simple method, you can load an existing Excel, CSV, or TSV file into a WorkBook with IronXL. Developers can quickly get started editing or extracting data by calling WorkBook.Load(...) for full Excel support, or LoadCSV(...) specifically for CSV formats. For comprehensive API documentation, visit the IronXL API Reference.
Get started making PDFs with NuGet now:
Install IronXL with NuGet Package Manager
Copy and run this code snippet.
IronXL.WorkBook workbook = IronXL.WorkBook.Load("sample.xlsx");Deploy to test on your live environment
Minimal Workflow (5 steps)
- Download the C# library to load spreadsheet
- Prepare the file or data object to be loaded into the spreadsheet
- Use the
Loadmethod to load the data into a workbook object - Use the
LoadWorkSheetsFromDataSetmethod to load data from a DataSet object - Export the workbook as an Excel file
How Do I Load Excel Spreadsheet Files in C#?
Use the static method Load to load an existing Excel workbook. The method supports XLSX, XLS, XLSM, XLTX, CSV, and TSV file formats. When the workbook is protected with a password, pass the password as the second parameter to the method. The method also accepts workbook data as a byte array or stream, where the dedicated FromByteArray and FromStream methods can be used, respectively.
IronXL's loading capabilities are designed to handle various scenarios in real-world applications. Whether you're processing files uploaded by users, reading from network streams, or working with encrypted documents, the library provides a consistent and reliable interface. The Load method automatically detects the file format based on the file extension and content, making it versatile for different use cases.
Which File Formats Can I Load?
:path=/static-assets/excel/content-code-examples/how-to/load-spreadsheet-load-spreadsheet.csusing IronXL;
// Supported for XLSX, XLS, XLSM, XLTX, CSV and TSV
WorkBook workBook = WorkBook.Load("sample.xlsx");Imports IronXL
' Supported for XLSX, XLS, XLSM, XLTX, CSV and TSV
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")The flexibility to load multiple file formats means you can work with legacy Excel files (XLS), modern Excel workbooks (XLSX), macro-enabled workbooks (XLSM), and template files (XLTX). This compatibility ensures smooth migration from older systems while supporting modern Excel features. For more details on creating new spreadsheets, check out our comprehensive guide.
How Do I Handle Password-Protected Files?
Working with sensitive data often requires password protection. IronXL makes it simple to load password-protected workbooks by providing the password as a parameter:
// Load password-protected Excel file
WorkBook protectedWorkBook = WorkBook.Load("protected.xlsx", "myPassword123");
// Work with the workbook normally
WorkSheet sheet = protectedWorkBook.DefaultWorkSheet;
sheet["B2"].Value = "Updated secure data";
// Save with password protection
protectedWorkBook.SaveAs("updated_protected.xlsx");// Load password-protected Excel file
WorkBook protectedWorkBook = WorkBook.Load("protected.xlsx", "myPassword123");
// Work with the workbook normally
WorkSheet sheet = protectedWorkBook.DefaultWorkSheet;
sheet["B2"].Value = "Updated secure data";
// Save with password protection
protectedWorkBook.SaveAs("updated_protected.xlsx");IRON VB CONVERTER ERROR developers@ironsoftware.comWhat About Loading from Byte Arrays or Streams?
In modern applications, files often come from various sources like web uploads, API responses, or database BLOBs. IronXL handles these scenarios elegantly:
// Loading from byte array
byte[] excelBytes = File.ReadAllBytes("sample.xlsx");
WorkBook workBookFromBytes = WorkBook.FromByteArray(excelBytes);
// Loading from stream
using (FileStream stream = new FileStream("sample.xlsx", FileMode.Open))
{
WorkBook workBookFromStream = WorkBook.FromStream(stream);
// Process the workbook
var sheet = workBookFromStream.DefaultWorkSheet;
Console.WriteLine($"Sheet has {sheet.RowCount} rows");
}
// Loading from MemoryStream (common in web applications)
using (MemoryStream memStream = new MemoryStream(excelBytes))
{
WorkBook workBookFromMemory = WorkBook.FromStream(memStream);
}// Loading from byte array
byte[] excelBytes = File.ReadAllBytes("sample.xlsx");
WorkBook workBookFromBytes = WorkBook.FromByteArray(excelBytes);
// Loading from stream
using (FileStream stream = new FileStream("sample.xlsx", FileMode.Open))
{
WorkBook workBookFromStream = WorkBook.FromStream(stream);
// Process the workbook
var sheet = workBookFromStream.DefaultWorkSheet;
Console.WriteLine($"Sheet has {sheet.RowCount} rows");
}
// Loading from MemoryStream (common in web applications)
using (MemoryStream memStream = new MemoryStream(excelBytes))
{
WorkBook workBookFromMemory = WorkBook.FromStream(memStream);
}IRON VB CONVERTER ERROR developers@ironsoftware.comWhy Should I Use a Dedicated Method for CSV Files?
While the Load method can read all available file formats, use the LoadCSV method specifically for CSV file formats for optimal handling. CSV files have unique characteristics that benefit from specialized processing, such as delimiter detection, encoding handling, and data type inference. It's particularly useful when dealing with international data formats or when CSV files use non-standard delimiters like semicolons or pipes.
When Is LoadCSV Better Than Load?
The LoadCSV method provides enhanced control over CSV parsing, making it ideal when you need to specify custom delimiters, handle different encodings, or work with large CSV files. It's particularly useful when dealing with international data formats or when CSV files use non-standard delimiters like semicolons or pipes.
How Does LoadCSV Handle Special Characters?
:path=/static-assets/excel/content-code-examples/how-to/load-spreadsheet-load-csv.csusing IronXL;
// Load CSV file
WorkBook workBook = WorkBook.LoadCSV("sample.csv");Imports IronXL
' Load CSV file
Private workBook As WorkBook = WorkBook.LoadCSV("sample.csv")The LoadCSV method automatically handles various encodings including UTF-8, UTF-16, and ASCII, ensuring that special characters, accented letters, and international symbols are preserved correctly. This is crucial when working with multilingual data or scientific notation. Learn more about writing CSV files in .NET for complete CSV workflow management.
How Can I Load DataSet Objects into Excel?
The DataSet class in Microsoft .NET is used for managing and working with data in a disconnected, in-memory representation. This DataSet can 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.
Why Would I Convert DataSet to Excel?
Converting DataSet objects to Excel format is essential for reporting, data export, and creating user-friendly data presentations. Business users often prefer Excel formats for data analysis, and automated reports frequently need to transform database results into spreadsheet formats. This conversion enables features like conditional formatting, chart creation, and formula application that aren't available in raw DataSet objects.
What Happens to DataSet Relationships in Excel?
When loading a DataSet with multiple related tables, IronXL creates separate worksheets for each DataTable in the DataSet. Table relationships are preserved through consistent key values, allowing users to maintain data integrity when working with the exported Excel file. This makes it ideal for exporting complex database schemas while maintaining referential integrity.
Can I Load Multiple Tables from DataSet?
:path=/static-assets/excel/content-code-examples/how-to/load-spreadsheet-load-dataset.csusing IronXL;
using System.Data;
// Create dataset
DataSet dataSet = new DataSet();
// Create workbook
WorkBook workBook = WorkBook.Create();
// Load DataSet
WorkBook.LoadWorkSheetsFromDataSet(dataSet, workBook);Imports IronXL
Imports System.Data
' Create dataset
Private dataSet As New DataSet()
' Create workbook
Private workBook As WorkBook = WorkBook.Create()
' Load DataSet
WorkBook.LoadWorkSheetsFromDataSet(dataSet, workBook)For more advanced scenarios involving database integration, explore our guide on Excel to SQL via System.Data.DataSet which demonstrates bidirectional data flow between Excel and databases.
The ability to load various data sources into Excel format makes IronXL an invaluable tool for .NET developers. Whether you're building reporting systems, data migration tools, or business intelligence applications, the loading capabilities provide the foundation for powerful spreadsheet manipulation. For more examples and advanced techniques, visit our comprehensive tutorials section.
Frequently Asked Questions
What file formats can I load into C# spreadsheets?
IronXL supports loading multiple file formats including XLSX, XLS, XLSM, XLTX, CSV, and TSV files, as well as DataSet objects. This comprehensive format support makes IronXL suitable for various spreadsheet data manipulation needs in .NET applications.
How do I load an Excel file in C#?
You can load an Excel file using IronXL's WorkBook.Load() method with just one line of code: IronXL.WorkBook workbook = IronXL.WorkBook.Load("sample.xlsx"). For CSV files specifically, you can use the LoadCSV() method.
Can I load password-protected Excel files?
Yes, IronXL supports loading password-protected workbooks. Simply pass the password as the second parameter to the Load method when opening encrypted Excel files.
What's the difference between CSV and TSV file formats?
CSV (Comma-Separated Values) uses commas to separate values and is commonly used for data exchange. TSV (Tab-Separated Values) uses tabs instead, which is preferred when your data contains commas. IronXL supports loading both formats seamlessly.
Can I load Excel data from sources other than files?
Yes, IronXL can load workbook data from multiple sources including byte arrays using the FromByteArray method and streams using the FromStream method. This flexibility allows processing files from uploads, network streams, or memory.
How do I work with DataSet objects in spreadsheets?
IronXL provides the LoadWorkSheetsFromDataSet method to import data from ADO.NET DataSet objects into Excel spreadsheets. This integration enables seamless data transfer between database applications and Excel workbooks.
What are the main steps to load and export spreadsheet data?
The workflow involves: 1) Download IronXL library, 2) Prepare your file or DataSet, 3) Use the Load method to create a workbook object, 4) Use LoadWorkSheetsFromDataSet for DataSet imports, and 5) Export the workbook as needed.
Is there comprehensive documentation available for the loading methods?
Yes, IronXL provides complete API documentation including detailed information about all loading methods and their parameters in the IronXL API Reference, helping developers implement spreadsheet loading functionality effectively.






