在读/写模式下从 SharePoint 站点以 C# 编程方式打开 xls 电子表格

发布于 2024-09-30 23:24:13 字数 1329 浏览 0 评论 0原文

我编写了一个程序,可以从本地磁盘打开 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 技术交流群。

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

发布评论

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

评论(2

柠檬心 2024-10-07 23:24:13

遗憾的是,您无法使用 Excel API 直接保存到 SharePoint。这就是文件以只读方式打开的原因 - 这是不允许的。

好消息是这是可能的,但您必须通过网络请求提交表单。更好的消息是 MSDN 上有 示例代码!特别注意 PublishWorkbook 方法,该方法通过 Web 请求将 Excel 文件的本地副本发送到服务器:

static void PublishWorkbook(string LocalPath, string SharePointPath)
{
    WebResponse response = null;

    try
    {
        // Create a PUT Web request to upload the file.
        WebRequest request = WebRequest.Create(SharePointPath);

        request.Credentials = CredentialCache.DefaultCredentials;
        request.Method = "PUT";

        // Allocate a 1K buffer to transfer the file contents.
        // The buffer size can be adjusted as needed depending on
        // the number and size of files being uploaded.
        byte[] buffer = new byte[1024];

        // Write the contents of the local file to the
        // request stream.
        using (Stream stream = request.GetRequestStream())
        using (FileStream fsWorkbook = File.Open(LocalPath,
            FileMode.Open, FileAccess.Read))
        {
            int i = fsWorkbook.Read(buffer, 0, buffer.Length);

            while (i > 0)
            {
                stream.Write(buffer, 0, i);
                i = fsWorkbook.Read(buffer, 0, buffer.Length);
            }
        }

        // Make the PUT request.
        response = request.GetResponse();
    }
    finally
    {
        response.Close();
    }
}

示例代码描述了这些产品 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:

static void PublishWorkbook(string LocalPath, string SharePointPath)
{
    WebResponse response = null;

    try
    {
        // Create a PUT Web request to upload the file.
        WebRequest request = WebRequest.Create(SharePointPath);

        request.Credentials = CredentialCache.DefaultCredentials;
        request.Method = "PUT";

        // Allocate a 1K buffer to transfer the file contents.
        // The buffer size can be adjusted as needed depending on
        // the number and size of files being uploaded.
        byte[] buffer = new byte[1024];

        // Write the contents of the local file to the
        // request stream.
        using (Stream stream = request.GetRequestStream())
        using (FileStream fsWorkbook = File.Open(LocalPath,
            FileMode.Open, FileAccess.Read))
        {
            int i = fsWorkbook.Read(buffer, 0, buffer.Length);

            while (i > 0)
            {
                stream.Write(buffer, 0, i);
                i = fsWorkbook.Read(buffer, 0, buffer.Length);
            }
        }

        // Make the PUT request.
        response = request.GetResponse();
    }
    finally
    {
        response.Close();
    }
}

The sample code describes a scenario for the 2007 versions of these products but other versions should behave in the same way.

静谧幽蓝 2024-10-07 23:24:13

失败示例的文件名是什么样的? 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.

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