可视化项目时间线有助于团队保持一致并按时完成任务。 Conholdate.Total for Java 提供了强大的 SDK,使得仅用几行代码就能在 Java 中轻松创建甘特图。 在本指南中,您将了解如何设置库、在工作表中填充任务数据、将堆叠条形图转换为甘特样式的时间线,以及将结果导出为 Excel。
设置 Conholdate.Total for Java
在开始编写代码之前,请确保您已安装 Java 17 或更高版本以及兼容 Maven 的 IDE。将 Conholdate Maven 仓库和 SDK 依赖添加到您的 pom.xml:
<repositories>
<repository>
<id>conholdate-repo</id>
<name>Conholdate Maven Repository</name>
<url>https://repository.conholdate.com/repo/</url>
</repository>
</repositories>
<dependency>
<groupId>com.conholdate</groupId>
<artifactId>conholdate-total</artifactId>
<version>24.9</version>
<type>pom</type>
</dependency>
您也可以直接从下载页面下载最新的 JAR 包。将 SDK 添加到类路径后,您就可以开始构建图表了。
逐步构建:在 Java 中创建甘特图
步骤 1:创建工作簿并填充任务数据
以这种方式构建的甘特图实际上是一个堆叠条形图,叠加在工作表数据之上,因此第一步是创建 Workbook,获取第一个 Worksheet,并填写任务名称、开始偏移量和持续时间,图表将从中读取这些数据。
com.aspose.cells.Workbook workbook = new com.aspose.cells.Workbook();
com.aspose.cells.Worksheet sheet = workbook.getWorksheets().get(0);
com.aspose.cells.Cells cells = sheet.getCells();
cells.get("A1").putValue("Task");
cells.get("C1").putValue("Duration (Days)");
cells.get("D1").putValue("Start Offset (Days)");
cells.get("A2").putValue("Requirement Analysis");
cells.get("C2").putValue(5);
cells.get("D2").putValue(0);
cells.get("A3").putValue("Design");
cells.get("C3").putValue(7);
cells.get("D3").putValue(5);
cells.get("A4").putValue("Implementation");
cells.get("C4").putValue(24);
cells.get("D4").putValue(12);
cells.get("A5").putValue("Testing");
cells.get("C5").putValue(15);
cells.get("D5").putValue(36);
cells.get("A6").putValue("Deployment");
cells.get("C6").putValue(2);
cells.get("D6").putValue(51);
列 A 保存任务名称,列 C 保存每个任务所需的天数,列 D 保存每个任务在项目开始后多少天开始。图表的两个系列(开始偏移和持续时间)直接从列 D 和 C 读取。
第2步:添加堆叠条形图并配置系列
在数据就绪后,向工作表添加一个 BAR_STACKED 图表并连接其系列。起始偏移系列将每个任务条向右移动;持续时间系列在其上绘制可见的任务条。
int chartIndex = sheet.getCharts().add(com.aspose.cells.ChartType.BAR_STACKED, 7, 0, 30, 10);
com.aspose.cells.Chart chart = sheet.getCharts().get(chartIndex);
chart.getTitle().setText("Gantt Chart");
// Add offset and duration as series
chart.getNSeries().add("D2:D6", true); // Start Offset
chart.getNSeries().add("C2:C6", true); // Duration
// Set task names as categories
chart.getNSeries().setCategoryData("A2:A6");
该图表锚定在工作表的第7行、第0列,跨越30行和10列。
第 3 步:将图表样式设置为甘特图外观
将堆叠条形图呈现为甘特图的技巧是将偏移系列设为不可见,这样只有持续时间条显示,并且漂浮在正确的起始位置。
// Make offset bars invisible
chart.getNSeries().get(0).getArea().setFormatting(com.aspose.cells.FormattingType.NONE);
// Style duration bars
chart.getNSeries().get(1).getArea().setFormatting(com.aspose.cells.FormattingType.CUSTOM);
chart.getNSeries().get(1).getArea().setForegroundColor(com.aspose.cells.Color.getSteelBlue());
chart.getNSeries().get(1).getDataLabels().setShowValue(true);
步骤 4:配置坐标轴、图例和大小
将类别轴反转,使第一个任务显示在顶部,给两个轴添加标签,并关闭图例,因为只有一个有意义的系列可供读取。
// Axis settings
chart.getCategoryAxis().setPlotOrderReversed(true);
chart.getCategoryAxis().getTitle().setText("Tasks");
chart.getValueAxis().getTitle().setText("Days from Start");
chart.getValueAxis().getMajorGridLines().setVisible(true);
// Hide legend
chart.setShowLegend(false);
// Resize chart
chart.getChartObject().setWidth(700);
chart.getChartObject().setHeight(400);
步骤 5:保存工作簿
最后,保存工作簿。SDK 支持 XLSX、XLS,以及 SaveFormat 枚举中列出的其他格式。
// Save the result
workbook.save("GanttChartResult.xlsx", com.aspose.cells.SaveFormat.XLSX);
在 Excel(或任何兼容的电子表格应用程序)中打开 GanttChartResult.xlsx,即可查看嵌入工作表的完成甘特图。
完整代码示例:甘特图 Java 示例
以下示例演示了如何在 Java 中从头到尾创建甘特图。
public class GanttChartDemo {
public static void main(String[] args) {
// Create the workbook and get the first worksheet
com.aspose.cells.Workbook workbook = new com.aspose.cells.Workbook();
com.aspose.cells.Worksheet sheet = workbook.getWorksheets().get(0);
// Populate task names, durations, and start offsets
com.aspose.cells.Cells cells = sheet.getCells();
cells.get("A1").putValue("Task");
cells.get("C1").putValue("Duration (Days)");
cells.get("D1").putValue("Start Offset (Days)");
cells.get("A2").putValue("Requirement Analysis");
cells.get("C2").putValue(5);
cells.get("D2").putValue(0);
cells.get("A3").putValue("Design");
cells.get("C3").putValue(7);
cells.get("D3").putValue(5);
cells.get("A4").putValue("Implementation");
cells.get("C4").putValue(24);
cells.get("D4").putValue(12);
cells.get("A5").putValue("Testing");
cells.get("C5").putValue(15);
cells.get("D5").putValue(36);
cells.get("A6").putValue("Deployment");
cells.get("C6").putValue(2);
cells.get("D6").putValue(51);
// Add a stacked bar chart that will act as the Gantt chart
int chartIndex = sheet.getCharts().add(com.aspose.cells.ChartType.BAR_STACKED, 7, 0, 30, 10);
com.aspose.cells.Chart chart = sheet.getCharts().get(chartIndex);
chart.getTitle().setText("Gantt Chart");
// Add offset and duration as series
chart.getNSeries().add("D2:D6", true); // Start Offset
chart.getNSeries().add("C2:C6", true); // Duration
// Set task names as categories
chart.getNSeries().setCategoryData("A2:A6");
// Make offset bars invisible
chart.getNSeries().get(0).getArea().setFormatting(com.aspose.cells.FormattingType.NONE);
// Style duration bars
chart.getNSeries().get(1).getArea().setFormatting(com.aspose.cells.FormattingType.CUSTOM);
chart.getNSeries().get(1).getArea().setForegroundColor(com.aspose.cells.Color.getSteelBlue());
chart.getNSeries().get(1).getDataLabels().setShowValue(true);
// Axis settings
chart.getCategoryAxis().setPlotOrderReversed(true);
chart.getCategoryAxis().getTitle().setText("Tasks");
chart.getValueAxis().getTitle().setText("Days from Start");
chart.getValueAxis().getMajorGridLines().setVisible(true);
// Hide legend
chart.setShowLegend(false);
// Resize chart
chart.getChartObject().setWidth(700);
chart.getChartObject().setHeight(400);
// Save the result
workbook.save("GanttChartResult.xlsx", com.aspose.cells.SaveFormat.XLSX);
}
}
注意: 此代码示例演示了核心功能。在将其用于项目之前,请确保更新所有文件路径和配置值以匹配实际环境,验证已正确安装所有必需的依赖项,并在开发环境中彻底测试。如果遇到任何问题,请参阅官方文档或联系支持团队获取帮助。
结论
使用 Conholdate.Total for Java 在 Java 中创建甘特图变得简单。通过在工作表中填充任务数据、添加堆叠条形图并设置其系列和坐标轴的样式,您可以生成专业外观的时间线,直接嵌入 Excel 文件中。隐藏起始偏移系列并为持续时间系列设置样式是将普通堆叠条形图转换为甘特图的关键技术。请记住,生产部署需要商业许可证;您可以在临时许可证页面获取临时许可证,或在定价页面查看定价详情。立即开始将甘特图集成到您的应用程序中,用清晰的可视化进度表让利益相关者了解最新情况。
常见问题
如何在 Java 中使用 Conholdate.Total 创建甘特图?
创建一个 Workbook,向工作表添加一个 BAR_STACKED 图表,并添加两个系列:一个隐藏的起始偏移系列和一个可见的持续时间系列。将任务名称设置为类别数据,设置可见系列的样式,并保存工作簿。本文提供了完整的代码片段。
我可以将甘特图导出为除 Excel 之外的其他格式吗?
是的。由于图表嵌入在工作簿中,您可以使用 SaveFormat 枚举中的任何值来保存文件,例如 XLSX、XLS 或 ODS。请参阅官方文档获取完整列表。
生产使用需要什么许可证?
商业许可证是强制性的。您可以从临时许可证页面请求临时许可证,并在定价页面查看价格。
如何提升大型甘特图的性能?
尽量将每个系列引用的数据范围保持紧凑,避免不必要的工作簿重新计算,并限制对实际需要的系列和坐标轴的格式设置调用。保持每个图表的任务数量在合理范围内,也能让大型项目文件的渲染保持快速。
