Add Data Table to Excel Chart in Java

In Excel, we could use charts to visualize and compare data. However, once the charts are created, it becomes much difficult for us to read the data precisely from charts, adding a data table below the chart is a good solution. This article is going to introduce how to add a data table to an Excel chart in Java using Spire.XLS for Java.

import com.spire.xls.*;
import com.spire.xls.charts.ChartSerie;

public class AddDataTableToChart {
    public static void main(String[] args){
        //Create a new workbook
        Workbook workbook = new Workbook();
        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);
        //Set sheet name
        sheet.setName("Demo");

        //Add Data to the sheet
        sheet.getRange().get("A1").setValue("Month");
        sheet.getRange().get("A2").setValue("Jan.");
        sheet.getRange().get("A3").setValue("Feb.");
        sheet.getRange().get("A4").setValue("Mar.");
        sheet.getRange().get("A5").setValue("Apr.");
        sheet.getRange().get("A6").setValue("May.");
        sheet.getRange().get("A7").setValue("Jun.");
        sheet.getRange().get("B1").setValue("Peter");
        sheet.getRange().get("B2").setNumberValue(3.3);
        sheet.getRange().get("B3").setNumberValue(2.5);
        sheet.getRange().get("B4").setNumberValue(2.0);
        sheet.getRange().get("B5").setNumberValue(3.7);
        sheet.getRange().get("B6").setNumberValue(4.5);
        sheet.getRange().get("B7").setNumberValue(4.0);
        sheet.getRange().get("C1").setValue("George");
        sheet.getRange().get("C2").setNumberValue(3.8);
        sheet.getRange().get("C3").setNumberValue(3.2);
        sheet.getRange().get("C4").setNumberValue(1.7);
        sheet.getRange().get("C5").setNumberValue(3.5);
        sheet.getRange().get("C6").setNumberValue(4.5);
        sheet.getRange().get("C7").setNumberValue(4.3);
        sheet.getRange().get("D1").setValue("Macbeth");
        sheet.getRange().get("D2").setNumberValue(3.0);
        sheet.getRange().get("D3").setNumberValue(2.8);
        sheet.getRange().get("D4").setNumberValue(3.5);
        sheet.getRange().get("D5").setNumberValue(2.3);
        sheet.getRange().get("D6").setNumberValue(3.3);
        sheet.getRange().get("D7").setNumberValue(3.8);

        //Add a chart to the sheet
        Chart chart = sheet.getCharts().add(ExcelChartType.ColumnClustered);
        //Set chart data
        chart.setDataRange(sheet.getRange().get("B1:D7"));
        chart.setSeriesDataFromRange(false);
        //Set chart position
        chart.setTopRow(8);
        chart.setBottomRow(28);
        chart.setLeftColumn(3);
        chart.setRightColumn(11);
        //Set chart title
        chart.setChartTitle("Chart with Data Table");
        chart.getChartTitleArea().isBold(true);
        chart.getChartTitleArea().setSize(12);
        //Set category labels for the first series of the chart
        ChartSerie cs1 = chart.getSeries().get(0);
        cs1.setCategoryLabels(sheet.getRange().get("A2:A7"));

        //Add data table to the chart
        chart.hasDataTable(true);

        //Save the result file
        workbook.saveToFile("AddDataTable.xlsx", ExcelVersion.Version2010);
    }
}

Output:

Add Data Table to Excel Chart in Java