Maneira mais rápida de preencher um arquivo Excel no IronXL
Quando você constrói planilhas grandes no IronXL, o método de carregamento de dados que você escolhe tem um grande efeito sobre quanto tempo a geração leva. O benchmark abaixo compara três abordagens comuns contra o mesmo conjunto de dados de 20.000 linhas por 55 colunas.
Benchmark
Cada teste preenche dados idênticos e grava o resultado no disco como um arquivo .xlsx. Tanto a duração (em segundos) quanto o tamanho do arquivo resultante foram registrados.
- Adicionando dados célula por célula: ~13 seg, 3094 KB
- Carregando de
DataTable: ~3,4 seg, 3094 KB - Carregando de CSV: ~4,1 seg, 3094 KB
Todos os três produzem o mesmo tamanho de arquivo. O que muda drasticamente é o tempo para gerá-lo.
Os Métodos
Opção 1: Adicionando Dados Célula por Célula
Esta abordagem atribui um valor para cada célula individual através de loops aninhados. É a mais lenta das três.
string[] fruits = { "Apples", "Apricot", "Banana", "Blackberry", "Blueberry", "Boysenberry", "Canary Melon", "Cantaloupe",
"Currants", "Dates (tree dried only) Dragon Fruit", "Durian (purchase cut) Figs", "Gooseberry", "Grapes", "Grapefruit",
"Lemon", "Lime", "Loganberry", "Longan", "Loquat", "Lychee", "Mandarin", "Mango",
"Blood Orange", "Papaya", "Passion Fruit", "Peach", "Pear", "Persimmon", "Pineapple", "Plum", "Honeydew",
"Rambutan", "Starfruit", "Tamarind", "Yuzu", "Açaí", "Abiu", "Ackee", "Breadfruit", "Cempedak",
"Cherimoya", "Buddha's Hand", "Citron", "Finger Lime", "Kumquat", "Pomelo", "Tangelo", "Ugli Fruit",
"Galia Melon", "Kiwano (Horned Melon)", "Mouse Melon", "Muskmelon",
"cherry", "nectarine", "Butternut squash"};
var workbook = WorkBook.Create(ExcelFileFormat.XLSX);
var worksheet = workbook.CreateWorkSheet("Fruits");
var columnsTitles = GetExcelCellLetters(fruits.Length);
for (int col = 0; col < columnsTitles.Count; col++)
{
string columnLetter = columnsTitles[col]; // To avoid interpolation that can cause performance issues in large loops because it build string objects in every loop
for (int row = 1; row <= 20000; row++)
{
worksheet[columnLetter + row].Value = fruits[col];
//worksheet.Rows[row].Columns[columnLetter].Value.ToString();
}
}
workbook.SaveAs("C:\\Temp\\ManualCell-by-CellAssignment.xlsx");
private static List<string> GetExcelCellLetters(int iMaxNum)
{
string[] alphabet = { string.Empty, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
IEnumerable<string> lst = (from c1 in alphabet
from c2 in alphabet
from c3 in alphabet.Skip(1)
where c1 == string.Empty || c2 != string.Empty
select c1 + c2 + c3).Take(iMaxNum);
return lst.ToList();
}Dim fruits() As String = {"Apples", "Apricot", "Banana", "Blackberry", "Blueberry", "Boysenberry", "Canary Melon", "Cantaloupe", "Currants", "Dates (tree dried only) Dragon Fruit", "Durian (purchase cut) Figs", "Gooseberry", "Grapes", "Grapefruit", "Lemon", "Lime", "Loganberry", "Longan", "Loquat", "Lychee", "Mandarin", "Mango", "Blood Orange", "Papaya", "Passion Fruit", "Peach", "Pear", "Persimmon", "Pineapple", "Plum", "Honeydew", "Rambutan", "Starfruit", "Tamarind", "Yuzu", "Açaí", "Abiu", "Ackee", "Breadfruit", "Cempedak", "Cherimoya", "Buddha's Hand", "Citron", "Finger Lime", "Kumquat", "Pomelo", "Tangelo", "Ugli Fruit", "Galia Melon", "Kiwano (Horned Melon)", "Mouse Melon", "Muskmelon", "cherry", "nectarine", "Butternut squash"}
Dim workbook = WorkBook.Create(ExcelFileFormat.XLSX)
Dim worksheet = workbook.CreateWorkSheet("Fruits")
Dim columnsTitles = GetExcelCellLetters(fruits.Length)
For col As Integer = 0 To columnsTitles.Count - 1
Dim columnLetter As String = columnsTitles(col)
For row As Integer = 1 To 20000
worksheet(columnLetter & row).Value = fruits(col)
Next
Next
workbook.SaveAs("C:\Temp\ManualCell-by-CellAssignment.xlsx")
Private Shared Function GetExcelCellLetters(ByVal iMaxNum As Integer) As List(Of String)
Dim alphabet() As String = {String.Empty, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
Dim lst As IEnumerable(Of String) = (From c1 In alphabet From c2 In alphabet From c3 In alphabet.Skip(1) Where c1 = String.Empty OrElse c2 <> String.Empty Select c1 & c2 & c3).Take(iMaxNum)
Return lst.ToList()
End FunctionDim fruits() As String = {"Apples", "Apricot", "Banana", "Blackberry", "Blueberry", "Boysenberry", "Canary Melon", "Cantaloupe", "Currants", "Dates (tree dried only) Dragon Fruit", "Durian (purchase cut) Figs", "Gooseberry", "Grapes", "Grapefruit", "Lemon", "Lime", "Loganberry", "Longan", "Loquat", "Lychee", "Mandarin", "Mango", "Blood Orange", "Papaya", "Passion Fruit", "Peach", "Pear", "Persimmon", "Pineapple", "Plum", "Honeydew", "Rambutan", "Starfruit", "Tamarind", "Yuzu", "Açaí", "Abiu", "Ackee", "Breadfruit", "Cempedak", "Cherimoya", "Buddha's Hand", "Citron", "Finger Lime", "Kumquat", "Pomelo", "Tangelo", "Ugli Fruit", "Galia Melon", "Kiwano (Horned Melon)", "Mouse Melon", "Muskmelon", "cherry", "nectarine", "Butternut squash"}
Dim workbook = WorkBook.Create(ExcelFileFormat.XLSX)
Dim worksheet = workbook.CreateWorkSheet("Fruits")
Dim columnsTitles = GetExcelCellLetters(fruits.Length)
For col As Integer = 0 To columnsTitles.Count - 1
Dim columnLetter As String = columnsTitles(col)
For row As Integer = 1 To 20000
worksheet(columnLetter & row).Value = fruits(col)
Next
Next
workbook.SaveAs("C:\Temp\ManualCell-by-CellAssignment.xlsx")
Private Shared Function GetExcelCellLetters(ByVal iMaxNum As Integer) As List(Of String)
Dim alphabet() As String = {String.Empty, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
Dim lst As IEnumerable(Of String) = (From c1 In alphabet From c2 In alphabet From c3 In alphabet.Skip(1) Where c1 = String.Empty OrElse c2 <> String.Empty Select c1 & c2 & c3).Take(iMaxNum)
Return lst.ToList()
End FunctionPré-computar columnLetter fora do loop interno evita a interpolação de strings em cada iteração, que caso contrário aloca novas strings milhões de vezes.

Opção 2: Carregamento a partir de um DataTable
Construa um DataTable na memória, em seguida, entregue-o ao IronXL em uma única chamada com LoadWorkSheet. Esta é uma das duas opções mais rápidas, e a partir de v2026.10.1 é tão rápido quanto CSV ou mais rápido, e muito mais rápido do que a inserção manual de células.
string[] fruits = { "Apples", "Apricot", "Banana", "Blackberry", "Blueberry", "Boysenberry", "Canary Melon", "Cantaloupe",
"Currants", "Dates (tree dried only) Dragon Fruit", "Durian (purchase cut) Figs", "Gooseberry", "Grapes", "Grapefruit",
"Lemon", "Lime", "Loganberry", "Longan", "Loquat", "Lychee", "Mandarin", "Mango",
"Blood Orange", "Papaya", "Passion Fruit", "Peach", "Pear", "Persimmon", "Pineapple", "Plum", "Honeydew",
"Rambutan", "Starfruit", "Tamarind", "Yuzu", "Açaí", "Abiu", "Ackee", "Breadfruit", "Cempedak",
"Cherimoya", "Buddha's Hand", "Citron", "Finger Lime", "Kumquat", "Pomelo", "Tangelo", "Ugli Fruit",
"Galia Melon", "Kiwano (Horned Melon)", "Mouse Melon", "Muskmelon",
"cherry", "nectarine", "Butternut squash"};
var workbook = WorkBook.Create(ExcelFileFormat.XLSX);
DataTable table = new DataTable("Fruits");
foreach (string fruitName in fruits)
{
table.Columns.Add(fruitName);
}
int rowCount = 20000;
for (int i = 0; i < rowCount; i++)
{
table.Rows.Add(fruits);
}
workbook.LoadWorkSheet(table);
workbook.SaveAs("C:\\Temp\\LoadDataTableDirectly.xlsx");string[] fruits = { "Apples", "Apricot", "Banana", "Blackberry", "Blueberry", "Boysenberry", "Canary Melon", "Cantaloupe",
"Currants", "Dates (tree dried only) Dragon Fruit", "Durian (purchase cut) Figs", "Gooseberry", "Grapes", "Grapefruit",
"Lemon", "Lime", "Loganberry", "Longan", "Loquat", "Lychee", "Mandarin", "Mango",
"Blood Orange", "Papaya", "Passion Fruit", "Peach", "Pear", "Persimmon", "Pineapple", "Plum", "Honeydew",
"Rambutan", "Starfruit", "Tamarind", "Yuzu", "Açaí", "Abiu", "Ackee", "Breadfruit", "Cempedak",
"Cherimoya", "Buddha's Hand", "Citron", "Finger Lime", "Kumquat", "Pomelo", "Tangelo", "Ugli Fruit",
"Galia Melon", "Kiwano (Horned Melon)", "Mouse Melon", "Muskmelon",
"cherry", "nectarine", "Butternut squash"};
var workbook = WorkBook.Create(ExcelFileFormat.XLSX);
DataTable table = new DataTable("Fruits");
foreach (string fruitName in fruits)
{
table.Columns.Add(fruitName);
}
int rowCount = 20000;
for (int i = 0; i < rowCount; i++)
{
table.Rows.Add(fruits);
}
workbook.LoadWorkSheet(table);
workbook.SaveAs("C:\\Temp\\LoadDataTableDirectly.xlsx");Dim fruits As String() = {"Apples", "Apricot", "Banana", "Blackberry", "Blueberry", "Boysenberry", "Canary Melon", "Cantaloupe", "Currants", "Dates (tree dried only) Dragon Fruit", "Durian (purchase cut) Figs", "Gooseberry", "Grapes", "Grapefruit", "Lemon", "Lime", "Loganberry", "Longan", "Loquat", "Lychee", "Mandarin", "Mango", "Blood Orange", "Papaya", "Passion Fruit", "Peach", "Pear", "Persimmon", "Pineapple", "Plum", "Honeydew", "Rambutan", "Starfruit", "Tamarind", "Yuzu", "Açaí", "Abiu", "Ackee", "Breadfruit", "Cempedak", "Cherimoya", "Buddha's Hand", "Citron", "Finger Lime", "Kumquat", "Pomelo", "Tangelo", "Ugli Fruit", "Galia Melon", "Kiwano (Horned Melon)", "Mouse Melon", "Muskmelon", "cherry", "nectarine", "Butternut squash"}
Dim workbook = WorkBook.Create(ExcelFileFormat.XLSX)
Dim table As New DataTable("Fruits")
For Each fruitName As String In fruits
table.Columns.Add(fruitName)
Next
Dim rowCount As Integer = 20000
For i As Integer = 0 To rowCount - 1
table.Rows.Add(fruits)
Next
workbook.LoadWorkSheet(table)
workbook.SaveAs("C:\Temp\LoadDataTableDirectly.xlsx")Dim fruits As String() = {"Apples", "Apricot", "Banana", "Blackberry", "Blueberry", "Boysenberry", "Canary Melon", "Cantaloupe", "Currants", "Dates (tree dried only) Dragon Fruit", "Durian (purchase cut) Figs", "Gooseberry", "Grapes", "Grapefruit", "Lemon", "Lime", "Loganberry", "Longan", "Loquat", "Lychee", "Mandarin", "Mango", "Blood Orange", "Papaya", "Passion Fruit", "Peach", "Pear", "Persimmon", "Pineapple", "Plum", "Honeydew", "Rambutan", "Starfruit", "Tamarind", "Yuzu", "Açaí", "Abiu", "Ackee", "Breadfruit", "Cempedak", "Cherimoya", "Buddha's Hand", "Citron", "Finger Lime", "Kumquat", "Pomelo", "Tangelo", "Ugli Fruit", "Galia Melon", "Kiwano (Horned Melon)", "Mouse Melon", "Muskmelon", "cherry", "nectarine", "Butternut squash"}
Dim workbook = WorkBook.Create(ExcelFileFormat.XLSX)
Dim table As New DataTable("Fruits")
For Each fruitName As String In fruits
table.Columns.Add(fruitName)
Next
Dim rowCount As Integer = 20000
For i As Integer = 0 To rowCount - 1
table.Rows.Add(fruits)
Next
workbook.LoadWorkSheet(table)
workbook.SaveAs("C:\Temp\LoadDataTableDirectly.xlsx")LoadWorkSheet(table) escreve a tabela inteira de uma vez, em vez de tocar células individualmente, o que é de onde vem o aumento de velocidade.

Opção 3: Carregamento a partir de CSV
Escreva os dados em um arquivo .csv delimitado, então leia-o de volta com LoadCSV. Esta é uma das duas opções mais rápidas e a escolha natural quando seus dados já estão planos e delimitados.
string[] fruits = { "Apples", "Apricot", "Banana", "Blackberry", "Blueberry", "Boysenberry", "Canary Melon", "Cantaloupe",
"Currants", "Dates (tree dried only) Dragon Fruit", "Durian (purchase cut) Figs", "Gooseberry", "Grapes", "Grapefruit",
"Lemon", "Lime", "Loganberry", "Longan", "Loquat", "Lychee", "Mandarin", "Mango",
"Blood Orange", "Papaya", "Passion Fruit", "Peach", "Pear", "Persimmon", "Pineapple", "Plum", "Honeydew",
"Rambutan", "Starfruit", "Tamarind", "Yuzu", "Açaí", "Abiu", "Ackee", "Breadfruit", "Cempedak",
"Cherimoya", "Buddha's Hand", "Citron", "Finger Lime", "Kumquat", "Pomelo", "Tangelo", "Ugli Fruit",
"Galia Melon", "Kiwano (Horned Melon)", "Mouse Melon", "Muskmelon",
"cherry", "nectarine", "Butternut squash"};
var sb = new StringBuilder();
// Step 1: Add header row
sb.AppendLine(string.Join(",", fruits));
// Step 2: Add 20,000 identical rows
string rowData = string.Join(",", fruits); // same as headers
for (int i = 1; i < 20000; i++)
{
sb.AppendLine(rowData);
}
File.WriteAllText("C:\\Temp\\csvfile.csv", sb.ToString(), Encoding.UTF8);
var workbook = WorkBook.LoadCSV("C:\\Temp\\csvfile.csv");
workbook.SaveAs("C:\\Temp\\LoadFromCSV.xlsx");string[] fruits = { "Apples", "Apricot", "Banana", "Blackberry", "Blueberry", "Boysenberry", "Canary Melon", "Cantaloupe",
"Currants", "Dates (tree dried only) Dragon Fruit", "Durian (purchase cut) Figs", "Gooseberry", "Grapes", "Grapefruit",
"Lemon", "Lime", "Loganberry", "Longan", "Loquat", "Lychee", "Mandarin", "Mango",
"Blood Orange", "Papaya", "Passion Fruit", "Peach", "Pear", "Persimmon", "Pineapple", "Plum", "Honeydew",
"Rambutan", "Starfruit", "Tamarind", "Yuzu", "Açaí", "Abiu", "Ackee", "Breadfruit", "Cempedak",
"Cherimoya", "Buddha's Hand", "Citron", "Finger Lime", "Kumquat", "Pomelo", "Tangelo", "Ugli Fruit",
"Galia Melon", "Kiwano (Horned Melon)", "Mouse Melon", "Muskmelon",
"cherry", "nectarine", "Butternut squash"};
var sb = new StringBuilder();
// Step 1: Add header row
sb.AppendLine(string.Join(",", fruits));
// Step 2: Add 20,000 identical rows
string rowData = string.Join(",", fruits); // same as headers
for (int i = 1; i < 20000; i++)
{
sb.AppendLine(rowData);
}
File.WriteAllText("C:\\Temp\\csvfile.csv", sb.ToString(), Encoding.UTF8);
var workbook = WorkBook.LoadCSV("C:\\Temp\\csvfile.csv");
workbook.SaveAs("C:\\Temp\\LoadFromCSV.xlsx");Dim fruits As String() = {"Apples", "Apricot", "Banana", "Blackberry", "Blueberry", "Boysenberry", "Canary Melon", "Cantaloupe", "Currants", "Dates (tree dried only) Dragon Fruit", "Durian (purchase cut) Figs", "Gooseberry", "Grapes", "Grapefruit", "Lemon", "Lime", "Loganberry", "Longan", "Loquat", "Lychee", "Mandarin", "Mango", "Blood Orange", "Papaya", "Passion Fruit", "Peach", "Pear", "Persimmon", "Pineapple", "Plum", "Honeydew", "Rambutan", "Starfruit", "Tamarind", "Yuzu", "Açaí", "Abiu", "Ackee", "Breadfruit", "Cempedak", "Cher...
Dim sb = New StringBuilder()
' Step 1: Add header row
sb.AppendLine(String.Join(",", fruits))
' Step 2: Add 20,000 identical rows
Dim rowData As String = String.Join(",", fruits) ' same as headers
For i As Integer = 1 To 19999
sb.AppendLine(rowData)
Next
File.WriteAllText("C:\Temp\csvfile.csv", sb.ToString(), Encoding.UTF8)
Dim workbook = WorkBook.LoadCSV("C:\Temp\csvfile.csv")
workbook.SaveAs("C:\Temp\LoadFromCSV.xlsx")Dim fruits As String() = {"Apples", "Apricot", "Banana", "Blackberry", "Blueberry", "Boysenberry", "Canary Melon", "Cantaloupe", "Currants", "Dates (tree dried only) Dragon Fruit", "Durian (purchase cut) Figs", "Gooseberry", "Grapes", "Grapefruit", "Lemon", "Lime", "Loganberry", "Longan", "Loquat", "Lychee", "Mandarin", "Mango", "Blood Orange", "Papaya", "Passion Fruit", "Peach", "Pear", "Persimmon", "Pineapple", "Plum", "Honeydew", "Rambutan", "Starfruit", "Tamarind", "Yuzu", "Açaí", "Abiu", "Ackee", "Breadfruit", "Cempedak", "Cher...
Dim sb = New StringBuilder()
' Step 1: Add header row
sb.AppendLine(String.Join(",", fruits))
' Step 2: Add 20,000 identical rows
Dim rowData As String = String.Join(",", fruits) ' same as headers
For i As Integer = 1 To 19999
sb.AppendLine(rowData)
Next
File.WriteAllText("C:\Temp\csvfile.csv", sb.ToString(), Encoding.UTF8)
Dim workbook = WorkBook.LoadCSV("C:\Temp\csvfile.csv")
workbook.SaveAs("C:\Temp\LoadFromCSV.xlsx")LoadCSV analisa o arquivo delimitado em massa, então o custo está em escrever e ler o .csv intermediário, em vez de atribuição por célula.

Qual escolher
- Para dados já no disco: leia-o com CSV quando a fonte for um arquivo plano e delimitado.
- Para dados já na memória: carregue um
DataTable; a partir dev2026.10.1é a opção mais rápida ou a opção mais rápida em conjunto. - Para lógica ou estilo por célula: opte pela escrita célula por célula apenas então, uma vez que é o caminho mais lento.
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 bem estruturados e visualmente atraentes.