客户端对象模型之列表数据导出到Excel
1,废话不多话,直接上代码,留着以后做类似功能时Copy一下!有需要的朋友也可以参考一下。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="jquery-1.6.2.min.js"></script>
<script type="text/javascript"> function exportalltaskdata() {
var flag = true;
try {
folderpath = BrowseFolder();
if (folderpath == null || folderpath == 'null' || folderpath == '') {
flag = false;
alert("请选择保存路径!");
} } catch (e) {
flag = false;
alert("请确认只能使用IE导出,并将该站点添加到本地ie信任站点中,IE浏览器要设置工具-Internet选择-安全-可信站点-自定义级别-ActiveX控件和插件-将‘对未标记为可安全执行脚本的ActiveX控件初并执行脚本’选项置为‘启用");
}
if (flag) {
oldvalue = $("#btnExport").val();
$("#btnExport").attr("disabled", "disabled");
$("#btnExport").val("正在导出...");
try {
var xls = new ActiveXObject("Excel.Application");
GetAllTasksWithProject();
}
catch (e) {
alert("请确保本地安装了Excel");
$("#btnExport").removeAttr("disabled");
$("#btnExport").val(oldvalue);
}
} }
function GetAllTasksWithProject() {
var projectid = getQueryString("FilterValue1");
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('项目工作项');
var queryCertType = new SP.CamlQuery();
queryCertType.set_viewXml(
'<View><Query><OrderBy><FieldRef Name=\'StartDate\' Ascending=\'True\'/></OrderBy><Where><Eq><FieldRef Name=\'Project\' LookupId=\'TRUE\' /><Value Type=\'Lookup\'>' + projectid + '</Value></Eq></Where></Query></View>'
); this.wangoListItemCertType = oList.getItems(queryCertType);
clientContext.load(wangoListItemCertType);
clientContext.executeQueryAsync(Function.createDelegate(this, this.ExportSuccessed), Function.createDelegate(this, this.ExportFailed));
}
var orderedtasks = new TaskColliection();
var relationship = new TaskColliection();
var roottask = new TaskColliection();
var alltasks = new TaskColliection(); function MakeTaskOrdered(parentid) {
if (relationship.ContainsKey(parentid)) {
var children = relationship.get(parentid);
for (var i = 0; i < children.size() ; i++) {
if (!orderedtasks.ContainsKey(children.keys[i])) {
var level = 0;
if (orderedtasks.ContainsKey(parentid)) {
level = orderedtasks.get(parentid) + 1;
}
orderedtasks.set(children.keys[i], level);
MakeTaskOrdered(children.keys[i]);
}
}
}
}
function ExportSuccessed(sender, args) {
orderedtasks = new TaskColliection();
relationship = new TaskColliection();
roottask = new TaskColliection();
alltasks = new TaskColliection(); var listItemEnumerator = wangoListItemCertType.getEnumerator();
while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
var taskid = oListItem.get_item('ID');
var title = oListItem.get_item('Title');
var ParentTask = oListItem.get_item('ParentTask');
if (ParentTask != null && ParentTask != '' && ParentTask != 'null') {
var parentid = ParentTask.get_lookupId();
if (!relationship.ContainsKey(parentid)) {
var children = new Map();
children.set(taskid, title);
relationship.set(parentid, children);
}
else {
var children = relationship.get(parentid)
children.set(taskid, title);
relationship.set(parentid, children)
}
}
else {
if (!relationship.ContainsKey(taskid)) {
var children = new Map();
relationship.set(taskid, children);
}
roottask.set(taskid, title);
}
alltasks.set(taskid, oListItem)
}
for (var num = 0; num < roottask.size() ; num++) {
if (!orderedtasks.ContainsKey(roottask.keys[num])) {
orderedtasks.set(roottask.keys[num], 0);
MakeTaskOrdered(roottask.keys[num]);
}
} var oApplication;
var xls = new ActiveXObject("Excel.Application");
xls.Visible = false;
var xlBook = xls.Workbooks.Add;
var oSheet = xlBook.Worksheets(1);
oSheet.Cells(1, 1).value = "工作项名称";
oSheet.Cells(1, 2).value = "开始日期";
oSheet.Cells(1, 3).value = "截止日期";
oSheet.Cells(1, 4).value = "负责人";
oSheet.Cells(1, 6).value = "前置工作项";
oSheet.Cells(1, 5).value = "父级工作项";
oSheet.Cells(1, 10).value = "所属项目";
oSheet.Cells(1, 7).value = "关系类型";
oSheet.Cells(1, 9).value = "任务级别";
oSheet.Cells(1, 8).value = "是否完成";
var projecttitle = "";
for (var i = 0; i < orderedtasks.size() ; i++) {
var tasklevelnum = orderedtasks.get(orderedtasks.keys[i]);
if (alltasks.ContainsKey(orderedtasks.keys[i])) {
var oListItem = alltasks.get(orderedtasks.keys[i]);
var title = oListItem.get_item('Title');
var startdate = new Date(oListItem.get_item('StartDate'));
var dueDate = new Date(oListItem.get_item('DueDate'));
var AssignedTo = oListItem.get_item('AssignedTo');
var Predecessors = oListItem.get_item('Predecessors');
var Status = oListItem.get_item('Status');
var RelationshipType = oListItem.get_item('RelationshipType');
var TaskLevel = oListItem.get_item('TaskLevel');
var Project = oListItem.get_item('Project');
var ParentTask = oListItem.get_item('ParentTask');
var rownum = i + 2;
switch (tasklevelnum) {
case 1:
title = " " + title;
break;
case 2:
title = " " + title;
break;
case 3:
title = " " + title;
break;
case 4:
title = " " + title;
break;
}
oSheet.Cells(rownum, 1).value = title;
oSheet.Cells(rownum, 2).value = startdate.getFullYear() + "/" + (startdate.getMonth() + 1) + "/" + startdate.getDate();
oSheet.Cells(rownum, 3).value = dueDate.getFullYear() + "/" + (dueDate.getMonth() + 1) + "/" + dueDate.getDate();
oSheet.Cells(rownum, 4).value = AssignedTo.get_lookupValue();
if (Predecessors != null && Predecessors != 'null' && Predecessors != '')
oSheet.Cells(rownum, 6).value = Predecessors.get_lookupValue();
if (ParentTask != null && ParentTask != 'null' && ParentTask != '')
oSheet.Cells(rownum, 5).value = ParentTask.get_lookupValue();
oSheet.Cells(rownum, 10).value = Project.get_lookupValue();
projecttitle = Project.get_lookupValue();
oSheet.Cells(rownum, 7).value = RelationshipType;
oSheet.Cells(rownum, 9).value = TaskLevel;
oSheet.Cells(rownum, 8).value = Status;
}
}
var datenow = new Date();
var filetitle = projecttitle + "_" + datenow.format("yyyyMMddHHmmss") + ".xls"; oSheet.SaveAs(folderpath + filetitle); xls.quit();
alert("导出成功");
$("#btnExport").removeAttr("disabled");
$("#btnExport").val(oldvalue); }
function ExportFailed(sender, args) {
alert('导出失败,请重试');
$("#btnExport").removeAttr("disabled");
$("#btnExport").val(oldvalue); }
function MakeTaskOrdered(parentid) {
if (relationship.ContainsKey(parentid)) {
var children = relationship.get(parentid);
for (var i = 0; i < children.size() ; i++) {
if (!orderedtasks.ContainsKey(children.keys[i])) {
var level = 0;
if (orderedtasks.ContainsKey(parentid)) {
level = orderedtasks.get(parentid) + 1;
}
orderedtasks.set(children.keys[i], level);
MakeTaskOrdered(children.keys[i]);
}
}
}
}
function TaskColliection() {
this.keys = new Array();
this.data = new Array();
//添加键值对
this.set = function (key, value) {
if (this.data[key] == null) {//如键不存在则身【键】数组添加键名
this.keys.push(key);
}
this.data[key] = value;//给键赋值
};
//获取键对应的值
this.get = function (key) {
return this.data[key];
};
//去除键值,(去除键数据中的键名及对应的值)
this.remove = function (key) {
this.keys.remove(key);
this.data[key] = null;
};
//判断键值元素是否为空
this.isEmpty = function () {
return this.keys.length == 0;
};
//获取键值元素大小
this.size = function () {
return this.keys.length;
};
this.ContainsKey = function (key) {
if (this.data[key] == null) {//如键不存在则身【键】数组添加键名
return false;
}
else {
return true;
}
}
}
function BrowseFolder() { var Message = "选择下载路径."; //选择框提示信息
var Shell = new ActiveXObject("Shell.Application");
//var Folder = Shell.BrowseForFolder(0, Message, 0x0040, 0x11); //起始目录为:我的电脑
var Folder = Shell.BrowseForFolder(0, Message, 0); //起始目录为:桌面
var folderpath = "";
if (Folder != null) {
folderpath = Folder.Self.Path; // 返回路径
if (folderpath.charAt(folderpath.length - 1) != "\\") {
folderpath = folderpath + "\\";
}
}
return folderpath; } </script>
</head>
<body>
<input name="btnExport" id="btnExport" type="button" value="导出当前项目的所有工作项到Excel" style="position: absolute; right: 10px; top: 0px;" onclick="exportalltaskdata();" />
</body>
</html>
客户端对象模型之列表数据导出到Excel的更多相关文章
- DataTable数据导出到Excel,并发送到客户端进行下载
本代码实现思路是:页面显示和导出分开,导出的数据和用于页面显示的是同一查询数据方式,所以也是同样的数据,只是在导出数据时从数据库重新捞了一次数据.此导出数据方式会先将数据保存到Excel中,然后将创建 ...
- 学习笔记 DataGridView数据导出为Excel
DataGridView数据导出为Excel 怎样把WinForm下的“DGV”里的绑定数据库后的数据导出到Excel中. 比如:在窗体里有个一“DGV”,DataGridView1,绑定了数据源 ...
- 数据导出至Excel文件--好库编程网http://code1.okbase.net/codefile/SerializeHelper.cs_2012122018724_118.htm
using System; using System.IO; using System.Data; using System.Collections; using System.Data.OleDb; ...
- asp.net将数据导出到excel
本次应用datatable导出,若用gridview(假设gridview设为了分页显示)会出现只导出当前页的情况. protected void btnPrn_Click(object sender ...
- 将datagrid中数据导出到excel中 -------<<工作日志2014-6-6>>
前台datagrid数据绑定 #region 导出到excel中 /// <summary> /// 2014-6-6 /// </summary> / ...
- abp中文件下载,将内存数据导出到Excel并下载
1.数据导出为Excel的Stream using System; using System.Collections.Generic; using System.IO; using Abp.Colle ...
- 大批量数据导出到Excel的实现
在平时的项目中,将数据导出到Excel的需求是很常见的,在此对一些常见的方法做以总结,并提供一种大数据量导出的实现. OLEDB 使用OLEDB可以很方便导出Excel,思路很简单,处理时将Exc ...
- MySQL数据导出为Excel, json,sql等格式
MySQL数据经常要导出为Excel, json,sql等格式,通过步骤都很多,麻烦,现在通过Treesoft可以方便的导出你要的数据格式. 1.在线执行SQL,在数据列表中有相应按钮,方便的将数据导 ...
- asp.net 将repeater上数据导出到excel
1,首先得到一个DataTable public DataTable GetTable(string sql) { SqlConnnection con=new SqlConnection(Confi ...
随机推荐
- Javascript 中正则表达式验证网址
其中ItemURL是需要验证的网址数据
- Android Tab与TabHost
这就是Tab,而盛放Tab的容器就是TabHost 如何实现?? 每一个Tab还对应了一个布局,这个就有点好玩了.一个Activity,对应了多个功能布局. ①新建一个Tab项目,注意,不要生成mai ...
- PowerDNS Authoritative Server 3.3 发布
PowerDNS Authoritative Server 3.3 发布,该版本改进了不同验证器的交互操作,修复了不少 bug. PowerDNS Authoritative Server (PDNS ...
- PRINCE2认证与其他项目管理认证标准有何不同?
国际项目管理协会 (IPMA)根据国际能力基线建立了一种国际公认的标准,并由其各国协会译成各国语言.这是IPMA四级证书的基础,现在已在全世界推广.可喜的是,PRINCE2与这个标准高度一致. 美国项 ...
- Androidpdf
https://www.jb51.net/article/110238.htm https://blog.csdn.net/u010046908/article/details/53927157 &l ...
- java笔记--String类格式化当天日期转换符文档
String类格式化当天日期 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3877389.html "谢谢-- 转换符:% ...
- 关于asp.net MVC3 ----@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction
1.带有Render的方法返回值是void,在方法内部进行输出:不带的返回值类型为MvcHtmlString,所以只能这样使用:@Html.Partial 对应 @{Html.RenderPartia ...
- 转 string和byte[]的转换 (C#)
转 string和byte[]的转换 (C#) string类型转成byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ...
- python的文件对象(1)
1 首先要明确的是,文件只是连续的字节. 数据的传输经常会用到字节流,无论字节流是由单个字节还是大块数据组成. 2 打开文件之门的钥匙--open() open()内建函数成功打开文件后会返回一个 ...
- [EffectiveC++]item41:了解隐式接口和编译器多态
classes和templates都支持接口和多态,interfaces and polymorphism 对classes而言接口是显示的explicit,以函数签名为中心.多态则是通过virtua ...