java导出excel甘特图GanttView
时间: 2023-08-31 18:06:37 浏览: 47
Java中可以使用Apache POI库来导出Excel表格,同时使用JavaFX的甘特图来生成数据。以下是一个示例代码:
```java
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.StackedBarChart;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class GanttViewExportToExcel extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// Create a category axis for the horizontal axis
CategoryAxis xAxis = new CategoryAxis();
xAxis.setLabel("Tasks");
// Create a number axis for the vertical axis
NumberAxis yAxis = new NumberAxis();
yAxis.setLabel("Time");
// Create a stacked bar chart
StackedBarChart<String, Number> chart = new StackedBarChart<>(xAxis, yAxis);
// Add the data to the chart
XYChart.Series<String, Number> series1 = new XYChart.Series<>();
series1.setName("Task 1");
series1.getData().add(new XYChart.Data<>("Start", 0));
series1.getData().add(new XYChart.Data<>("End", 5));
XYChart.Series<String, Number> series2 = new XYChart.Series<>();
series2.setName("Task 2");
series2.getData().add(new XYChart.Data<>("Start", 1));
series2.getData().add(new XYChart.Data<>("End", 6));
XYChart.Series<String, Number> series3 = new XYChart.Series<>();
series3.setName("Task 3");
series3.getData().add(new XYChart.Data<>("Start", 2));
series3.getData().add(new XYChart.Data<>("End", 7));
chart.getData().addAll(series1, series2, series3);
// Create a border pane to hold the chart
BorderPane root = new BorderPane();
root.setCenter(chart);
// Create a scene for the chart and add it to the stage
Scene scene = new Scene(root, 600, 400);
primaryStage.setScene(scene);
// Export chart data to Excel
exportToExcel(chart);
primaryStage.show();
}
private void exportToExcel(StackedBarChart<String, Number> chart) {
// Create a new Excel workbook
Workbook workbook = new XSSFWorkbook();
// Create a new sheet in the workbook
Sheet sheet = workbook.createSheet("Gantt Chart");
// Add the data to the sheet
int rowIndex = 0;
for (XYChart.Series<String, Number> series : chart.getData()) {
Row row = sheet.createRow(rowIndex++);
row.createCell(0).setCellValue(series.getName());
CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
for (XYChart.Data<String, Number> data : series.getData()) {
Cell cell = row.createCell(data.getXValue().hashCode() % 65536);
cell.setCellValue(data.getYValue().doubleValue());
cell.setCellStyle(style);
}
}
// Resize the columns in the sheet
sheet.autoSizeColumn(0);
for (int i = 1; i <= 100; i++) {
sheet.setColumnWidth(i, 1000);
}
// Write the workbook to a file
try (FileOutputStream outputStream = new FileOutputStream("GanttChart.xlsx")) {
workbook.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
```
这个示例代码会创建一个包含三个任务的甘特图,并将数据导出到Excel表格中。你可以根据需要修改数据和文件名来创建自己的甘特图。