2014-11-05 83 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