// Initialize random number generator for sample dataRandom r = new Random();// Populate cells with random budget data for each monthfor (int i = 2; i <= 11; i++){ // Set different budget categories with increasing ranges workSheet[$"A{i}"].Value = r.Next(1, 1000); // Office Supplies workSheet[$"B{i}"].Value = r.Next(1000, 2000); // Utilities workSheet[$"C{i}"].Value = r.Next(2000, 3000); // Rent workSheet[$"D{i}"].Value = r.Next(3000, 4000); // Salaries workSheet[$"E{i}"].Value = r.Next(4000, 5000); // Marketing workSheet[$"F{i}"].Value = r.Next(5000, 6000); // IT Services workSheet[$"G{i}"].Value = r.Next(6000, 7000); // Travel workSheet[$"H{i}"].Value = r.Next(7000, 8000); // Training workSheet[$"I{i}"].Value = r.Next(8000, 9000); // Insurance workSheet[$"J{i}"].Value = r.Next(9000, 10000); // Equipment workSheet[$"K{i}"].Value = r.Next(10000, 11000); // Research workSheet[$"L{i}"].Value = r.Next(11000, 12000); // Misc}// Alternative: Set range of cells with same valueworkSheet["A13:L13"].Value = 0; // Initialize totals row
// Initialize random number generator for sample data
Random r = new Random();
// Populate cells with random budget data for each month
for (int i = 2; i <= 11; i++)
{
// Set different budget categories with increasing ranges
workSheet[$"A{i}"].Value = r.Next(1, 1000); // Office Supplies
workSheet[$"B{i}"].Value = r.Next(1000, 2000); // Utilities
workSheet[$"C{i}"].Value = r.Next(2000, 3000); // Rent
workSheet[$"D{i}"].Value = r.Next(3000, 4000); // Salaries
workSheet[$"E{i}"].Value = r.Next(4000, 5000); // Marketing
workSheet[$"F{i}"].Value = r.Next(5000, 6000); // IT Services
workSheet[$"G{i}"].Value = r.Next(6000, 7000); // Travel
workSheet[$"H{i}"].Value = r.Next(7000, 8000); // Training
workSheet[$"I{i}"].Value = r.Next(8000, 9000); // Insurance
workSheet[$"J{i}"].Value = r.Next(9000, 10000); // Equipment
workSheet[$"K{i}"].Value = r.Next(10000, 11000); // Research
workSheet[$"L{i}"].Value = r.Next(11000, 12000); // Misc
}
// Alternative: Set range of cells with same value
workSheet["A13:L13"].Value = 0; // Initialize totals row
' Initialize random number generator for sample dataDim r As New Random()' Populate cells with random budget data for each monthFor i AsInteger = 2 To 11 ' Set different budget categories with increasing ranges workSheet($"A{i}").Value = r.Next(1, 1000) ' Office Supplies workSheet($"B{i}").Value = r.Next(1000, 2000) ' Utilities workSheet($"C{i}").Value = r.Next(2000, 3000) ' Rent workSheet($"D{i}").Value = r.Next(3000, 4000) ' Salaries workSheet($"E{i}").Value = r.Next(4000, 5000) ' Marketing workSheet($"F{i}").Value = r.Next(5000, 6000) ' IT Services workSheet($"G{i}").Value = r.Next(6000, 7000) ' Travel workSheet($"H{i}").Value = r.Next(7000, 8000) ' Training workSheet($"I{i}").Value = r.Next(8000, 9000) ' Insurance workSheet($"J{i}").Value = r.Next(9000, 10000) ' Equipment workSheet($"K{i}").Value = r.Next(10000, 11000) ' Research workSheet($"L{i}").Value = r.Next(11000, 12000) ' MiscNext i' Alternative: Set range of cells with same valueworkSheet("A13:L13").Value = 0 ' Initialize totals row
' Initialize random number generator for sample data
Dim r As New Random()
' Populate cells with random budget data for each month
For i As Integer = 2 To 11
' Set different budget categories with increasing ranges
workSheet($"A{i}").Value = r.Next(1, 1000) ' Office Supplies
workSheet($"B{i}").Value = r.Next(1000, 2000) ' Utilities
workSheet($"C{i}").Value = r.Next(2000, 3000) ' Rent
workSheet($"D{i}").Value = r.Next(3000, 4000) ' Salaries
workSheet($"E{i}").Value = r.Next(4000, 5000) ' Marketing
workSheet($"F{i}").Value = r.Next(5000, 6000) ' IT Services
workSheet($"G{i}").Value = r.Next(6000, 7000) ' Travel
workSheet($"H{i}").Value = r.Next(7000, 8000) ' Training
workSheet($"I{i}").Value = r.Next(8000, 9000) ' Insurance
workSheet($"J{i}").Value = r.Next(9000, 10000) ' Equipment
workSheet($"K{i}").Value = r.Next(10000, 11000) ' Research
workSheet($"L{i}").Value = r.Next(11000, 12000) ' Misc
Next i
' Alternative: Set range of cells with same value
workSheet("A13:L13").Value = 0 ' Initialize totals row
// Set header row background to light gray using hex colorworkSheet["A1:L1"].Style.SetBackgroundColor("#d3d3d3");// Apply different colors for data categorizationworkSheet["A2:A11"].Style.SetBackgroundColor("#E7F3FF"); // Light blue for JanuaryworkSheet["B2:B11"].Style.SetBackgroundColor("#FFF2CC"); // Light yellow for February// Highlight important cells with bold colorsworkSheet["L12"].Style.SetBackgroundColor("#FF0000"); // Red for totalsworkSheet["L12"].Style.Font.SetColor("#FFFFFF"); // White text// Create alternating row colors for better readabilityfor (int row = 2; row <= 11; row++){ if (row % 2 == 0) { workSheet[$"A{row}:L{row}"].Style.SetBackgroundColor("#F2F2F2"); }}
// Set header row background to light gray using hex color
workSheet["A1:L1"].Style.SetBackgroundColor("#d3d3d3");
// Apply different colors for data categorization
workSheet["A2:A11"].Style.SetBackgroundColor("#E7F3FF"); // Light blue for January
workSheet["B2:B11"].Style.SetBackgroundColor("#FFF2CC"); // Light yellow for February
// Highlight important cells with bold colors
workSheet["L12"].Style.SetBackgroundColor("#FF0000"); // Red for totals
workSheet["L12"].Style.Font.SetColor("#FFFFFF"); // White text
// Create alternating row colors for better readability
for (int row = 2; row <= 11; row++)
{
if (row % 2 == 0)
{
workSheet[$"A{row}:L{row}"].Style.SetBackgroundColor("#F2F2F2");
}
}
// Use IronXL built-in aggregationsdecimal sum = workSheet["A2:A11"].Sum();decimal avg = workSheet["B2:B11"].Avg();decimal max = workSheet["C2:C11"].Max();decimal min = workSheet["D2:D11"].Min();// Assign value to cellsworkSheet["A12"].Value = sum;workSheet["B12"].Value = avg;workSheet["C12"].Value = max;workSheet["D12"].Value = min;
// Use IronXL built-in aggregations
decimal sum = workSheet["A2:A11"].Sum();
decimal avg = workSheet["B2:B11"].Avg();
decimal max = workSheet["C2:C11"].Max();
decimal min = workSheet["D2:D11"].Min();
// Assign value to cells
workSheet["A12"].Value = sum;
workSheet["B12"].Value = avg;
workSheet["C12"].Value = max;
workSheet["D12"].Value = min;
' Use IronXL built-in aggregationsDim sum AsDecimal = workSheet("A2:A11").Sum()Dim avg AsDecimal = workSheet("B2:B11").Avg()Dim max AsDecimal = workSheet("C2:C11").Max()Dim min AsDecimal = workSheet("D2:D11").Min()' Assign value to cellsworkSheet("A12").Value = sumworkSheet("B12").Value = avgworkSheet("C12").Value = maxworkSheet("D12").Value = min
' Use IronXL built-in aggregations
Dim sum As Decimal = workSheet("A2:A11").Sum()
Dim avg As Decimal = workSheet("B2:B11").Avg()
Dim max As Decimal = workSheet("C2:C11").Max()
Dim min As Decimal = workSheet("D2:D11").Min()
' Assign value to cells
workSheet("A12").Value = sum
workSheet("B12").Value = avg
workSheet("C12").Value = max
workSheet("D12").Value = min
// Save as XLSX (recommended for modern Excel)workBook.SaveAs("Budget.xlsx");// Save as XLS for legacy compatibilityworkBook.SaveAs("Budget.xls");// Save as CSV for data exchangeworkBook.SaveAsCsv("Budget.csv");// Save as JSON for web applicationsworkBook.SaveAsJson("Budget.json");// Save to stream for web downloads or cloud storageusing (var stream = new MemoryStream()){ workBook.SaveAs(stream); byte[] excelData = stream.ToArray(); // Send to client or save to cloud}// Save with specific encoding for international charactersworkBook.SaveAsCsv("Budget_UTF8.csv", System.Text.Encoding.UTF8);
// Save as XLSX (recommended for modern Excel)
workBook.SaveAs("Budget.xlsx");
// Save as XLS for legacy compatibility
workBook.SaveAs("Budget.xls");
// Save as CSV for data exchange
workBook.SaveAsCsv("Budget.csv");
// Save as JSON for web applications
workBook.SaveAsJson("Budget.json");
// Save to stream for web downloads or cloud storage
using (var stream = new MemoryStream())
{
workBook.SaveAs(stream);
byte[] excelData = stream.ToArray();
// Send to client or save to cloud
}
// Save with specific encoding for international characters
workBook.SaveAsCsv("Budget_UTF8.csv", System.Text.Encoding.UTF8);
' Save as XLSX (recommended for modern Excel)workBook.SaveAs("Budget.xlsx")' Save as XLS for legacy compatibilityworkBook.SaveAs("Budget.xls")' Save as CSV for data exchangeworkBook.SaveAsCsv("Budget.csv")' Save as JSON for web applicationsworkBook.SaveAsJson("Budget.json")' Save to stream for web downloads or cloud storageUsing stream = New MemoryStream() workBook.SaveAs(stream) Dim excelData() AsByte = stream.ToArray() ' Send to client or save to cloudEndUsing' Save with specific encoding for international charactersworkBook.SaveAsCsv("Budget_UTF8.csv", System.Text.Encoding.UTF8)
' Save as XLSX (recommended for modern Excel)
workBook.SaveAs("Budget.xlsx")
' Save as XLS for legacy compatibility
workBook.SaveAs("Budget.xls")
' Save as CSV for data exchange
workBook.SaveAsCsv("Budget.csv")
' Save as JSON for web applications
workBook.SaveAsJson("Budget.json")
' Save to stream for web downloads or cloud storage
Using stream = New MemoryStream()
workBook.SaveAs(stream)
Dim excelData() As Byte = stream.ToArray()
' Send to client or save to cloud
End Using
' Save with specific encoding for international characters
workBook.SaveAsCsv("Budget_UTF8.csv", System.Text.Encoding.UTF8)
// CreateExcelBenchmark/Program.cs (excerpt)IronXL.License.LicenseKey = Environment.GetEnvironmentVariable("IRONXL_LICENSE_KEY");var sw = Stopwatch.StartNew();var workbook = WorkBook.Create(ExcelFileFormat.XLSX);var sheet = workbook.CreateWorkSheet("Data");sheet["A1"].Value = "Id";sheet["B1"].Value = "Name";sheet["C1"].Value = "Amount";for (int i = 0; i < 10_000; i++){ int row = i + 2; sheet[$"A{row}"].Value = i + 1; sheet[$"B{row}"].Value = $"Item {i + 1}"; sheet[$"C{row}"].Value = (i + 1) * 1.25m;}workbook.SaveAs("Generated.xlsx");sw.Stop();Console.WriteLine($"cold: {sw.Elapsed.TotalMilliseconds:F1} ms");
// CreateExcelBenchmark/Program.cs (excerpt)
IronXL.License.LicenseKey = Environment.GetEnvironmentVariable("IRONXL_LICENSE_KEY");
var sw = Stopwatch.StartNew();
var workbook = WorkBook.Create(ExcelFileFormat.XLSX);
var sheet = workbook.CreateWorkSheet("Data");
sheet["A1"].Value = "Id";
sheet["B1"].Value = "Name";
sheet["C1"].Value = "Amount";
for (int i = 0; i < 10_000; i++)
{
int row = i + 2;
sheet[$"A{row}"].Value = i + 1;
sheet[$"B{row}"].Value = $"Item {i + 1}";
sheet[$"C{row}"].Value = (i + 1) * 1.25m;
}
workbook.SaveAs("Generated.xlsx");
sw.Stop();
Console.WriteLine($"cold: {sw.Elapsed.TotalMilliseconds:F1} ms");
ImportsSystemImportsSystem.DiagnosticsImportsIronXLModuleProgram Sub Main()License.LicenseKey = Environment.GetEnvironmentVariable("IRONXL_LICENSE_KEY") Dim sw AsStopwatch = Stopwatch.StartNew() Dim workbook AsWorkBook = WorkBook.Create(ExcelFileFormat.XLSX) Dim sheet AsWorkSheet = workbook.CreateWorkSheet("Data") sheet("A1").Value = "Id" sheet("B1").Value = "Name" sheet("C1").Value = "Amount" For i AsInteger = 0 To 9999 Dim row AsInteger = i + 2 sheet($"A{row}").Value = i + 1 sheet($"B{row}").Value = $"Item {i + 1}" sheet($"C{row}").Value = (i + 1) * 1.25D Next workbook.SaveAs("Generated.xlsx") sw.Stop()Console.WriteLine($"cold: {sw.Elapsed.TotalMilliseconds:F1} ms") End SubEndModule
Imports System
Imports System.Diagnostics
Imports IronXL
Module Program
Sub Main()
License.LicenseKey = Environment.GetEnvironmentVariable("IRONXL_LICENSE_KEY")
Dim sw As Stopwatch = Stopwatch.StartNew()
Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
Dim sheet As WorkSheet = workbook.CreateWorkSheet("Data")
sheet("A1").Value = "Id"
sheet("B1").Value = "Name"
sheet("C1").Value = "Amount"
For i As Integer = 0 To 9999
Dim row As Integer = i + 2
sheet($"A{row}").Value = i + 1
sheet($"B{row}").Value = $"Item {i + 1}"
sheet($"C{row}").Value = (i + 1) * 1.25D
Next
workbook.SaveAs("Generated.xlsx")
sw.Stop()
Console.WriteLine($"cold: {sw.Elapsed.TotalMilliseconds:F1} ms")
End Sub
End Module
在dotnet run -c Release下運行它,並插入您自己的行數、列數或樣式,以查看工作簿複雜性如何影響資料。