Excel 的导出/导入,iCal 的导出
dhtmlxGantt 库允许将甘特图数据导出为 Excel 和 iCal 格式。你也可以从 Excel 文件将数据导入到甘特图中。
该服务是免费的,但输出的 Excel/iCal 文件在 GPL 许可下会包含本库的水印。若购买许可,在有效的技术支持期内(所有 PRO 许可证为 12 个月),导出的结果将不带水印。
有多种导出服务可用。你可以在本地计算机上安装它们,并将甘特图导出为 Excel 或 iCal。本地导出服务并不包含在 Gantt 包中,请阅读 相应文章 以了解每种服务的使用条款。
在线导出服务的限制
导出服务对时间和请求大小有限制。
时间限制
如果处理时间超过 20 秒,导出将被取消,并出现以下错误:
Error: Timeout trigger 20 seconds
若多人同时导出甘特图,处理时间可能比平时长一些。但这也没关系,因为针对某个用户的导出请求所花费的时间是分开统计的。
请求大小限制
存在一个通用的 API 端点 https://export.dhtmlx.com/gantt,用于所有导出方法(exportToPDF、exportToPNG、exportToMSProject 等)。最大请求大小为 10 MB。
另有一个专用端点 https://export.dhtmlx.com/gantt/project,专用于 MSProject 与
Primavera P6 的导出/导入服务(仅有 exportToMSProject / importFromMSProject / exportToPrimaveraP6 / importFromPrimaveraP6)。最大请求大小:40 MB。
使用导出模块
如果你需要导出较大图表,可以使用一个 standalone export module。 如果你在 Commercial、Enterprise 或 Ultimate 授权下获得了 Gantt,导出模块可免费使用,或你也可以 单独购买模块。
更多关于导出模块用于 PDF 的用法 的信息。该导出模块可将数据导出为 PDF、PNG、Excel 及 iCal 文件。
导出到 Excel
要将甘特图的数据导出到 Excel 文档,请执行以下操作:
- 要使用导出/导入功能,请通过 plugins 方法启用 export_api 插件:
gantt.plugins({
export_api: true
});
它允许 你使用在线导出服务或本地导出模块。
如果你使用的 Gantt 版本低于 8.0,需要在页面中包含 https://export.dhtmlx.com/gantt/api.js 以启用导出功能,例如:
<script src="codebase/dhtmlxgantt.js"></script>
<script src="https://export.dhtmlx.com/gantt/api.js"></script>
- 调用 exportToExcel 方法以从 Gantt 图导出数据到 Excel:
<input value="Export to Excel" type="button" onclick='gantt.exportToExcel()'/>
<script>
gantt.init("gantt_here");
gantt.parse(demo_tasks);
</script>
相关示例: Export data : MS Project, PrimaveraP6, Excel & iCal
相关示例: Export data: store online
导出方法的参数
exportToExcel() 方法的参数是一个包含若干属性的对象(所有属性均为可选):
- name - (string) 设置输出文件的名称,扩展名为 '.xlsx'
- columns - (array) 允许配置输出的 Excel 工作表列。列对象的属性包括:
- 'id' - (string,number) 将映射到该列的事件属性
- 'header' - (string) 列头
- 'width' - (number) 列宽(像素)
- 'type' - (string) 列类型
- server - (string) 设置请求的 API 端点。可用于本地安装的导出服务。默认值为
https://export.dhtmlx.com/gantt - callback - (function) 如果你希望收到用于下载生成的 XLSX 文件的 URL,可以使用 callback 属性。它会接收一个具有 url 属性的 JSON 对象
- visual - (boolean) 在导出的 Excel 文档中添加时间线图。默认值为 false
- cellColors - (boolean) 如果设为 true,导出文档的单元格将具有由 timeline_cell_class 模板定义的颜色,导出 color 与 background-color 属性
- data - (object) 设置在导出的甘特图中展示的自定义数据源
- date_format - (string) 设置在导出的 Excel 文档中日期的显示格式。你可以在这里看到可用格式代码的完整列表 这里。
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: (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: (task) => {
return formatter.format(task.duration_formatted);
}
},
...
];
否则,Gantt 数据将无法导出。 查看相关示例。
设置自定义数据源进行导出
要使用自定义数据集导出甘特图(即不使用初始甘特图中显示的数据),请在 exportToExcel 方法的参数中使用 data 属性:
gantt.exportToExcel({
name: "document.xlsx",
data: [
{ id: 1, text: "Project #1", start_date: "01-04-2026", duration: 18},
{ id: 2, text: "Task #1", start_date: "02-04-2026", duration: 8, parent: 1},
{ id: 3, text: "Task #2", start_date: "11-04-2026", duration: 8, parent: 1}
]
});
请注意,不能将 URL 作为 data 参数的值来使用,只能使用数据对象。
导出任务颜色
你可以通过将 exportToExcel 的 visual 属性设为 "base-colors",将任务颜色导出到 Excel 文件中:
gantt.exportToExcel({
visual: "base-colors",
cellColors: true
})
相关示例: Export colors of tasks