excel vsto 跨表引用更改数据源

 private object[] ConvertArray(Array arr)
 {
     int lb = arr.GetLowerBound(0);
     var ret = new object[arr.GetUpperBound(0) - lb + 1];
     for (int ix = 0; ix < ret.Length; ++ix)
     {
         ret[ix] = arr.GetValue(ix + lb);
     }
     return ret;
 }


 private void App_WorkbookOpen(Excel.Workbook Wb)
 {
     var aa = Wb.LinkSources(Excel.XlLink.xlExcelLinks);
     object[] obj = ConvertArray(aa as Array);
     foreach (var link in obj )
     {
         // 在此处访问每个链接的属性和方法,例如:
       
         string newLinkPath = @"C:\Users\abc.xlsx";
         Wb.ChangeLink(link.ToString(), newLinkPath, Excel.XlLinkType.xlLinkTypeExcelLinks);
     }


       // Update the link with the new path
    if (Wb.UpdateLink(oldLinkPath, newLinkPath, Excel.XlLinkType.xlLinkTypeExcelLinks))
    {
        // Link update successful
    }
    else
    {
        // Link update failed
    }
}

你可能感兴趣的:(excel,vsto)