首页 > 代码库 > SharePoint2013数据导入、读取
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(); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。