Spreadsheetgear Example – Must Read

// Create a new empty workbook IWorkbook workbook = Factory.GetWorkbook();

public void CreateStyledWorkbook(string filePath) { IWorkbook workbook = Factory.GetWorkbook(); IWorksheet worksheet = workbook.Worksheets[0]; worksheet.Name = "Styled Report"; // --- Data Entry --- worksheet.Cells["A1"].Value = "Item"; worksheet.Cells["B1"].Value = "Cost";

In this article, we will focus on the keyword that matters most when you are ready to start coding: . We will move from basic creation to advanced calculation and rendering, providing real, copy-paste-ready code snippets.

The most common use case is creating a report from a data source. This example shows how to initialize a workbook, name a sheet, and populate cells with data. spreadsheetgear example

// 1. Create a new workbook and get the active worksheet IWorkbook workbook = Factory.GetWorkbook(); IWorksheet worksheet = workbook.Worksheets["Sheet1"]; worksheet.Name = "Sales Report";

Run the following command in the Package Manager Console:

: Easily copy data from a DataTable or Entity Framework query directly into a worksheet. // Create a new empty workbook IWorkbook workbook = Factory

SpreadsheetGear shines in web apps. Below is a for an ASP.NET MVC controller that generates an Excel file and sends it to the browser.

Use .Value for raw data, .Text for formatted display.

// Read results double sum = sheet.Cells["B5"].Number; // 600 Console.WriteLine($"Sum: sum"); This example shows how to initialize a workbook,

public FileResult OnGet() // Open a template that already has your branding and styles SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook("template.xlsx"); SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets[0]; // Fill the template with dynamic data IRange dataRange = worksheet.Cells["B2:B5"]; dataRange.Formula = "=RAND() * 1000"; // Dynamic calculation example // Save to a memory stream for the browser System.IO.Stream stream = workbook.SaveToStream(SpreadsheetGear.FileFormat.OpenXMLWorkbook); stream.Seek(0, System.IO.SeekOrigin.Begin); return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Report.xlsx"); Use code with caution. Source: Live Razor Pages Sample . 3. High-Performance Calculations

// 4. Add sample data (normally from DB) worksheet.Cells["A2"].Value = "Widget A"; worksheet.Cells["B2"].Value = 150; worksheet.Cells["C2"].Value = 12.99;