How can I help you?
How to override an Excel document using C#?
4 Nov 20252 minutes to read
You can override an existing Excel document by opening it, making necessary changes, and saving it using the Syncfusion XlsIO library.
The following code examples demonstrate how to do this in C# (Cross-platform and Windows-specific) and VB.NET.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
//Open an existing Excel file as stream
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Sample.xlsx"));
IWorksheet worksheet = workbook.Worksheets[0];
//Modify the data
worksheet.Range["A1"].Text = "Hello World";
#region Save
//Saving the workbook
workbook.SaveAs(Path.GetFullPath("Output/Sample.xlsx"));
#endregion
}using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
//Open an existing Excel file
IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
IWorksheet worksheet = workbook.Worksheets[0];
//Modify the data
worksheet.Range["A1"].Text = "Hello World";
workbook.SaveAs("Sample.xlsx");
}Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
'Open an existing Excel file
Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx")
Dim worksheet As IWorksheet = workbook.Worksheets(0)
'Modify the data
worksheet.Range("A1").Text = "Hello World"
workbook.SaveAs("Sample.xlsx")
End UsingA complete working example to override an Excel document in C# is present on this GitHub page.