
Create & Modify Excel Workbooks
Document Solutions for Excel .NET makes it easy to create and modify Excel workbooks directly from .NET applications. Developers can create new workbooks, add worksheets, write values to cells, apply formulas, style ranges, and save the completed spreadsheet using C# or VB.NET. This is useful for building reports, invoices, statements, dashboards, templates, and other Excel-based documents that need to be generated dynamically.
With DsExcel .NET, developers can work with Excel workbooks using a familiar object model that includes workbooks, worksheets, ranges, cells, rows, columns, formulas, and styles. This makes it possible to automate common spreadsheet workflows such as populating templates, generating formatted reports, inserting calculated values, and preparing files for download or distribution.
Aspose.Cells for .NET also provides APIs for creating and modifying Excel workbooks programmatically. Developers can create new workbooks, access worksheets, update cells, apply formatting, and save the result in Excel-compatible formats. The code comparison below shows how both products handle a basic workbook generation workflow, including creating a workbook, writing worksheet content, applying formatting, and saving the final Excel file.
Aspose.Cells for .NET Code Example:
// NuGet: Install-Package Aspose.Cells
using Aspose.Cells;
using System;
class Program
{
static void Main()
{
// Create a new workbook.
var workbook = new Workbook();
// Access the first worksheet.
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Name = "Sales Report";
// Add worksheet content.
worksheet.Cells["A1"].PutValue("Quarterly Sales Report");
worksheet.Cells["A3"].PutValue("Quarter");
worksheet.Cells["B3"].PutValue("Revenue");
worksheet.Cells["A4"].PutValue("Q1");
worksheet.Cells["B4"].PutValue(125000);
worksheet.Cells["A5"].PutValue("Q2");
worksheet.Cells["B5"].PutValue(142500);
worksheet.Cells["A6"].PutValue("Q3");
worksheet.Cells["B6"].PutValue(158000);
worksheet.Cells["A7"].PutValue("Q4");
worksheet.Cells["B7"].PutValue(171250);
worksheet.Cells["A8"].PutValue("Total");
worksheet.Cells["B8"].Formula = "=SUM(B4:B7)";
// Style the title.
Style titleStyle = worksheet.Cells["A1"].GetStyle();
titleStyle.Font.IsBold = true;
titleStyle.Font.Size = 16;
worksheet.Cells["A1"].SetStyle(titleStyle);
// Style the header row.
Style headerStyle = workbook.CreateStyle();
headerStyle.Font.IsBold = true;
headerStyle.Pattern = BackgroundType.Solid;
headerStyle.ForegroundColor = System.Drawing.Color.LightGray;
worksheet.Cells.CreateRange("A3:B3").SetStyle(headerStyle);
// Format revenue cells.
Style currencyStyle = workbook.CreateStyle();
currencyStyle.Custom = "$#,##0";
worksheet.Cells.CreateRange("B4:B8").SetStyle(currencyStyle);
worksheet.AutoFitColumns();
// Save the Excel workbook.
workbook.Save("aspose-create-modify-workbook.xlsx", SaveFormat.Xlsx);
Console.WriteLine("Workbook created successfully.");
}
}
Document Solutions for Excel .NET Code Example:
// NuGet: Install-Package DS.Documents.Excel
using GrapeCity.Documents.Excel;
using System;
class Program
{
static void Main()
{
// Create a new workbook.
var workbook = new Workbook();
// Access the first worksheet.
IWorksheet worksheet = workbook.Worksheets[0];
worksheet.Name = "Sales Report";
// Add worksheet content.
worksheet.Range["A1"].Value = "Quarterly Sales Report";
worksheet.Range["A3"].Value = "Quarter";
worksheet.Range["B3"].Value = "Revenue";
worksheet.Range["A4"].Value = "Q1";
worksheet.Range["B4"].Value = 125000;
worksheet.Range["A5"].Value = "Q2";
worksheet.Range["B5"].Value = 142500;
worksheet.Range["A6"].Value = "Q3";
worksheet.Range["B6"].Value = 158000;
worksheet.Range["A7"].Value = "Q4";
worksheet.Range["B7"].Value = 171250;
worksheet.Range["A8"].Value = "Total";
worksheet.Range["B8"].Formula = "=SUM(B4:B7)";
// Style the title.
worksheet.Range["A1"].Font.Bold = true;
worksheet.Range["A1"].Font.Size = 16;
// Style the header row.
worksheet.Range["A3:B3"].Font.Bold = true;
worksheet.Range["A3:B3"].Interior.Color = System.Drawing.Color.LightGray;
// Format revenue cells.
worksheet.Range["B4:B8"].NumberFormat = "$#,##0";
worksheet.Range["A:B"].Columns.AutoFit();
// Save the Excel workbook.
workbook.Save("dsexcel-create-modify-workbook.xlsx", SaveFileFormat.Xlsx);
Console.WriteLine("Workbook created successfully.");
}
}