SharePoint开发 - Excel数据导入到SharePoint自定义列表(数据视图方式)

博客地址 http://blog.csdn.net/foxdave

本篇讲解一个有些新颖的SharePoint实例应用,给甲方做过项目的都有过体会,数据太多了,客户有Excel,要求实现批量导入。

效果图大致如下所示

此实例是借用列表的数据视图实现导入,数据视图类似Excel,所以可以直接在上面编辑,甚至从Excel中直接粘贴进去,然后点击保存将数据插入到列表中。

首先,我们需要有一个列表,这个列表的字段跟Excel是对应的,在我的实例中选择创建列表定义及列表实例(怎么创建项目不多废话了)

创建方法戳这里

写完列表实例之后,添加一个作用于列表的事件接收器,用来处理后台操作。添加一个事件接收器,勾选正在删除、已添加和已更新事件,完成创建。

编写核心代码就可以了,在我们的实际应用中,做了一个临时表用来处理这里的数据,点击页面的保存之后再转到真实的数据表。

 

/// <summary>
        /// 正在删除项.
        /// </summary>
        public override void ItemDeleting(SPItemEventProperties properties)
        {
            base.ItemDeleting(properties);
            SPListItem item = properties.ListItem;
            SPMIPEntities entities = new SPMIPEntities();
            string uniqueId = item.UniqueId.ToString();
            ZY_YeZhqdjch_Temp temp = (from p in entities.ZY_YeZhqdjch_Temp where p.UniqueId.Equals(uniqueId) select p).FirstOrDefault();
            entities.DeleteObject(temp);
            entities.SaveChanges();
        }

        /// <summary>
        /// 已添加项.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            SPListItem item = properties.ListItem;
            SPMIPEntities entities = new SPMIPEntities();
            ZY_YeZhqdjch_Temp temp = new ZY_YeZhqdjch_Temp();
            string a = properties.UserLoginName;
            //获取登录者账号
            string username = properties.UserLoginName.Substring(properties.UserLoginName.IndexOf("spmipmp|") + 8);
            var list = from p in entities.SYS_User where (p.UserCode.Equals(username)) select new { p.UserCode, p.Zhi_gid };
            DataTable dt = IQueryableExtensions.ToDataTable(list);
            //获取登录者ID
            temp.Chuang_jzh = Convert.ToInt32(dt.Rows[0]["Zhi_gid"]);
            temp.UniqueId = item.UniqueId.ToString();

            if (item["业主清单编码"] == null) { temp.Ye_zhqdbm = ""; }
            else { temp.Ye_zhqdbm = item["业主清单编码"].ToString(); }
            if (item["业主清单名称"] == null) { temp.Ye_zhqdmch = ""; }
            else { temp.Ye_zhqdmch = item["业主清单名称"].ToString(); }
            if (item["清单项目特征"] == null) { temp.Qing_dxmtzh = ""; }
            else { temp.Qing_dxmtzh = item["清单项目特征"].ToString(); }
            if (item["单位"] == null) { temp.Dan_w = ""; }
            else { temp.Dan_w = item["单位"].ToString(); }
            if (item["清单量"] == null) { temp.Qing_dl = 0; }
            else { temp.Qing_dl = Convert.ToDecimal(item["清单量"]); }
            if (item["签证量"] == null) { temp.Qian_zhl = 0; }
            else { temp.Qian_zhl = Convert.ToDecimal(item["签证量"]); }
            if (item["验收量"] == null) { temp.Yan_shl = 0; }
            else { temp.Yan_shl = Convert.ToDecimal(item["验收量"]); }
            if (item["合同单价"] == null) { temp.He_tdj = 0; }
            else { temp.He_tdj = Convert.ToDecimal(item["合同单价"]); }
            if (item["业主签认单价"] == null) { temp.Ye_zhqrdj = 0; }
            else { temp.Ye_zhqrdj = Convert.ToDecimal(item["业主签认单价"]); }
            if (item["预计决算单价"] == null) { temp.Yu_jjsdj = 0; }
            else { temp.Yu_jjsdj = Convert.ToDecimal(item["预计决算单价"]); }
            if (item["预计审减比例"] == null) { temp.Yu_jshjbl = 0; }
            else { temp.Yu_jshjbl = Convert.ToDecimal(item["预计审减比例"]); }
            if (item["决算单价"] == null) { temp.Jue_sdj = 0; }
            else { temp.Jue_sdj = Convert.ToDecimal(item["决算单价"]); }
            entities.AddToZY_YeZhqdjch_Temp(temp);
            entities.SaveChanges();
        }

        /// <summary>
        /// 已更新项.
        /// </summary>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);
            SPListItem item = properties.ListItem;
            SPMIPEntities entities = new SPMIPEntities();
            string uniqueId = item.UniqueId.ToString();
            ZY_YeZhqdjch_Temp temp = (from p in entities.ZY_YeZhqdjch_Temp where p.UniqueId.Equals(uniqueId) select p).FirstOrDefault();
            //获取登录者账号
            string username = properties.UserLoginName.Substring(properties.UserLoginName.IndexOf("spmipmp|") + 8);
            var list = from p in entities.SYS_User where (p.UserCode.Equals(username)) select new { p.UserCode, p.Zhi_gid };
            DataTable dt = IQueryableExtensions.ToDataTable(list);
            //获取登录者ID
            temp.Chuang_jzh = Convert.ToInt32(dt.Rows[0]["Zhi_gid"]);
            if (item["业主清单编码"] == null) { temp.Ye_zhqdbm = ""; }
            else { temp.Ye_zhqdbm = item["业主清单编码"].ToString(); }
            if (item["业主清单名称"] == null) { temp.Ye_zhqdmch = ""; }
            else { temp.Ye_zhqdmch = item["业主清单名称"].ToString(); }
            if (item["清单项目特征"] == null) { temp.Qing_dxmtzh = ""; }
            else { temp.Qing_dxmtzh = item["清单项目特征"].ToString(); }
            if (item["单位"] == null) { temp.Dan_w = ""; }
            else { temp.Dan_w = item["单位"].ToString(); }
            if (item["清单量"] == null) { temp.Qing_dl = 0; }
            else { temp.Qing_dl = Convert.ToDecimal(item["清单量"]); }
            if (item["签证量"] == null) { temp.Qian_zhl = 0; }
            else { temp.Qian_zhl = Convert.ToDecimal(item["签证量"]); }
            if (item["验收量"] == null) { temp.Yan_shl = 0; }
            else { temp.Yan_shl = Convert.ToDecimal(item["验收量"]); }
            if (item["合同单价"] == null) { temp.He_tdj = 0; }
            else { temp.He_tdj = Convert.ToDecimal(item["合同单价"]); }
            if (item["业主签认单价"] == null) { temp.Ye_zhqrdj = 0; }
            else { temp.Ye_zhqrdj = Convert.ToDecimal(item["业主签认单价"]); }
            if (item["预计决算单价"] == null) { temp.Yu_jjsdj = 0; }
            else { temp.Yu_jjsdj = Convert.ToDecimal(item["预计决算单价"]); }
            if (item["预计审减比例"] == null) { temp.Yu_jshjbl = 0; }
            else { temp.Yu_jshjbl = Convert.ToDecimal(item["预计审减比例"]); }
            if (item["决算单价"] == null) { temp.Jue_sdj = 0; }
            else { temp.Jue_sdj = Convert.ToDecimal(item["决算单价"]); }
            entities.SaveChanges();
        }


接下来,我们想更自动化一些,自动创建出这个列表的数据视图并设置为默认视图,用于专门的导入操作

 

处理FeatureActivated事件,实例中的核心代码如下

 

SPWeb _web = properties.Feature.Parent as SPWeb;
            SPList list = _web.Lists.TryGetList("业主清单基础导入");
            SPList listTwo = _web.Lists.TryGetList("内部清单基础导入");
            SPView newView = null;
            SPView newViewTwo = null;
            string strQuery = default(string);
            string strQueryTwo = default(string);
            StringCollection strCol = new StringCollection();
            StringCollection strColTwo = new StringCollection();
            newView = list.Views.Cast<SPView>().FirstOrDefault(v => v.Title == "业主清单基础");
            if (newView == null)
            {
                strCol.Clear();
                strCol.Add("Title");
                strCol.Add("Ye_zhqdmch");
                strCol.Add("Qing_dxmtzh");
                strCol.Add("Dan_w");
                strCol.Add("Qing_dl");
                strCol.Add("Qian_zhl");
                strCol.Add("Yan_shl");
                strCol.Add("He_tdj");
                strCol.Add("Ye_zhqrdj");
                strCol.Add("Yu_jjsdj");
                strCol.Add("Yu_jshjbl");
                strCol.Add("Jue_sdj");
                newView = list.Views.Add("Default", strCol, strQuery, 30, true/**//*是否支持分页*/, true/**//*是否是默认视图*/,
                    Microsoft.SharePoint.SPViewCollection.SPViewType.Grid, false);
                newView.Title = "业主清单基础";
                newView.Update();
            }
            newViewTwo = listTwo.Views.Cast<SPView>().FirstOrDefault(v => v.Title == "内部清单基础");
            if (newViewTwo == null)
            {
                strColTwo.Clear();
                strColTwo.Add("Title");
                strColTwo.Add("Nei_bqdbm");
                strColTwo.Add("Nei_bqdmch");
                strColTwo.Add("Xiang_mtzh");
                strColTwo.Add("Dan_w");
                strColTwo.Add("Gong_chshl");
                strColTwo.Add("Ding_e");
                strColTwo.Add("Ren_gf");
                strColTwo.Add("Ji_xf");
                strColTwo.Add("Qu_ybm");
                strColTwo.Add("Qu_ymch");
                newViewTwo = listTwo.Views.Add("Default", strColTwo, strQueryTwo, 30, true/**//*是否支持分页*/, true/**//*是否是默认视图*/,
                    Microsoft.SharePoint.SPViewCollection.SPViewType.Grid, false);
                newViewTwo.Title = "内部清单基础";
                newViewTwo.Update();
            }

最后一步,在应用程序页上应用它(这里我只是描述实例的情况,具体怎么应用可以自行选择),在页面上添加一个iframe元素,引入列表视图的URL地址就可以了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值