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的更多相关文章

  1. DataTable数据导出到Excel,并发送到客户端进行下载

    本代码实现思路是:页面显示和导出分开,导出的数据和用于页面显示的是同一查询数据方式,所以也是同样的数据,只是在导出数据时从数据库重新捞了一次数据.此导出数据方式会先将数据保存到Excel中,然后将创建 ...

  2. 学习笔记 DataGridView数据导出为Excel

    DataGridView数据导出为Excel   怎样把WinForm下的“DGV”里的绑定数据库后的数据导出到Excel中. 比如:在窗体里有个一“DGV”,DataGridView1,绑定了数据源 ...

  3. 数据导出至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; ...

  4. asp.net将数据导出到excel

    本次应用datatable导出,若用gridview(假设gridview设为了分页显示)会出现只导出当前页的情况. protected void btnPrn_Click(object sender ...

  5. 将datagrid中数据导出到excel中 -------<<工作日志2014-6-6>>

    前台datagrid数据绑定 #region 导出到excel中    /// <summary>    /// 2014-6-6    /// </summary>    / ...

  6. abp中文件下载,将内存数据导出到Excel并下载

    1.数据导出为Excel的Stream using System; using System.Collections.Generic; using System.IO; using Abp.Colle ...

  7. 大批量数据导出到Excel的实现

    在平时的项目中,将数据导出到Excel的需求是很常见的,在此对一些常见的方法做以总结,并提供一种大数据量导出的实现. OLEDB   使用OLEDB可以很方便导出Excel,思路很简单,处理时将Exc ...

  8. MySQL数据导出为Excel, json,sql等格式

    MySQL数据经常要导出为Excel, json,sql等格式,通过步骤都很多,麻烦,现在通过Treesoft可以方便的导出你要的数据格式. 1.在线执行SQL,在数据列表中有相应按钮,方便的将数据导 ...

  9. asp.net 将repeater上数据导出到excel

    1,首先得到一个DataTable public DataTable GetTable(string sql) { SqlConnnection con=new SqlConnection(Confi ...

随机推荐

  1. java 自定义 LRU(最近最少使用)策略 实现 缓存机制

    1. java提供了一个简单的方式实现LRU:  LinkedHashMap   2. 自定义实现 LRU至少需要两个主要操作: 添加(add)和搜索(search) public class LRU ...

  2. Django请求生命周期之响应内容

    Django请求生命周期: 1.发送http请求2.服务器接受,根据请求头中的url在路由关系表中进行匹配(从上到下)3.匹配成功后,执行指定的views函数 URL -> 函数 ==>F ...

  3. 01.css选择器-->类选择器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Web前端面试指导(十六):为什么要初始化CSS样式?

    题目点评 这个题目乍一看感觉怪怪的,什么叫初始化样式了?如果换一句话你可能就理解了,就是通用样式.这道题目主要涉及的是理论方面的知识,不用写代码,只要描述清楚就可以了 初始化样式的原因 因为浏览器的兼 ...

  5. 新安装的Apache和php,测试可以解析phpinfo,但是无法打开drupal网站

    没有去掉php.ini中mysql相关的一些扩展的注释

  6. Vue.js学习(常用指令)

    Vue.js的指令是以v-开头,它们用于HTML元素,指令提供了一些特殊的特性,将指令绑定在元素上时,指令会为绑定的目标元素添加一些特殊的行为,我们可以将指令看作特殊的HTML特性. 本文参考:htt ...

  7. 微服务架构之spring boot admin

    Spring boot admin是可视化的监控组件,依赖spring boot actuator收集各个服务的运行信息,通过spring boot actuator可以非常方便的查看每个微服务的He ...

  8. Android实时获得经纬度,发送给c++服务端

    Android 客户端: package com.example.admin.gpsget; import android.Manifest; import android.content.Conte ...

  9. VUE知识day3_vue插件总结

  10. sequelize 学习之路

    如果你觉得Sequelize的文档有点多.杂,不方便看,可以看看这篇. 在使用NodeJS来关系型操作数据库时,为了方便,通常都会选择一个合适的ORM(Object Relationship Mode ...