码迷,mamicode.com
首页 > 其他好文 > 详细

SharePoint2013数据导入、读取

时间:2014-06-20 09:32:22      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:des   class   blog   code   http   tar   

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数据导入、读取,布布扣,bubuko.com

SharePoint2013数据导入、读取

标签:des   class   blog   code   http   tar   

原文地址:http://blog.csdn.net/u014316433/article/details/28394803

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!