java生成excel甘特图
时间: 2023-08-04 07:17:51 浏览: 46
要生成Excel甘特图,可以使用Java的Apache POI库。以下是一个简单的示例代码:
```java
import java.io.FileOutputStream;
import java.util.Date;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
public class ExcelGanttChartGenerator {
public static void main(String[] args) {
try {
HSSFWorkbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.createSheet("Gantt Chart");
Row headerRow = sheet.createRow(0);
Cell cell = headerRow.createCell(0);
cell.setCellValue("Task Name");
cell = headerRow.createCell(1);
cell.setCellValue("Start Date");
cell = headerRow.createCell(2);
cell.setCellValue("End Date");
Row row1 = sheet.createRow(1);
row1.createCell(0).setCellValue("Task 1");
row1.createCell(1).setCellValue(new Date(2022, 0, 1));
row1.createCell(2).setCellValue(new Date(2022, 0, 10));
Row row2 = sheet.createRow(2);
row2.createCell(0).setCellValue("Task 2");
row2.createCell(1).setCellValue(new Date(2022, 0, 11));
row2.createCell(2).setCellValue(new Date(2022, 0, 20));
// Apply date format to date cells
CellStyle dateStyle = workbook.createCellStyle();
dateStyle.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("yyyy-MM-dd"));
for (int i = 1; i < 3; i++) {
Cell startDateCell = sheet.getRow(i).getCell(1);
startDateCell.setCellStyle(dateStyle);
Cell endDateCell = sheet.getRow(i).getCell(2);
endDateCell.setCellStyle(dateStyle);
}
// Create a Gantt chart
int rowNum = 4;
for (int i = 1; i < 3; i++) {
Row taskRow = sheet.createRow(rowNum++);
Cell taskCell = taskRow.createCell(0);
taskCell.setCellValue(sheet.getRow(i).getCell(0).getStringCellValue());
Cell startDateCell = sheet.getRow(i).getCell(1);
Cell endDateCell = sheet.getRow(i).getCell(2);
long startDate = startDateCell.getDateCellValue().getTime();
long endDate = endDateCell.getDateCellValue().getTime();
long duration = endDate - startDate;
for (int j = 1; j < 365; j++) {
Row chartRow = sheet.getRow(rowNum - 1);
if (chartRow == null) {
chartRow = sheet.createRow(rowNum - 1);
}
Cell chartCell = chartRow.createCell(j);
if (startDate <= DateUtil.getExcelDate(new Date(j)) && DateUtil.getExcelDate(new Date(j)) <= endDate) {
if (j == 1) {
chartCell.setCellValue("X");
} else {
chartCell.setCellValue("");
}
chartCell.setCellStyle(getCellStyle(workbook, duration));
} else {
chartCell.setCellValue("");
}
}
}
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("GanttChart.xls");
workbook.write(fileOut);
fileOut.close();
workbook.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private static CellStyle getCellStyle(HSSFWorkbook workbook, long duration) {
CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(HSSFCellStyle.LESS_DARK_GREEN.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFCellStyle.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFCellStyle.BLACK.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFCellStyle.BLACK.index);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setTopBorderColor(HSSFCellStyle.BLACK.index);
style.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("[Green]0.0%"));
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
style.setWrapText(true);
style.setIndention((short) 1);
style.setRotation((short) 0);
style.setFont(workbook.createFont());
style.getFont(workbook).setFontHeightInPoints((short) 10);
style.getFont(workbook).setFontName("Arial");
style.getFont(workbook).setBoldweight((short) 700);
style.getFont(workbook).setCharSet((byte) 0);
style.getFont(workbook).setColor(HSSFCellStyle.LESS_DARK_GREEN.index);
return style;
}
}
```
这个示例代码将生成包含两个任务的甘特图,日期范围为2022年1月1日至2022年1月20日。你需要将其调整为你的要求,并运行该代码。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)