- 使用 Java 在 Excel 中创建图表
- 使用 Java 在 Excel 中创建折线图
- 使用Java在Excel中生成金字塔图

为了在 Excel 文件中创建图表,我们将使用
使用 Java 在 Excel 中创建图表
以下是使用 Java 在 Excel 文件中创建图表的步骤。
- 创建Workbook类的实例以加载现有的 Excel 文件或创建新的 Excel 文件。
- 将所需工作表的引用获取到Worksheet对象中。
- 将数据添加到工作表(可选)。
- 通过使用Worksheet.getCharts().add()方法指定其类型来创建一个新图表。
- 将添加的图表的引用获取到Chart对象中。
- 使用Chart.setChartDataRange(String, boolean)方法设置图表的数据。
- 使用Workbook.save(String, SaveFormat)方法保存 Excel 工作簿。
以下代码示例展示了如何使用 Java 在 Excel 中创建图表。
// Create a new workbookWorkbook workbook = new Workbook();// Obtain the reference of the first worksheetWorksheet worksheet = workbook.getWorksheets().get(0);// Add sample values to cellsworksheet.getCells().get("A2").putValue("Category1");worksheet.getCells().get("A3").putValue("Category2");worksheet.getCells().get("A4").putValue("Category3");worksheet.getCells().get("B1").putValue("Column1");worksheet.getCells().get("B2").putValue(4);worksheet.getCells().get("B3").putValue(20);worksheet.getCells().get("B4").putValue(50);worksheet.getCells().get("C1").putValue("Column2");worksheet.getCells().get("C2").putValue(50);worksheet.getCells().get("C3").putValue(100);worksheet.getCells().get("C4").putValue(150);// Add a chart to the worksheetint chartIndex = worksheet.getCharts().add(ChartType.COLUMN, 5, 0, 15, 5);// Access the instance of the newly added chartChart chart = worksheet.getCharts().get(chartIndex);// Set chart data source as the range "A1:C4"chart.setChartDataRange("A1:C4", true);workbook.save("Column-Chart.xlsx", SaveFormat.XLSX);

使用 Java 在 Excel 中创建折线图
以下是使用 Java 在 Excel 中创建折线图的步骤。
- 创建一个Workbook类的实例来加载一个现有的或创建一个新的Excel文件。
- 获取所需工作表的引用到一个工作表对象中。
- 向工作表添加数据(可选)。
- 使用Worksheet.getCharts().add()方法指定ChartType.LINE类型,创建一个新的线形图。
- 在Chart对象中获取所添加图表的引用。
- 使用Chart.setChartDataRange(String, boolean)方法设置图表的数据。
- 使用Workbook.save(String, SaveFormat)方法保存Excel工作簿。
以下代码示例展示了如何使用 Java 在 Excel 中创建折线图。
// Instantiate a Workbook objectWorkbook workbook = new Workbook();// Obtain the reference of the first worksheetWorksheet worksheet = workbook.getWorksheets().get(0);// Add sample values to cellsworksheet.getCells().get("A2").putValue("Category1");worksheet.getCells().get("A3").putValue("Category2");worksheet.getCells().get("A4").putValue("Category3");worksheet.getCells().get("B1").putValue("Column1");worksheet.getCells().get("B2").putValue(4);worksheet.getCells().get("B3").putValue(20);worksheet.getCells().get("B4").putValue(50);worksheet.getCells().get("C1").putValue("Column2");worksheet.getCells().get("C2").putValue(50);worksheet.getCells().get("C3").putValue(100);worksheet.getCells().get("C4").putValue(150);// Add a chart to the worksheetint chartIndex = worksheet.getCharts().add(ChartType.LINE, 5, 0, 15, 5);// Access the instance of the newly added chartChart chart = worksheet.getCharts().get(chartIndex);// Set chart data source as the range "A1:C4"chart.setChartDataRange("A1:C4", true);// Save the Excel fileworkbook.save("Line-Chart.xls", SaveFormat.XLSX);

使用Java在Excel中创建金字塔图
以下是使用 Java 在 Excel 中创建金字塔图的步骤。
- 创建一个Workbook类的实例来加载一个现有的或创建一个新的Excel文件。
- 获取所需工作表的引用到一个工作表对象中。
- 向工作表添加数据(可选)。
- 使用Worksheet.getCharts().add()方法,通过指定ChartType.PYRAMID类型创建一个新的金字塔图表。
- 将添加的图表的参考信息获取为一个Chart对象。
- 使用Chart.getNSeries().add(String, boolean)方法向图表中添加NSeries(图表数据源)。
- 使用Workbook.save(String, SaveFormat)方法保存Excel工作簿。
以下代码示例展示了如何使用 Java 在 Excel 中创建金字塔图。
// Instantiate a Workbook objectWorkbook workbook = new Workbook();// Obtain the reference of the first worksheetWorksheetCollection worksheets = workbook.getWorksheets();Worksheet sheet = worksheets.get(0);// Add some sample value to cellsCells cells = sheet.getCells();Cell cell = cells.get("A1");cell.setValue(50);cell = cells.get("A2");cell.setValue(100);cell = cells.get("A3");cell.setValue(150);cell = cells.get("B1");cell.setValue(4);cell = cells.get("B2");cell.setValue(20);cell = cells.get("B3");cell.setValue(180);cell = cells.get("C1");cell.setValue(320);cell = cells.get("C2");cell.setValue(110);cell = cells.get("C3");cell.setValue(180);cell = cells.get("D1");cell.setValue(40);cell = cells.get("D2");cell.setValue(120);cell = cells.get("D3");cell.setValue(250);// Access chart collectionChartCollection charts = sheet.getCharts();// Add a chart to the worksheetint chartIndex = charts.add(ChartType.PYRAMID, 5, 0, 15, 5);Chart chart = charts.get(chartIndex);// Add NSeries (chart data source) to the chart ranging from "A1"// cell to "B3"SeriesCollection serieses = chart.getNSeries();serieses.add("A1:B3", true);// Save the Excel fileworkbook.save("Pyramid-Chart.xlsx", SaveFormat.XLSX);

如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。
还想要更多吗可以点击阅读【Aspose最新资源在线文库】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询。
标签:
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!