SSIS脚本任务,用于从Sharepoint访问Excel文件

时间:2019-03-20 12:30:27

标签: c# excel sharepoint ssis remote-access

我有一段C#代码,可以让我在SharePoint中抓取一个Excel文件并放入本地文件夹中。

尽管我可以使用Windows登录名访问该文件,但是当我尝试运行该程序包时,却出现以下错误:

  

远程服务器返回错误:(403)禁止。

这是我正在使用的一段代码:

        //  downloads the file from SharePoint or a file system location to a local folder
        Dts.TaskResult = (int)ScriptResults.Success;

        try
        {
            //  obtain location of local folder from variable
            DirectoryInfo dir = new DirectoryInfo(Dts.Variables["User::ImportFolder"].Value.ToString());
            if (dir.Exists)
            {
                //  Create the filename for local storage using
                //  the GUID from SharePoint as this will be unique.
                FileInfo file = new FileInfo((dir.FullName + ("\\"
                                + (Dts.Variables["User::WorkbookGUID"].Value.ToString() + Dts.Variables["User::Extension"].Value.ToString()))));
                if (!file.Exists)
                {
                    //  get the path of the file we need to download
                    string fileUrl = Dts.Variables["User::EncodedAbsUrl"].Value.ToString();
                    if ((fileUrl.Length != 0))
                    {
                        //  download the file from SharePoint or Archive file system to local folder
                        WebClient client = new WebClient();
                        if ((fileUrl.Substring(0, 4).ToLower() == "http"))
                        {
                            // download the file from SharePoint
                            client.Credentials = System.Net.CredentialCache.DefaultCredentials;
                            client.DownloadFile(fileUrl, file.FullName);
                        }
                        else
                        {
                            //  copy file from remote file system
                            System.IO.File.Copy(fileUrl, file.FullName);
                        }

                    }
                    else
                    {
                        throw new ApplicationException("EncodedAbsUrl variable does not contain a value!");
                    }

                }

            }
            else
            {
                throw new ApplicationException("ImportFolder does not exist!");
            }

        }
        catch (Exception ex)
        {
            Dts.Events.FireError(0, String.Empty, ex.Message, String.Empty, 0);
            Dts.TaskResult = (int)ScriptResults.Failure;
        }

        Dts.TaskResult = (int)ScriptResults.Success;

0 个答案:

没有答案