SharePoint开发 - Excel数据导入到SharePoint自定义列表(数据视图方式)
2014-12-18 11:02
190 查看
博客地址 http://blog.csdn.net/foxdave 本篇讲解一个有些新颖的SharePoint实例应用,给甲方做过项目的都有过体会,数据太多了,客户有Excel,要求实现批量导入。
效果图大致如下所示
此实例是借用列表的数据视图实现导入,数据视图类似Excel,所以可以直接在上面编辑,甚至从Excel中直接粘贴进去,然后点击保存将数据插入到列表中。
首先,我们需要有一个列表,这个列表的字段跟Excel是对应的,在我的实例中选择创建列表定义及列表实例(怎么创建项目不多废话了)
创建方法戳这里
写完列表实例之后,添加一个作用于列表的事件***,用来处理后台操作。添加一个事件***,勾选正在删除、已添加和已更新事件,完成创建。
编写核心代码就可以了,在我们的实际应用中,做了一个临时表用来处理这里的数据,点击页面的保存之后再转到真实的数据表。
接下来,我们想更自动化一些,自动创建出这个列表的数据视图并设置为默认视图,用于专门的导入操作
处理FeatureActivated事件,实例中的核心代码如下
效果图大致如下所示
此实例是借用列表的数据视图实现导入,数据视图类似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地址就可以了。
相关文章推荐
- SharePoint 2010 中有个新的列表模板“导入电子表格”可以直接导入Excel数据并创建为列表 .
- SharePoint 2010 中有个新的列表模板“导入电子表格”可以直接导入Excel数据并创建为列表
- SharePoint 2010 中有个新的列表模板“导入电子表格”可以直接导入Excel数据并创建为列表 .
- 从SharePoint列表往Excel导入数据
- SharePoint 2010 中有个新的列表模板“导入电子表格”可以直接导入Excel数据并创建为列表
- QTP的那些事--在QC中importsheet方式导入excel数据驱动
- 利用UDF在Excel Service 中使用SharePoint 列表的数据
- Sharepoint2010应用开发四:使用客户对象模型(Client Object Model)向SharePoint列表写数据
- sharepoint 2010 列表的数据表视图无法使用 the list cannot be displayed in datasheet view 解决方法
- MOSS 2010:Visual Studio 2010开发体验(20)——使用ADO.NET Data Service快速访问SharePoint列表数据
- 【Android应用开发】-(22)Excel数据导入Sqlite
- Ado方式导入excel混用数据类型引起数据缺失问题解决方法
- Sharepoint2010应用开发五:使用WCF操作Sharepoint列表数据
- SharePoint 2010 使用url参数过滤列表视图数据(使用get方法过滤列表数据)
- Python 最近因开发项目的需要,有一个需求,就是很多SNS网站都有的通过 Email地址 导入好友列表,不过这次要导入的不是Email 列表,而是QQ的好友列表。 实现方式: 通过goog
- 利用OleDb方式对DataSet 和 Excel 数据快速导入导出
- ADO方式,从Excel导入数据
- MOSS 2010:Visual Studio 2010开发体验(20)——使用ADO.NET Data Service快速访问SharePoint列表数据
- 数据绑定控件把数据导入到EXCEL的实现方式
- PHPExcel导入导出及大量数据导入缓存方式的修改