2014-11-05 80 views
0

我在將Excel數據上傳到SharePoint 2010環境中部署的.Net表單時遇到問題。功能是通過.net格式上傳excel,並將excel保存到服務器上的某個位置。一旦將excel保存在服務器上,Microsoft.ACE.OLEDB.12.0提供程序將從Excel中提取數據,並將其上載爲.Net文本框控件上的逗號分隔值。將Excel導入到SharePoint 2010自定義表格

以下是我們目前面臨的挑戰。

1.)僅當用戶以SharePoint 2010上的服務帳戶(更高權限帳戶)身份登錄時,此功能才起作用。 2.)我們嘗試以SharePoint用戶帳戶身份登錄,但它無法正常工作,正在拋出下面的錯誤。我認爲這主要是由於權限問題。我們無法爲所有用戶分配服務帳戶權限。

有沒有辦法讓我們模擬登錄到服務帳戶來完成上傳excel和處理用戶帳戶登錄的活動?或有任何建議可以幫助我解決我的問題?

錯誤消息:

出錯信息:System.Data.OleDb.OleDbException:在System.Data.OleDb.OleDbConnectionInternal..ctor未指定的錯誤在System.Data.OleDb(OleDbConnectionString構造,OleDbConnection的連接) System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)在System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection,DbConnectionPoolGroup poolGroup).OleDbConnectionFactory.CreateConnection(DbConnectionOptions選項,對象poolGroupProviderInfo,DbConnectionPool池,DbConnection擁有對象) .Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection,DbConnection (System.Data.Common.DbDataAdapter.QuietOpen(IDbConnection connection,ConnectionState & originalState)at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset,DataTable [])中的System.Data.OleDb.OleDbConnection.Open() System.Data.Common.DbDataAdapter.Fill(DataSet dataSet,Int32 startRecord,Int32 maxRecords,String srcTable,IDbCommand命令,CommandBehavior行爲)中的數據庫,Intat startRecord,Int32 maxRecords,String srcTable,IDbCommand命令,CommandBehavior行爲) .Common.DbDataAdapter.Fill(數據集的數據集)中的SegmentationTool.createSegment.btnFileUploadForDNIS_Click(對象發件人,EventArgs的)

代碼:

strTarget = fileUploadForDNIS.FileName; 

    string[] arrCheckExtension = strTarget.Split('.'); 
        if (arrCheckExtension.Length >= 2) 
        { 
         if (arrCheckExtension[1].ToString().Equals("xls") || arrCheckExtension[1].ToString().Equals("xlsx")) 
         { 
          fileUploadForDNIS.SaveAs("B:\\Test\\" + strTarget); 
          strConnForExcel = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;""", "B:\\Test\\"+strTarget); 
          strQueryForExcel = String.Format("select dnis_id from [{0}$]", "oms_dnis"); 
          OleDbDataAdapter adapForDNIS = new OleDbDataAdapter(strQueryForExcel, strConnForExcel); 
          dsForDNIS = new DataSet(); 
          adapForDNIS.Fill(dsForDNIS); 
          if (dsForDNIS.Tables[0].Rows.Count > 0) 
          { 
           for (int i = 0; i < dsForDNIS.Tables[0].Rows.Count; i++) 
           { 
            if (strDNISids == "") 
            { 
             strDNISids += dsForDNIS.Tables[0].Rows[i]["dnis_id"].ToString(); 
            } 
            else 
            { 
             strDNISids += "," + dsForDNIS.Tables[0].Rows[i]["dnis_id"].ToString(); 
            } 
           } 
           txtSpecAcquWinbackDNISForUpload.Text = strDNISids; 
           rdoMSCSpecificValue.Focus(); 
           System.IO.File.Delete("B:\\Test\\" + strTarget); 
          } 
         } 
         else 
         { 
          Response.Write("<script language='javascript'>alert('Please Select File with .xls or xlsx Extension');</script>"); 
          fileUploadForDNIS.Focus(); 
         } 
        } 

回答

0

系統帳戶是指定到IIS應用程序池的用戶,我想這個用戶有權寫入b:\ test文件夾,其他用戶沒有這個權限,因此操作失敗。

您可以使用RunWithElevatedPermission以及在系統帳戶權限下運行的代碼。

不過我建議你:

1)使用Microsoft OpenXml讀/寫辦公文件,最​​好使用包裝庫,簡化像ExcelDataReaderClosedXml

2)操作您可以將文件保存到文檔庫而不是文件系統(如果你有多個FrontEnd文件系統不是更好的解決方案..)

3)從我看到你,你並不需要保存Excel,您可以處理內存中的Excel文件中的代碼(流)見點1

希望這有助於。

+0

感謝Max的迴應,我想將我的代碼更改爲存儲讀取excel數據並將其存儲在內存流中,並且它們將excel數據分配給字符串。目前,我正在嘗試從SharePoint 2010服務器文件系統中保存和讀取excel時遇到許多問題。我很感激你指導我到上面的鏈接。但是,你能否告訴我它是否有可能將數據存儲在內存流中並通過C#將其分配給一個字符串? – user545359 2014-11-06 23:29:18

+0

[See here](http://stackoverflow.com/questions/262341/get-data-from-an-uploaded-excel-file-without-saving-to-file-system)用ExcelDataReader從內存中讀取excel,無論如何不清楚你想達到什麼目的。 – Max 2014-11-06 23:49:08