Tutorial de C# para Excel: Aprenda a usar a biblioteca IronXL (sem interoperabilidade)

A Guide to Reading and Writing Excel Files in C#

This article was translated from English: Does it need improvement?
Translated
View the article in English

Ler e criar arquivos Excel (XLS, XLSX e CSV) em C# e outras linguagens .NET é fácil utilizando a biblioteca de software IronXL da Iron Software.

IronXL não requer que o Excel Interop esteja instalado no seu servidor. IronXL fornece uma API mais rápida e intuitiva do que Microsoft.Office.Interop.Excel.

IronXL funciona nas seguintes plataformas:

  • .NET Framework 4.6.2 e superior para Windows e Azure
  • .NET Core 2 e superior para Windows, Linux, MacOS e Azure
  • .NET 5, .NET 6, .NET 7, .NET 8, Mono, Maui e Xamarin

Instale o IronXL

Firstly install IronXL, using our NuGet package or by downloading the DLL. IronXL classes can be found in the IronXL namespace.

A maneira mais fácil de instalar o IronXL é usando o Gerenciador de Pacotes NuGet para o Visual-Studio: O nome do pacote é IronXl.Excel.

Install-Package IronXl.Excel

https://www.nuget.org/packages/ironxl.excel/

Como ler um documento do Excel

Com o IronXL, extrair dados de um arquivo Excel pode ser feito em apenas algumas linhas de código.

:path=/static-assets/excel/content-code-examples/get-started/get-started-1.cs
using IronXL;

// Supported spreadsheet formats for reading include: XLSX, XLS, CSV and TSV
WorkBook workBook = WorkBook.Load("data.xlsx");
WorkSheet workSheet = workBook.WorkSheets.First();

// Select cells easily in Excel notation and return the calculated value, date, text or formula
int cellValue = workSheet["A2"].IntValue;

// Read from Ranges of cells elegantly.
foreach (var cell in workSheet["A2:B10"])
{
    Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
$vbLabelText   $csharpLabel

Criando novos documentos do Excel

IronXL oferece uma interface rápida e fácil para gerar documentos Excel usando C# ou VB.NET.

:path=/static-assets/excel/content-code-examples/get-started/get-started-2.cs
using IronXL;

// Create new Excel WorkBook document.
WorkBook workBook = WorkBook.Create(ExcelFileFormat.XLSX);
workBook.Metadata.Author = "IronXL";

// Add a blank WorkSheet
WorkSheet workSheet = workBook.CreateWorkSheet("main_sheet");

// Add data and styles to the new worksheet
workSheet["A1"].Value = "Hello World";
workSheet["A2"].Style.BottomBorder.SetColor("#ff6600");
workSheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Double;

// Save the excel file
workBook.SaveAs("NewExcelFile.xlsx");
$vbLabelText   $csharpLabel

Exportação em CSV, XLS, XLSX, JSON ou XML

IronXL também permite que você salve ou exporte dados para uma variedade de formatos de planilhas estruturados e populares.

:path=/static-assets/excel/content-code-examples/get-started/get-started-3.cs
// Export to many formats with fluent saving
workSheet.SaveAs("NewExcelFile.xls");
workSheet.SaveAs("NewExcelFile.xlsx");
workSheet.SaveAsCsv("NewExcelFile.csv");
workSheet.SaveAsJson("NewExcelFile.json");
workSheet.SaveAsXml("NewExcelFile.xml");
$vbLabelText   $csharpLabel

Estilizando células e intervalos

Você pode aplicar formatação a células e intervalos de Excel usando o objeto IronXl.Range.Style.

:path=/static-assets/excel/content-code-examples/get-started/get-started-4.cs
// Set cell's value and styles
workSheet["A1"].Value = "Hello World";
workSheet["A2"].Style.BottomBorder.SetColor("#ff6600");
workSheet["A2"].Style.BottomBorder.Type = IronXL.Styles.BorderType.Double;
$vbLabelText   $csharpLabel

Intervalos de classificação

Com o IronXL, você pode ordenar um intervalo de células Excel facilmente utilizando o objeto Range.

:path=/static-assets/excel/content-code-examples/get-started/get-started-5.cs
using IronXL;
using Range = IronXL.Range;

WorkBook workBook = WorkBook.Load("test.xls");
WorkSheet workSheet = workBook.WorkSheets.First();

// This is how we get range from Excel worksheet
Range range = workSheet["A2:A8"];

// Sort the range in the sheet
range.SortAscending();
workBook.Save();
$vbLabelText   $csharpLabel

Editando fórmulas

Modificar uma fórmula Excel é tão simples quanto atribuir um valor que começa com um sinal de "=". A fórmula será calculada instantaneamente.

:path=/static-assets/excel/content-code-examples/get-started/get-started-6.cs
// Set a formula
workSheet["A1"].Value = "=SUM(A2:A10)";

// Get the calculated value
decimal sum = workSheet["A1"].DecimalValue;
$vbLabelText   $csharpLabel

Por que Escolher o IronXL?

IronXL oferece uma API amigável para desenvolvedores para ler e escrever documentos Excel em .NET. Funciona sem requerer que o Microsoft Excel ou Excel Interop estejam instalados no servidor, tornando o manuseio de arquivos Excel rápido, leve e sem complicações.

Seguindo em frente

Para explorar mais recursos e capacidades, recomendamos revisar a Referência de API .NET formatada de forma semelhante à documentação MSDN.

Curtis Chau
Redator Técnico

Curtis Chau é bacharel em Ciência da Computação (Universidade Carleton) e se especializa em desenvolvimento front-end, com experiência em Node.js, TypeScript, JavaScript e React. Apaixonado por criar interfaces de usuário intuitivas e esteticamente agradáveis, Curtis gosta de trabalhar com frameworks modernos e criar manuais ...

Leia mais

A PHP Error was encountered

Severity: Notice

Message: Undefined index: IronXl.Excel

Filename: helpers/counter_helper.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/helpers/counter_helper.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/views/main/sections/ready_to_started_202509.php
Line: 12
Function: getTotalDonwloadNumber

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 489
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Docs.php
Line: 29
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Notice

Message: Undefined index: IronXl.Excel

Filename: helpers/counter_helper.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/helpers/counter_helper.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/views/main/sections/ready_to_started_202509.php
Line: 19
Function: getTotalDonwloadNumber

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 489
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Docs.php
Line: 29
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

Pronto para começar?
Nuget Downloads 1,890,100 | Versão: 2026.3 acaba de ser lançado

A PHP Error was encountered

Severity: Notice

Message: Undefined index: IronXl.Excel

Filename: helpers/counter_helper.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/helpers/counter_helper.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/views/main/sections/still_scrolling_202512.php
Line: 17
Function: getTotalDonwloadNumber

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 71
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/docs/docs.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 550
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Docs.php
Line: 29
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

A PHP Error was encountered

Severity: Notice

Message: Undefined index: IronXl.Excel

Filename: helpers/counter_helper.php

Line Number: 85

Backtrace:

File: /var/www/ironpdf.com/application/helpers/counter_helper.php
Line: 85
Function: _error_handler

File: /var/www/ironpdf.com/application/views/main/sections/still_scrolling_202512.php
Line: 24
Function: getTotalDonwloadNumber

File: /var/www/ironpdf.com/application/views/products/sections/three_column_docs_page_structure.php
Line: 71
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/views/products/docs/docs.php
Line: 2
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 88
Function: view

File: /var/www/ironpdf.com/application/libraries/Render.php
Line: 550
Function: view

File: /var/www/ironpdf.com/application/controllers/Products/Docs.php
Line: 29
Function: render_products_view

File: /var/www/ironpdf.com/index.php
Line: 292
Function: require_once

Still Scrolling Icon

Ainda está rolando a tela?

Quer provas rápidas? PM > Install-Package IronXl.Excel
executar um exemplo Veja seus dados se transformarem em uma planilha.