在读/写模式下从 SharePoint 站点以 C# 编程方式打开 xls 电子表格
我编写了一个程序,可以从本地磁盘打开 xls,刷新其中的数据,然后再次保存。这很好用。
当我替换文件名以指向 SharePoint 网站时,会出现问题。它可以很好地打开文件。刷新文件,但当它尝试保存文件时,它会抛出异常,并显示消息“无法保存为该名称。文档以只读方式打开。”。 如果我尝试使用不同的文件名保存文件,那么它工作正常。
有人知道我错过了什么吗?我认为这一定与我打开文件的方式有关。还有另一种方法可以强制以读/写方式打开文件吗?
private static void RefreshExcelDocument(string filename)
{
var xls = new Microsoft.Office.Interop.Excel.Application();
xls.Visible = true;
xls.DisplayAlerts = false;
var workbook = xls.Workbooks.Open(Filename: filename, IgnoreReadOnlyRecommended: true, ReadOnly: false);
try
{
// Refresh the data from data connections
workbook.RefreshAll();
// Wait for the refresh occurs - *wish there was a better way than this.
System.Threading.Thread.Sleep(5000);
// Save the workbook back again
workbook.SaveAs(Filename: filename); // This is when the Exception is thrown
// Close the workbook
workbook.Close(SaveChanges: false);
}
catch (Exception ex)
{
//Exception message is "Cannot save as that name. Document was opened as read-only."
}
finally
{
xls.Application.Quit();
xls = null;
}
}
非常感谢您的建议。
乔纳森
I have written a procedure that will open a xls from a local disc, refresh the data in it and then save it again. This works fine.
The problem occurs when I replace the filename to point to a SharePoint site. It opens the file fine. Refreshes the file, but when it trys to save the file it throws an exception with the message "Cannot save as that name. Document was opened as read-only.".
If I try and save the file with a different filename then it works fine.
Does anybody know what I am missing? I think it must have somethoing to do with how I am opening the file. Is there another way that I can force the opening of the file in a read/write manner?
private static void RefreshExcelDocument(string filename)
{
var xls = new Microsoft.Office.Interop.Excel.Application();
xls.Visible = true;
xls.DisplayAlerts = false;
var workbook = xls.Workbooks.Open(Filename: filename, IgnoreReadOnlyRecommended: true, ReadOnly: false);
try
{
// Refresh the data from data connections
workbook.RefreshAll();
// Wait for the refresh occurs - *wish there was a better way than this.
System.Threading.Thread.Sleep(5000);
// Save the workbook back again
workbook.SaveAs(Filename: filename); // This is when the Exception is thrown
// Close the workbook
workbook.Close(SaveChanges: false);
}
catch (Exception ex)
{
//Exception message is "Cannot save as that name. Document was opened as read-only."
}
finally
{
xls.Application.Quit();
xls = null;
}
}
Many thanks in advance for suggestions.
Jonathan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
遗憾的是,您无法使用 Excel API 直接保存到 SharePoint。这就是文件以只读方式打开的原因 - 这是不允许的。
好消息是这是可能的,但您必须通过网络请求提交表单。更好的消息是 MSDN 上有 示例代码!特别注意
PublishWorkbook
方法,该方法通过 Web 请求将 Excel 文件的本地副本发送到服务器:示例代码描述了这些产品 2007 版本的场景,但其他版本的行为应为同样的方式。
Unfortunately you can't save directly to SharePoint using the Excel API. That's why the file is being opened as read only - it's not allowed.
The good news is that it is possible, but you have to submit the form via a web request. Even better news is that there is sample code on MSDN! In particular notice the
PublishWorkbook
method that sends a local copy of the Excel file to the server via a web request:The sample code describes a scenario for the 2007 versions of these products but other versions should behave in the same way.
失败示例的文件名是什么样的? SharePoint 中使用的文档不是存储在数据库中吗?还是我把你的问题理解错了?否则我可以想象您尝试存储的文件受到操作系统的写保护并且无法修改。
What does the filename of a failed example looks like? Aren't documents used in SharePoint stored in the database? Or am I getting your problem wrong? Otherwise I could imagine that the file you are trying to store is write protected by the operation system and cannot be modified.