SharePoint2013数据导入、读取

static void Main(string[] args)
        {
            //OperateFolderItem();
            DataTable dt = ExcelToDS(@"C:\Users\Administrator\Desktop\wage1.xlsx");
            using (SPSite spSite = new SPSite("http://127.0.0.1"))
            {
                using (SPWeb spWeb = spSite.RootWeb)
                {
                    SPList list = spWeb.GetListFromUrl("/Lists/salary/AllItems.aspx");
                    foreach (DataRow row in dt.AsEnumerable())
                    {
                        //向列表中指定的文件夹中添加列表项
                        SPListItem spListItem = list.AddItem("/Lists/salary/2014/2", SPFileSystemObjectType.File);

                        spListItem[spListItem.Fields["Number"].InternalName] = row[0];
                        spListItem["Date"] = Convert.ToDateTime(row[2]);
                        spListItem[spListItem.Fields["Name"].InternalName] = row[1];
                        spListItem[spListItem.Fields["Else"].InternalName] = row[5];
                        spListItem[spListItem.Fields["RealWage"].InternalName] = row[7];
                        spListItem[spListItem.Fields["Tax"].InternalName] = row[6];
                        spListItem[spListItem.Fields["Wage"].InternalName] = row[3];
                        spListItem[spListItem.Fields["YangLao"].InternalName] = row[4];
                        //别忘了保存
                        spListItem.Update();
                    }
                }
            }
        }

        private static void OperateFolderItem()
        {
            //要读取的文件夹ID
            int _iFolderId = 21;
            using (SPSite spSite = new SPSite("http://127.0.0.1"))
            {
                using (SPWeb spWeb = spSite.RootWeb)
                {
                    SPList list = spWeb.GetListFromUrl("/Lists/salary/AllItems.aspx");
                    SPQuery query = new SPQuery();
                    query.ViewAttributes = "Scope=\"Recursive\"";
                    //设置查询文件夹并且统计查询列表项数量
                    if (_iFolderId == -1)
                    {
                        query.Folder = list.RootFolder;
                    }
                    else
                    {
                        query.Folder = list.GetItemById(_iFolderId).Folder;
                    }
                    //查询列表中的文件夹中的所有列表项
                    SPListItemCollection spListItemColl = list.GetItems(query);
                    DataTable dtss = spListItemColl.GetDataTable();

                    //向列表中指定的文件夹中添加列表项
                    SPListItem spListItem = list.AddItem("/Lists/salary/2014/1", SPFileSystemObjectType.File);
                    spListItem["Title"] = "a";
                    spListItem["应发酬金"] = 66;
                    //别忘了保存
                    spListItem.Update();
                }
            }
        }
        public static DataTable ExcelToDS(string Path)
        {
            string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "Data Source=" + Path + ";" + "Extended Properties='Excel 12.0;HDR=Yes;IMEX=1';";

            OleDbConnection conn = new OleDbConnection(strConn);
            try
            {
                DataTable dt = new DataTable();
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                string strExcel = "select * from [Sheet1$]";
                OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, conn);
                adapter.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (conn.State != ConnectionState.Closed)
                    conn.Close();
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值