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

标签 c# excel sharepoint wss

我写了一个程序,可以从本地磁盘打开一个 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;
        }
    }

非常感谢您的建议。

乔纳森

最佳答案

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

好消息是这是可能的,但您必须通过网络请求提交表单。更好的消息是有 sample code on 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 版本的场景,但其他版本应该以相同的方式运行。

关于c# - 在读/写模式下从 SharePoint 站点以 C# 编程方式打开 xls 电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4132160/

相关文章:

c# - 如何在TextBox中书写时自动添加点

VBA:输入关键字的分隔符

asp.net - 开发一个网站花费300万。用户: SharePoint OR pure ASP. NET?

sharepoint - Sharepoint 列表和每月上传到 Sharepoint 的 Excel 文件之间的核对

azure - 如何使用逻辑应用程序将新行添加到共享点网站内部的 csv 文件中?

c# - Ping 网络上的主机名

c# - 无法在运行时从程序集中获取方法

c# - BufferedStream 和 MemoryStream 在应用上有什么区别?

c# - 使用 oledb 从 Excel 中读取整数

excel - VBA 启动画面