将excel导入SharePoint 2010自定义表单

本文关键字:自定义 表单 2010 SharePoint excel 导入 | 更新日期: 2023-09-27 18:29:19

我在将excel数据上传到部署在SharePoint 2010环境中的.Net表单时遇到问题。该功能是通过.net表单上传excel,excel将保存到服务器上的某个位置。excel保存在服务器上后,Microsoft.ACE.OLEDB.12.0提供商将从excel中提取数据,并将其作为逗号分隔值上传到.Net文本框控件上。

以下是我们目前面临的挑战。

1.)只有当用户以服务帐户(权限更高的帐户)的身份登录SharePoint 2010时,该功能才起作用。2.)我们尝试以用户帐户的身份登录SharePoint,但没有成功,并引发了以下错误。我认为这主要是由于权限问题。我们无法为所有用户分配服务帐户权限。

有没有一种方法可以模拟登录到服务帐户来完成上传excel的活动并处理用户帐户的登录?或者有什么建议可以帮助我解决问题吗?

错误消息:

发生错误:System.Data.OleDb.OleDbException:在System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions选项,对象池GroupProviderInfo,DbConnectionPool池,DbConnection owningObjectSystem.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnectionSystem.Data.Common.DbDataAdapter.Fill处System.Data.Comn.DbDataAdapter.QuietOpen()处System.Data.OleDb.OleDbConnection.Open(IDbConnection连接,ConnectionState&originalState)SegmentationTool.createSegment.btFileUploadForDNIS_Click(Object sender,EventArgs e)处的System.Data.Common.DbDataAdapter.Fill(数据集数据集)

代码:

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

将excel导入SharePoint 2010自定义表单

系统帐户是指定到IIS应用程序池中的用户,我想这个用户有权写入b:''test文件夹,其他用户没有这个权限,因此操作失败。

您可以使用RunWithElevatedPermission和在系统帐户权限下运行的代码。

不过我建议你:

1) 使用Microsoft OpenXml读取/写入办公室文件,最好使用简化操作的包装库,如ExcelDataReader或ClosedXml

2) 您可以将文件保存到文档库而不是文件系统中(如果您有多个前端文件系统不是更好的解决方案。)

3) 从代码中我看到你不需要保存Excel,你可以在内存中处理Excel文件(流)见第1点

希望得到帮助。