Skip to content

935073-FAQ to show how to override an Excel document using C# #1113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: hotfix/hotfix-v29.1.33
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Document-Processing-toc.html
Original file line number Diff line number Diff line change
Expand Up @@ -2517,6 +2517,9 @@
<li>
<a href="/document-processing/excel/excel-library/net/faqs/how-to-show-the-leader-line-on-Excel-chart">How to show the leader line on Excel chart?</a>
</li>
<li>
<a href="/document-processing/excel/excel-library/net/faqs/how-to-override-an-Excel-document-using-C#">How to override an Excel document using C#?</a>
</li>
</ul>
</li>
</ul>
Expand Down
3 changes: 2 additions & 1 deletion Document-Processing/Excel/Excel-Library/NET/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,5 @@ The frequently asked questions in Essential<sup>&reg;</sup> XlsIO are listed bel
* [How to get the list of worksheet names in an Excel workbook?](faqs/how-to-get-the-list-of-worksheet-names-in-an-Excel-workbook)
* [How to switch chart series data interpretation from horizontal (rows) to vertical (columns) in Excel?](faqs/how-to-switch-chart-series-data-interpretation-from-horizontal-(rows)-to-vertical-(columns)-in-Excel)
* [How to add Oval shape to Excel chart using XlsIO?](faqs/how-to-add-oval-shape-to-Excel-chart)
* [How to show the leader line on Excel chart?](faqs/how-to-show-the-leader-line-on-Excel-chart)
* [How to show the leader line on Excel chart?](faqs/how-to-show-the-leader-line-on-Excel-chart)
* [How to override an Excel document using C#?](faqs/how-to-override-an-Excel-document-using-C#)
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
title: How to override an Excel document using C# | Syncfusion
description: Code example to override an existing Excel document using Syncfusion .NET Excel library (XlsIO).
platform: document-processing
control: XlsIO
documentation: UG
---

# How to override an Excel document using C#?

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.

{% tabs %}
{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/XlsIO-Examples/master/FAQ/Override%20Excel%20Document/.NET/Override%20Excel%20Document/Override%20Excel%20Document/Program.cs,180" %}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;

//Open an existing Excel file as stream
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Sample.xlsx"), FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(inputStream);
IWorksheet worksheet = workbook.Worksheets[0];

//Modify the data
worksheet.Range["A1"].Text = "Hello World";

//Dispose input stream
inputStream.Dispose();

#region Save
//Saving the workbook
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Sample.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion

//Dispose output stream
outputStream.Dispose();
}
{% endhighlight %}

{% highlight c# tabtitle="C# [Windows-specific]" %}
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");
}
{% endhighlight %}

{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
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 Using
{% endhighlight %}
{% endtabs %}

A complete working example to override an Excel document in C# is present on [this GitHub page](https://github.com/SyncfusionExamples/XlsIO-Examples/tree/master/FAQ/Override%20Excel%20Document/.NET/Override%20Excel%20Document).