How to edit external workbook reference link?

25 May 20232 minutes to read

Existing external workbook reference link can be modified through URL property of ExternWorkbookImpl class. Please find the code snippet below.

string DataPathBase = System.Environment.CurrentDirectory + @"\";
using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Xlsx;
  FileStream inputStream = new FileStream("Sample.xlsx", FileMode.Open, FileAccess.Read);
  IWorkbook workbook = application.Workbooks.Open(inputStream);
  string filepath = (workbook as WorkbookImpl).ExternWorkbooks[0].URL;

  (workbook as WorkbookImpl).ExternWorkbooks[0].URL = DataPathBase + "Template.xlsx";

  FileStream outputStream = new FileStream("Output.xlsx", FileMode.Create, FileAccess.Write);
  workbook.SaveAs(outputStream);
}
string DataPathBase = System.Environment.CurrentDirectory + @"\";
using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Xlsx;
  IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
  string filepath = (workbook as WorkbookImpl).ExternWorkbooks[0].URL;

  (workbook as WorkbookImpl).ExternWorkbooks[0].URL = DataPathBase + "Template.xlsx";

  workbook.SaveAs("Output.xlsx");
}
Dim DataPathBase As String = (System.Environment.CurrentDirectory + "\")
Using excelEngine As ExcelEngine = New ExcelEngine
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Xlsx
  Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx")
  Dim filepath As String = CType(workbook, WorkbookImpl).ExternWorkbooks(0).URL
  CType(workbook, WorkbookImpl).ExternWorkbooks(0).URL = (DataPathBase + "Template.xlsx")
  workbook.SaveAs("Output.xlsx")
End Using