如何使用 RODBC 或 RCurl 从 R 中受密码保护的 Sharepoint 2007 站点打开 Excel 2007 文件?

发布于 2024-10-16 18:17:17 字数 1974 浏览 10 评论 0原文

我有兴趣使用 RODBCR 2.11.1 中打开 Excel 2007 文件。 Excel 文件位于 MOSS2007 网站的共享文档页面中。我当前将 .xlsx 文件下载到我的硬盘,然后使用以下代码导入到 R

library(RODBC)
con<-odbcConnectExcel2007("C:/file location/file.xlsx")
data<-sqlFetch(con, "worksheet name")
close(con)

当我在 中输入文档的 Web url 时odbcConnectExcel2007 连接时,会弹出一条错误消息:

ODBC Excel Driver Login Failed: Invalid internet Address.

后跟我的 R 控制台中的以下消息:

 ERROR: Could not SQLDriverConnect

您可以提供的任何见解将不胜感激。

谢谢!


**更新**

我尝试下载的网站受密码保护。我尝试了另一种方法,使用包 RCurl 中的方法“getUrl”:

x = getURL("http://website.com/file.xlsx", userpwd = "uname:pw" )

我收到的错误是:

curlPerform(curl = curl, .opts = opts, .encoding = .encoding) 中的错误: 字符串中嵌入 nul: 'PK\003\004\024\0\006\0\b\0\0\0!\0dA»ï\001\0\0O\n\0\0\023\0Ò\001 [Content_Types].xml ∨\001(\0\002\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\

我不知道这是什么意思。任何帮助将不胜感激。谢谢!

I am interested in opening an Excel 2007 file in R 2.11.1 using RODBC. The Excel file resides in the shared documents page of a MOSS2007 website. I currently download the .xlsx file to my hard drive and then import to R using the following code:

library(RODBC)
con<-odbcConnectExcel2007("C:/file location/file.xlsx")
data<-sqlFetch(con, "worksheet name")
close(con)

When I type in the web url for the document into the odbcConnectExcel2007 connection, an error message pops up with:

ODBC Excel Driver Login Failed: Invalid internet Address.

followed by the following message in my R console:

 ERROR: Could not SQLDriverConnect

Any insights you can provide would be greatly appreciated.

Thanks!


**UPDATE**

The site I am attempting to download from is password protected. I tried another method using the method 'getUrl' in the package RCurl:

x = getURL("http://website.com/file.xlsx", userpwd = "uname:pw")

The error that I receive is:

Error in curlPerform(curl = curl, .opts = opts, .encoding = .encoding) :
embedded nul in string: 'PK\003\004\024\0\006\0\b\0\0\0!\0dA»ï\001\0\0O\n\0\0\023\0Ò\001[Content_Types].xml ¢Î\001( \0\002\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\

I have no idea what this means. Any help would be appreciated. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你曾走过我的故事 2024-10-23 18:17:17

两种解决方案对我有用。

  1. 如果您不需要自动执行提取数据的脚本,您可以映射网络驱动器,指向要从中提取 Excel 文档的共享点文件夹。

  2. 如果您需要自动化脚本以每隔几分钟提取 Excel 文件,我建议您在请求中发送身份验证凭据,以自动将文件保存到本地驱动器。从那里您可以将其读入 R 中以进行进一步的数据整理。

library("httr")   
library("openxlsx")
 
user <- <USERNAME>
password <- <PASSWORD>

url <- "https://sharepoint.company/file_to_obtain.xlsx"

httr::GET(url,
 authenticate(user, password, type="ntlm"),
 write_disk("C:/tempfile.xlsx", overwrite = TRUE))

df <- openxlsx::read.xlsx("C:/tempfile.xlsx")

您可以通过单击共享点位置并在文件结尾(xlsx、xlsb、xls...)后删除“?Web=1”来获取文件的正确 URL。用户名和密码通常是 Windows 凭据。它有助于将它们存储在密钥管理器中(例如:

library("keyring")
keyring::key_set_with_value(service = "Windows", username = "Key", password = <PASSWORD>) 

然后通过在某些情况下进行身份验证

authenticate(user, kreyring::key_get("Windows", "Key"), type="ntlm")

则可能足以通过。

authenticate(":", ":", type="ntlm")

,如果只需要您的 Windows 凭据并且代码从您的计算机运行,

Two solutions worked for me.

  1. If you do not need to automate the script that pulls the data, you can map a network drive pointing to the sharepoint folder from which you want to extract the Excel document.

  2. If you need to automate a script to pull the Excel file every couple of minutes, I recommend sending your authentication credentials in a request that automatically saves the file to a local drive. From there you can read it into R for further data wrangling.

library("httr")   
library("openxlsx")
 
user <- <USERNAME>
password <- <PASSWORD>

url <- "https://sharepoint.company/file_to_obtain.xlsx"

httr::GET(url,
 authenticate(user, password, type="ntlm"),
 write_disk("C:/tempfile.xlsx", overwrite = TRUE))

df <- openxlsx::read.xlsx("C:/tempfile.xlsx")

You can obtain the correct URL to the file by clicking on the sharepoint location and removing "?Web=1" after the file ending (xlsx, xlsb, xls,...). USERNAME and PASSWORD are usually windows credentials. It helps storing them in a key manager (such as:

library("keyring")
keyring::key_set_with_value(service = "Windows", username = "Key", password = <PASSWORD>) 

and then authenticating via

authenticate(user, kreyring::key_get("Windows", "Key"), type="ntlm")

in some instances it may be sufficient to pass

authenticate(":", ":", type="ntlm")

if only your Windows credentials are required and the code is running from your machine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文