翻譯|使用教程|編輯:李顯亮|2021-06-24 10:19:29.480|閱讀 320 次
概述:在本文中,將學(xué)習(xí)如何根據(jù) Excel 工作表中提供的數(shù)據(jù)以編程方式生成圖表。特別是,本文介紹了如何使用 Java 在 Excel 中創(chuàng)建不同類型的圖表。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
圖表和圖形用于匯總和直觀地表示數(shù)據(jù)。它們提供了可進一步用于做出決策的洞察力。圖表被認(rèn)為是 Excel 電子表格的一個組成部分,廣泛用于各種應(yīng)用程序。在本文中,將學(xué)習(xí)如何根據(jù) Excel 工作表中提供的數(shù)據(jù)以編程方式生成圖表。特別是,本文介紹了如何使用 Java 在 Excel 中創(chuàng)建不同類型的圖表。
為了在 Excel 文件中創(chuàng)建圖表,我們將使用Aspose.Cells for Java,它是一個強大的 API,允許您在 Java 應(yīng)用程序中實現(xiàn) Excel 自動化功能。此外,它允許您在幾行代碼內(nèi)生成各種圖表。點擊下方按鈕可下載試用。
以下是使用 Java 在 Excel 文件中創(chuàng)建圖表的步驟。
以下代碼示例展示了如何使用 Java 在 Excel 中創(chuàng)建圖表。
// Create a new workbook Workbook workbook = new Workbook(); // Obtain the reference of the first worksheet Worksheet worksheet = workbook.getWorksheets().get(0); // Add sample values to cells worksheet.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 worksheet int chartIndex = worksheet.getCharts().add(ChartType.COLUMN, 5, 0, 15, 5); // Access the instance of the newly added chart Chart 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 中創(chuàng)建折線圖的步驟。
以下代碼示例展示了如何使用 Java 在 Excel 中創(chuàng)建折線圖。
// Instantiate a Workbook object Workbook workbook = new Workbook(); // Obtain the reference of the first worksheet Worksheet worksheet = workbook.getWorksheets().get(0); // Add sample values to cells worksheet.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 worksheet int chartIndex = worksheet.getCharts().add(ChartType.LINE, 5, 0, 15, 5); // Access the instance of the newly added chart Chart chart = worksheet.getCharts().get(chartIndex); // Set chart data source as the range "A1:C4" chart.setChartDataRange("A1:C4", true); // Save the Excel file workbook.save("Line-Chart.xls", SaveFormat.XLSX);
以下是使用 Java 在 Excel 中創(chuàng)建金字塔圖的步驟。
以下代碼示例展示了如何使用 Java 在 Excel 中創(chuàng)建金字塔圖。
// Instantiate a Workbook object Workbook workbook = new Workbook(); // Obtain the reference of the first worksheet WorksheetCollection worksheets = workbook.getWorksheets(); Worksheet sheet = worksheets.get(0); // Add some sample value to cells Cells 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 collection ChartCollection charts = sheet.getCharts(); // Add a chart to the worksheet int 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 file workbook.save("Pyramid-Chart.xlsx", SaveFormat.XLSX);
如果你想試用Aspose的全部完整功能,可聯(lián)系在線客服獲取30天臨時授權(quán)體驗。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn