How to Import or Export data to Excel or HTML files from your .NET applications?

Following examples show two different ways to import or export data to Excel or HTML files by using only GemBox.Spreadsheet .NET component.

GemBox.Spreadsheet is a C# / VB.NET component which provides simple and efficient way to read, write, convert and print native Microsoft Excel (XLSX, XLS) and other spreadsheet (ODS, CSV) formats and various document formats (HTML, PDF, XPS) from .NET applications without the need for Microsoft Excel on either the developer or client machines.
GemBox.Spreadsheet Free is free of charge while GemBox.Spreadsheet Professional is a commercial version licensed per developer.
For more information, see GemBox.Spreadsheet features and why GemBox.Spreadsheet outperforms Excel automation.

First example imports/exports data to Excel file (in XLSX format) by directly working with DataTable using CreateDataTable and InsertDataTable methods:

C# code:

// Load Excel file.
var workbook = ExcelFile.Load("Workbook.xlsx");

// Select active worksheet from the file.
var worksheet = workbook.Worksheets.ActiveWorksheet;

// Extract the data from the worksheet to newly created DataTable starting at
// first row and first column for 10 rows or until the first empty row appears.
var dataTable = worksheet.CreateDataTable(new CreateDataTableOptions()
{
StartRow = 0,
StartColumn = 0,
NumberOfRows = 10,
ExtractDataOptions = ExtractDataOptions.StopAtFirstEmptyRow
});

// Change the value of the first cell in the DataTable.
dataTable.Rows[0][0] = "Hello world!";

// Insert the data from DataTable to the worksheet starting at cell "A1".
worksheet.InsertDataTable(dataTable,
new InsertDataTableOptions("A1") { ColumnHeaders = true });

// Save the file to XLS format.
workbook.Save("DataTable.xlsx");

Visual Basic .NET code:

' Load Excel file.
Dim workbook = ExcelFile.Load("Workbook.xlsx")

' Select active worksheet from the file.
Dim worksheet = workbook.Worksheets.ActiveWorksheet

' Extract the data from the worksheet to newly created DataTable starting at
' first row and first column for 10 rows or until the first empty row appears.
Dim dataTable = worksheet.CreateDataTable(New CreateDataTableOptions() With
{
.StartRow = 0,
.StartColumn = 0,
.NumberOfRows = 10,
.ExtractDataOptions = ExtractDataOptions.StopAtFirstEmptyRow
})

' Change the value of the first cell in the DataTable.
dataTable.Rows(0)(0) = "Hello world!"

' Insert the data from DataTable to the worksheet starting at cell "A1".
worksheet.InsertDataTable(dataTable,
New InsertDataTableOptions("A1") With {.ColumnHeaders = True})

' Save the file to XLS format.
workbook.Save("DataTable.xlsx")

Second example imports/exports data to Excel file (in XLS format) by direct access to cells in the active worksheet:

C# code:

// Load Excel file.
var workbook = ExcelFile.Load("Workbook.xls");

// Select active worksheet.
var worksheet = workbook.Worksheets.ActiveWorksheet;

// Select the first row from the worksheet.
var row = worksheet.Rows[0];

// Write every cell of the first row to Console.
foreach (var cell in row.AllocatedCells)
Console.WriteLine(cell.GetFormattedValue());

// Write "Hello world!" to the first cell of the worksheet.
worksheet.Cells[0, 0].Value = "Hello world!";

// Save the file in XLS format.
workbook.Save("NewWorkbook.xls");

Visual Basic .NET code:

' Load Excel file.
Dim workbook = ExcelFile.Load("Workbook.xls")

' Select active worksheet.
Dim worksheet = workbook.Worksheets.ActiveWorksheet

' Select the first row from the worksheet.
Dim row = worksheet.Rows(0)

' Write every cell of the first row to Console.
For Each cell In row.AllocatedCells
Console.WriteLine(cell.GetFormattedValue())
Next

' Write "Hello world!" to the first cell of the worksheet.
worksheet.Cells(0, 0).Value = "Hello world!"

' Save the file in XLS format.
workbook.Save("NewWorkbook.xls")