Generate Bar Chart Or Column Chart In Excel Using Apache POI

Bar or Column Chart

In this example, you will see how to generate bar chart or column chart in excel file using Apache POI library in Java. I will show you how to build this application using both maven and gradle tools. This is a standalone application and you can use this concept of bar chart or column chart generation in any Java based application. In this example, I will create bar chart and column chart for displaying area-wise top seven countries.

A bar chart or bar graph is a chart or graph that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent. The bars can be plotted vertically or horizontally. When plotted vertically it’s called column chart.

Related Posts:

Bar charts have a discrete domain of categories, and are usually scaled so that all the data can fit on the chart. When there is no natural ordering of the categories being compared, bars on the chart may be arranged in any order. Bar charts arranged from highest to lowest incidence are called Pareto charts. Bar graphs/charts provide a visual presentation of categorical data.


Java 8+, Apache POI 4.1.1/5.2.2, Gradle 5.6, Maven 3.6.1/3.8.5

Project Setup

The name of the project is apache-poi-excel-bar-column-chart. For the maven based project you can use below pom.xml file.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">




		<!-- required only if you are using jdk 9 or above -->


If you are creating gradle based project, then use below build.gradle script to include the required dependencies.

plugins {
    id 'java-library'

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {
    implementation 'org.apache.poi:poi-ooxml:4.1.1'
    //required only for jdk 9 or above

Create Bar and Column Chart

Now I will create a Java class to create bar chart and column chart in excel file using Apache POI library. The charts will display area-wise top seven countries in the world.

public class ApachePoiBarChart {

	public static void main(String[] args) throws IOException {

	public static void barColumnChart() throws FileNotFoundException, IOException {
		try (XSSFWorkbook wb = new XSSFWorkbook()) {

			String sheetName = "CountryBarChart";//"CountryColumnChart";
			XSSFSheet sheet = wb.createSheet(sheetName);

			// Create row and put some cells in it. Rows and cells are 0 based.
			Row row = sheet.createRow((short) 0);

			Cell cell = row.createCell((short) 0);

			cell = row.createCell((short) 1);

			cell = row.createCell((short) 2);

			cell = row.createCell((short) 3);

			cell = row.createCell((short) 4);

			cell = row.createCell((short) 5);

			cell = row.createCell((short) 6);

			row = sheet.createRow((short) 1);

			cell = row.createCell((short) 0);

			cell = row.createCell((short) 1);

			cell = row.createCell((short) 2);

			cell = row.createCell((short) 3);

			cell = row.createCell((short) 4);

			cell = row.createCell((short) 5);

			cell = row.createCell((short) 6);

			XSSFDrawing drawing = sheet.createDrawingPatriarch();
			XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 4, 7, 20);

			XSSFChart chart = drawing.createChart(anchor);
			chart.setTitleText("Area-wise Top Seven Countries");

			XDDFChartLegend legend = chart.getOrAddLegend();
			XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
            XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);

			XDDFDataSource<String> countries = XDDFDataSourcesFactory.fromStringCellRange(sheet,
					new CellRangeAddress(0, 0, 0, 6));

			XDDFNumericalDataSource<Double> values = XDDFDataSourcesFactory.fromNumericCellRange(sheet,
					new CellRangeAddress(1, 1, 0, 6));
			XDDFChartData data = chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);
            XDDFChartData.Series series1 = data.addSeries(countries, values);
            series1.setTitle("Country", null);
			// in order to transform a bar chart into a column chart, you just need to change the bar direction
            XDDFBarChartData bar = (XDDFBarChartData) data;

			// Write output to an excel file
            String filename = "bar-chart-top-seven-countries.xlsx";//"column-chart-top-seven-countries.xlsx";
			try (FileOutputStream fileOut = new FileOutputStream(filename)) {


I will explain few important lines from the above class.

I have created XSSFWorkbook object and create a sheet with name CountryBarChart or CountryColumnChart.

Next, I have created two rows and 7 columns in the excel sheet. First row contains country names and second row contains each country’s area.

Then, I have created XSSFDrawing object and XSSFClientAnchor object.

In XSSFClientAnchor, I have set the range of the rows and columns or cells. The first two values indicate coordinates of x and y of first cell respectively. The next two values indicate coordinates of x and y of second cell respectively. The next two values indicate starting row index and column index of chart area. The next two values indicate ending row index and column index of chart area.

Now, I have created chart from the XSSFClientAnchor object using createChart(). I have set title to the chart.

The legends are shown on right side of the bar chart or column chart.

Then I create bottom axis and left axis. Bottom axis adds label “Country” and left axis adds label “Area”.

Next I define data source for drawing chart. I set country names as string and area as numeric values.

Then I plot the data onto bar chart or column chart.

Next I need to set direction whether I want it to be bar chart or column chart.

Finally I write the chart into excel file.

Testing Bar or Column Chart

Running the above main class will generate the bar chart and column chart in excel similar to the following images.

Bar Chart

The following image shows how bar chart looks in the excel sheet:

bar chart or column chart in excel using apache poi

Column Chart

The following image shows how column chart looks in the excel sheet:

bar chart or column chart in excel using apache poi

Source Code


4 thoughts on “Generate Bar Chart Or Column Chart In Excel Using Apache POI

  1. What I did to “fix” it was adding this: leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);
    Another question: do you know if there is a way to put the value at the end of the bar? I mean, at the in each bar, I want to put the value (number) accordingly?

  2. Is there a way to change the bars? For example, I need to change the first and the last bars. As you can see, both bars are halfed due the limits of the chart.
    Is there a way to change this?

Leave a Reply

Your email address will not be published. Required fields are marked *