dhtmlxGantt 库支持将甘特图数据导出为 Excel 和 iCal 格式。同时也支持从 Excel 文件导入数据到甘特图中。
该导出服务可免费使用,但在 GPL 许可下生成的 Excel/iCal 文件将包含库的水印。 如果您购买了许可证,在支持有效期内(所有 PRO 许可证为 12 个月),导出文件将不包含水印。
您可以在本地计算机上安装多个导出服务,以便将甘特图导出为 Excel 或 iCal 文件。 请注意,这些导出服务未随 Gantt 包一起提供。 更多详情请参阅相关文档以了解使用条款。
导出服务对处理时间和请求大小有限制。
如果导出过程超过 20 秒,将会被中止,并显示如下错误:
Error: Timeout trigger 20 seconds
如果有多个用户同时导出甘特图,处理时间可能会比平时更长。但每个用户的导出请求时间是单独计算的。
主 API 端点 https://export.dhtmlx.com/gantt 处理所有导出方法(如 exportToPDF、exportToPNG、exportToMSProject 等)。此端点的最大请求大小为 10 MB。
还有一个专用 API 端点 https://export.dhtmlx.com/gantt/project,用于 MSProject 和 Primavera P6 的导出/导入服务(exportToMSProject / importFromMSProject / exportToPrimaveraP6 / importFromPrimaveraP6)。该端点最大请求大小为 40 MB。
如需导出大型甘特图,建议使用独立导出模块。 如果您拥有 Commercial、Enterprise 或 Ultimate 许可证,则该模块免费。否则可以单独购买。
关于 PDF 导出模块的使用详情请参考:PDF 导出模块。该模块支持导出为 PDF、PNG、Excel 和 iCal 格式。
要将甘特图数据导出为 Excel 文件,请执行以下步骤:
gantt.plugins({
export_api: true
});
对于 8.0 之前的 Gantt 版本,请在页面中引入 https://export.dhtmlx.com/gantt/api.js 脚本以启用在线导出服务,例如:
<script src="codebase/dhtmlxgantt.js"></script>
<script src="https://export.dhtmlx.com/gantt/api.js"></script>
<input value="Export to Excel" type="button" onclick='gantt.exportToExcel()'>
<script> gantt.init("gantt_here");
gantt.parse(demo_tasks);
</script>
Related sample: Export data : MS Project, PrimaveraP6, Excel & iCal
Related sample: Export data: store online
exportToExcel() 方法可以接收一个可选对象,包含如下属性:
调用导出方法并使用可选属性的示例
gantt.exportToExcel({
name:"document.xlsx",
columns:[
{ id:"text", header:"Title", width:150 },
{ id:"start_date", header:"Start date", width:250, type:"date" }
],
server:"https://myapp.com/myexport/gantt",
callback: function(res){
alert(res.url);
},
visual:true,
cellColors:true,
data:{},
date_format: "dddd d, mmmm yyyy"
});
导出模块期望 start_date 和 end_date 列为 Date 类型,duration 列为 number 类型。
如果使用了自定义模板,请确保返回值与期望类型一致,或在列配置的 name 字段中指定不同的属性名。例如:
gantt.config.columns = [
...
{name: "start_date", align: "center", width: 100, resize: true,
editor: start_dateEditor},
{name: "end_date", align: "center", width: 100, resize: true,
editor: end_dateEditor},
{name: "duration_formatted", align: "center", width: 40, resize: true,
editor: durationEditor,
template: function(task){ return formatter.format(task.duration_formatted); }
},
...
];
否则,甘特图数据将无法正确导出。相关示例见此。
如需基于自定义数据集(非当前甘特图数据)导出甘特图,请在 exportToExcel 方法参数对象中使用 data 属性:
gantt.exportToExcel({
name:"document.xlsx",
data:[
{id:1, text:"Project #1", start_date:"01-04-2020", duration:18},
{id:2, text:"Task #1", start_date:"02-04-2020",duration:8, parent:1},
{id:3, text:"Task #2", start_date:"11-04-2020",duration:8, parent:1}
]
});
请注意,data 参数应为数据对象,而不是 URL 字符串。
如需在导出的 Excel 文件中包含任务颜色,请将 visual 属性设置为 "base-colors":
gantt.exportToExcel({
visual: "base-colors", cellColors: true
})
由于不支持自动将任意 Excel 列映射到甘特图数据模型,导出服务会将 Excel 文档转换为 JSON 格式的行数组返回。 将该数据转换为甘特图格式需由开发者自行处理。
要转换 Excel 文件,请向导出服务发送如下请求:
请求参数:
表单示例:
<form action="https://export.dhtmlx.com/gantt" method="POST"
enctype="multipart/form-data">
<input type="file" name="file" />
<input type="hidden" name="type" value="excel-parse">
<button type="submit">Get</button>
</form>
或者,您可以使用客户端 API:
gantt.importFromExcel({
server:"https://export.dhtmlx.com/gantt",
data: file,
callback: function(project){
console.log(project)
}
});
Related sample: Import Excel file
这里的 file 为 File 对象,代表一个 Excel (xlsx) 文件。
gantt.importFromExcel 需要支持 HTML5 File API。
响应为对象数组的 JSON 格式:
[
{ "Name": "Task Name", "Start": "2018-08-11 10:00", "Duration": 8 },
...
]
说明:
gantt.importFromExcel({
server:"https://export.dhtmlx.com/gantt",
data: file,
sheet:2, // 处理第三个工作表
callback: function (rows) {}
});
要将甘特图数据导出为 iCal 字符串,请执行以下步骤:
gantt.plugins({
export_api: true
});
<input value="Export to iCal" type="button" onclick='gantt.exportToICal()'>
<script> gantt.init("gantt_here");
gantt.parse(demo_tasks);
</script>
Related sample: Export data : MS Project, PrimaveraP6, Excel & iCal
Related sample: Export data: store online
exportToICal() 方法可以接收一个可选对象,包含如下属性:
调用导出方法并使用可选属性的示例
gantt.exportToICal({
server:"https://myapp.com/myexport/gantt"
});
Back to top