2010-11-09 6 views
6

Ho scritto una procedura che aprirà un xls da un disco locale, aggiornerà i dati al suo interno e quindi lo salverà di nuovo. Funziona beneApertura di un foglio di calcolo xls in modo programmatico in C# da un sito di SharePoint in modalità di lettura/scrittura

Il problema si verifica quando si sostituisce il nome file per fare riferimento a un sito di SharePoint. Apre il file bene. Aggiorna il file, ma quando recupera per salvare il file genera un'eccezione con il messaggio "Impossibile salvare come quel nome. Il documento è stato aperto in sola lettura.". Se provo a salvare il file con un nome file diverso, allora funziona bene.

Qualcuno sa cosa mi manca? Penso che debba avere a che fare con il modo in cui sto aprendo il file. C'è un altro modo in cui posso forzare l'apertura del file in modo lettura/scrittura?

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; 
     } 
    } 

Molte grazie in anticipo per i suggerimenti.

Jonathan

risposta

7

Purtroppo non è possibile salvare direttamente in SharePoint utilizzando l'API di Excel. Ecco perché il file viene aperto in sola lettura - non è consentito.

La buona notizia è che è possibile, ma è necessario inviare il modulo tramite una richiesta web. Le notizie ancora migliori sono che c'è lo sample code on MSDN! In particolare avviso il metodo PublishWorkbook che invia una copia locale del file di Excel al server tramite una richiesta Web:

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(); 
    } 
} 

Il codice di esempio descrive uno scenario per le versioni 2007 di questi prodotti, ma altre versioni dovrebbero comportarsi in stessa strada.

0

Che cosa significa il nome di un esempio riuscito assomiglia? I documenti non vengono utilizzati in SharePoint memorizzati nel database? O sto sbagliando il tuo problema? Altrimenti potrei immaginare che il file che stai cercando di memorizzare sia protetto da scrittura dal sistema operativo e non possa essere modificato.

+0

L'URL del documento è simile a "https: //domainname/sites/sitename/Shared%20Documents/filename.xls". Può aprire questo file e salvare il file in un url come questo, tuttavia non può salvare nello stesso file. Non si dovrebbe fare riferimento al documento nel database direttamente poiché Microsoft potrebbe cambiare il metodo di memorizzazione del documento durante la notte. –

Problemi correlati