How to save the edited changes in the same Excel document?
15 Jul 20243 minutes to read
The Save method is only supported on the framework platform.
The following code example illustrates how to save the edited changes in the same Excel document.
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
//File path
string filepath = @"../../../Data/InputTemplate.xlsx";
//Load an Excel document
FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(fileStream);
IWorksheet worksheet = workbook.Worksheets[0];
//Set text in the cell
worksheet["B1"].Text = "Text";
//Close the input stream before writing
fileStream.Close();
//Save the workbook to the same file
fileStream = new FileStream(filepath, FileMode.Create, FileAccess.Write);
workbook.SaveAs(fileStream);
//Dispose the stream
fileStream.Dispose();
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
//File path
string filepath = @"../../Data/InputTemplate.xlsx";
//Load an Excel document
IWorkbook workbook = application.Workbooks.Open(filepath);
IWorksheet worksheet = workbook.Worksheets[0];
//Set text in the cell
worksheet["B1"].Text = "Text";
//Save the workbook to the same file
workbook.Save();
}
Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx
'File path
Dim filepath As String = "../../Data/InputTemplate.xlsx"
'Load an Excel document
Dim workbook As IWorkbook = application.Workbooks.Open(filepath)
Dim worksheet As IWorksheet = workbook.Worksheets(0)
'Set text in the cell
worksheet("B1").Text = "Text"
'Save the workbook to the same file
workbook.Save()
End Using