Follow these steps to generate a simple Excel Report from ASP.NET Code Behind using SpreadsheetGear 2023
Simple ASP.NET Excel Report using SpreadsheetGear 2023 |
Copy Code
|
---|---|
void Page_Load(Object sender, EventArgs e) { // Create a new workbook. SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook(); SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets["Sheet1"]; SpreadsheetGear.IRange cells = worksheet.Cells; // Set the worksheet name. worksheet.Name = "2017 Sales"; // Load column titles and center. cells["B1"].Formula = "North"; cells["C1"].Formula = "South"; cells["D1"].Formula = "East"; cells["E1"].Formula = "West"; cells["B1:E1"].HorizontalAlignment = SpreadsheetGear.HAlign.Center; // Load row titles using multiple cell text reference and iteration. int quarter = 1; foreach (SpreadsheetGear.IRange cell in cells["A2:A5"]) cell.Formula = "Q" + quarter++; // Load random data and format as $ using a multiple cell range. SpreadsheetGear.IRange body = cells[1, 1, 4, 4]; body.Formula = "=RAND() * 10000"; body.NumberFormat = "$#,##0_);($#,##0)"; // Stream the Excel workbook to the client in a format // compatible with Excel 2010, Excel 2013, Excel 2016, Excel 2019 and Excel for Office 365. Response.Clear(); Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("Content-Disposition", "attachment; filename=report.xls"); workbook.SaveToStream(Response.OutputStream, SpreadsheetGear.FileFormat.Excel8); Response.End(); } |