Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Excel中使用C#更改链接时出现异常_C#_Excel_Excel Interop - Fatal编程技术网

在Excel中使用C#更改链接时出现异常

在Excel中使用C#更改链接时出现异常,c#,excel,excel-interop,C#,Excel,Excel Interop,作为文件存储迁移项目的一部分,我正在尝试更改某些excel工作簿中的某些excel链接,以反映新的文件存储位置 我正在VS2017 RC中使用Winforms和C#开发我打算部署的解决方案 在我的解决方案中;我正在调用Excel工作簿对象上的ChangeLink方法,并传入旧链接、新链接和Excel链接类型 public string ProcessFile(string fileName) { // Private member variable and

作为文件存储迁移项目的一部分,我正在尝试更改某些excel工作簿中的某些excel链接,以反映新的文件存储位置

我正在VS2017 RC中使用Winforms和C#开发我打算部署的解决方案

在我的解决方案中;我正在调用Excel工作簿对象上的ChangeLink方法,并传入旧链接、新链接和Excel链接类型

        public string ProcessFile(string fileName)
    {
        // Private member variable and placeholder declarations
        missingValue = Type.Missing;

        string oldLink;
        string newLink;
        int splitLocation;
        string stringToFind = "\\Finance";


        //Open the specified Excel Workbook
        Excel.Workbook excelWorkbook;

        StringBuilder resultsOut = new StringBuilder();

        if (MsOfficeHelper.IsPasswordProtected(fileName))
        {
            resultsOut = resultsOut.AppendLine("Password Protected - " + fileName);
        }
        else
        {
            // Open
            excelWorkbook = excelApp.Workbooks.Open(Filename: fileName, UpdateLinks: false);

            Array olinks = excelWorkbook.LinkSources(Excel.XlLink.xlExcelLinks) as Array;
            if (olinks != null)
            {
                if (olinks.Length > 0)
                {
                    resultsOut = resultsOut.AppendLine("Contains Links - " + fileName);
                    foreach (var olink in olinks)
                    {
                        oldLink = olink.ToString();
                        splitLocation = oldLink.IndexOf(stringToFind, 0);

                        newLink = "C:\\SteveTest\\" + oldLink.Substring(splitLocation + 1);

                        resultsOut = resultsOut.AppendLine(oldLink);
                        resultsOut = resultsOut.AppendLine(newLink);

                        try
                        {
                            excelWorkbook.ChangeLink(Name: oldLink, NewName: newLink, Type: Excel.XlLinkType.xlLinkTypeExcelLinks);
                        }
                        catch (Exception whoopsy)
                        {
                            MessageBox.Show(whoopsy.Message);
                            //throw;
                        }
                    }
                }
            }

            excelWorkbook.Close(SaveChanges: false);

        }
        return resultsOut.ToString();


    }
然而,当我执行ChangeLink方法时,我得到以下异常

有人知道是什么导致了异常吗?
您经过深思熟虑的回答将非常受欢迎。

这意味着找不到名称-因此,您可能需要检查一些没有出错的地方?新链接是否指向实际文件?它的格式正确吗?@Madenis,是的,新链接确实指向一个实际的文件。它应该存在。@BugFinder-我将仔细检查的有效点。@BugFinder我仔细检查了一下,有些地方确实出了问题。它的意思应该是找不到名称-所以,也许你需要检查一些没有出错的地方?新链接是否指向实际的文件?它的格式正确吗?@Madenis,是的,新链接确实指向一个实际的文件。它应该存在。@BugFinder-我将仔细检查的一个有效点。@BugFinder我仔细检查了一下,确实出了问题。