Bu kısa eğitimde, adım adım bir yaklaşımı izleyerek Java’da Excel grafiği oluşturmayı öğreneceksiniz. Programlı olarak bir çalışma kitabı oluşturmak ve ardından verileri manuel olarak doldurmak veya bir grafik oluşturmak için bazı harici kaynaklardan veri almak çok yaygındır. İşlemin sonunda bu Excel dosyası bir diske XLSX dosyası olarak kaydedilir.
Java’da Excel Grafiği Oluşturma Adımları
- İlk olarak, Maven deposunu kullanarak projedeki Aspose.Cells kitaplığına bir referans ekleyin
- Programda İçe Aktar’ı kullanarak Workbook, Worksheet ve diğer gerekli sınıflara bir referans ekleyin
- Varsayılan olarak bir çalışma sayfasına sahip boş bir çalışma kitabı oluşturun
- İlk sayfaya bir referans alın ve grafikte oluşturulacak verileri doldurun
- İlk sayfada, ilk ayarlarla bir pasta grafik diyelim ki bir grafik oluşturun
- Her pasta grafiği diliminin grafik veri serisini, kategorisini, grafik başlığını ve veri etiketlerini ayarlayın
- Veri ve grafik içeren çalışma kitabını kaydedin
Yukarıdaki adımları kullanarak, yalnızca bir çalışma kitabı oluşturarak ve ardından grafik için örnek verileri doldurarak bir Excel grafiği oluşturabilirsiniz. Veriler doldurulduktan sonra, temel ayarlarla bir pasta grafiği oluşturulur ve ardından her dilimin grafik veri serisi, kategorisi, başlığı ve veri etiketleri eklenir. Aşağıdaki örnek kodda gösterildiği gibi, bu grafiğin diğer birçok özelliğini ayarlayabilirsiniz.
Java’da Excel’de Grafik Oluşturma Kodu
import com.aspose.cells.License; | |
import com.aspose.cells.Cells; | |
import com.aspose.cells.Chart; | |
import com.aspose.cells.ChartType; | |
import com.aspose.cells.Color; | |
import com.aspose.cells.DataLabels; | |
import com.aspose.cells.Workbook; | |
import com.aspose.cells.Worksheet; | |
public class CreateExcelChartInJava { | |
public static void main(String[] args) throws Exception { //main function for CreateExcelChartInJava | |
// Initialize a license to avoid trial version watermark while creating Excel chart | |
License license = new License(); | |
license.setLicense("Aspose.Cells.lic"); | |
// Create an empty Excel Workbook | |
Workbook ExcelWorkbookForChart = new Workbook(); | |
// Get reference to the first worksheet for creating chart | |
Worksheet ExcelChartWorksheet = ExcelWorkbookForChart.getWorksheets().get(0); | |
// Set sheet name | |
ExcelChartWorksheet.setName("PieChart"); | |
// Get worksheet cells collection to set values | |
Cells WorksheetCells = ExcelChartWorksheet.getCells(); | |
// Set values in the cells to create a pie chart | |
WorksheetCells.get("A1").putValue("Quarters"); | |
WorksheetCells.get("A2").putValue("1st_Qtr"); | |
WorksheetCells.get("A3").putValue("2nd_Qtr"); | |
WorksheetCells.get("A4").putValue("3rd_Qtr"); | |
WorksheetCells.get("A5").putValue("4th_Qtr"); | |
WorksheetCells.get("B1").putValue("Sales"); | |
WorksheetCells.get("B2").putValue(6.3); | |
WorksheetCells.get("B3").putValue(3.1); | |
WorksheetCells.get("B4").putValue(2.2); | |
WorksheetCells.get("B5").putValue(1.9); | |
// Create a Pie chart and get its reference for setting chart properties | |
int chart_Index = 0; | |
chart_Index = ExcelChartWorksheet.getCharts().add(ChartType.PIE, 10, 2, 34, 13); | |
Chart WorksheetChart = ExcelChartWorksheet.getCharts().get(chart_Index); | |
// Set the chart data series and category | |
WorksheetChart.getNSeries().add("B2:B5", true); | |
WorksheetChart.getNSeries().setCategoryData("A2:A5"); | |
// Set properties of chart title | |
WorksheetChart.getTitle().setText("Sales By Quarter"); | |
WorksheetChart.getTitle().getFont().setColor(Color.getBlue()); | |
WorksheetChart.getTitle().getFont().setBold(true); | |
WorksheetChart.getTitle().getFont().setSize(11); | |
// Set the data labels of each pie chart slice | |
DataLabels data_labels; | |
for (int i = 0; i < WorksheetChart.getNSeries().getCount(); i++) | |
{ | |
data_labels = WorksheetChart.getNSeries().get(i).getDataLabels(); | |
data_labels.setShowValue(true); | |
data_labels.setShowPercentage(true); | |
} | |
// Save the workbook containing the chart | |
ExcelWorkbookForChart.save("pie_chart.xlsx"); | |
} | |
} |
Bu örnek kodda, Java’da Excel’de grafik oluşturmayı ve XLSX dosyası olarak kaydetmeyi öğrendik. Ancak, bu grafiği bir resme dönüştürmek istiyorsanız, Java’da Excel grafiğini JPG’ye dönüştürme makalesine bakın.
Yukarıdaki kodu çalıştırmak için Interop veya MS Excel gibi başka bir bileşen veya kitaplık gerekmediğini unutmayın.