// 2. Define headers worksheet.Cells["A1"].Value = "Product"; worksheet.Cells["B1"].Value = "Units Sold"; worksheet.Cells["C1"].Value = "Unit Price"; worksheet.Cells["D1"].Value = "Total Revenue";
In the world of .NET development, the need to generate, manipulate, and render Excel workbooks is a ubiquitous requirement. From financial reporting systems to data import/export utilities, Excel integration is often the "final mile" of an application. While there are several libraries available on the market, has established itself as a premier choice for developers seeking high performance, a robust API, and a familiar object model.
// --- Formulas --- // Define a formula for a total at the bottom. worksheet.Cells["B4"].Formula = "=SUM(B2:B3)"; worksheet.Cells["A4"].Value = "Grand Total";
// Populate data rows. worksheet.Cells["A2"].Value = "Widget A"; worksheet.Cells["B2"].Value = 100; worksheet.Cells["C2"].Value = 15.50; spreadsheetgear example
: You can retrieve values from specific cells or ranges, such as double value = (double)worksheet.Cells["A1"].Value; .
workbook.SaveAs(@"C:\Temp\SalesChart.xlsx");
The most fundamental task is creating a new Excel file, populating it with data, and saving it to disk. This demonstrates the "Hello World" of workbook generation. While there are several libraries available on the
// Set values worksheet.Cells["A1"].Value = "Hello, SpreadsheetGear!"; worksheet.Cells["A2"].Formula = "=LEN(A1)"; // Result: 22
Suppose you need to email this report as a PDF. With SpreadsheetGear, you can add two lines:
worksheet.Cells["A2"].Value = "Server Maintenance"; worksheet.Cells["B2"].Value = 1200.50; worksheet
public void CreateBasicWorkbook(string filePath)
: It can perform complex calculations and data transfers significantly quicker than Excel.
string name = worksheet.Cells[row, 1].Text; double salary = worksheet.Cells[row, 2].Number; Console.WriteLine($"Employee: name, Salary: salary:C"); row++;