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(); } }
SharePoint2013数据导入、读取
时间: 2024-10-12 22:16:54