How to Export Excel Charts to PNG or PDF in Java

Turn an editable Excel chart into an image, a chart-only PDF, or a complete report.

An Excel chart is useful inside a workbook, but a dashboard, email, or presentation often needs a picture or PDF. Axyra Sheets, a Java library for reading, writing, and processing Excel files, can render the chart directly from the workbook without Microsoft Excel installed.

This guide opens an existing sales chart comparing actual revenue with targets and produces three files: a PNG of the chart, a PDF containing the chart, and a PDF containing the source table and chart. Review the source and outputs, run the sample project, then explore the export calls.

TL;DR

  • Use Sheet.renderChartToPng(chartIndex, width, height) to export a single chart at a chosen pixel size.
  • Use a worksheet print area and Workbook.renderPdf(...) to choose between a chart-only PDF and a complete report page.
  • Keep the source data in the workbook, select the sheet explicitly, and save the exports to separate files.

Table of contents

Export results: PNG and PDF

This guide exports an existing chart in an Excel workbook. If your workbook does not have one yet, use the chart creation guide or generate the sample workbook with the commands below.

Need to create the chart first? Create editable Excel charts in Java

The source file, sales-chart.xlsx, contains a Sales worksheet with actual and target revenue in A1:C5 and a column chart comparing the four regions. CreateExcelCharts generates this file; the next section shows how to obtain and run the project.

Source Sales worksheet with actual and target revenue and its existing Excel chart

Source Sales worksheet from sales-chart.xlsx, with chart data labels and the value-axis title hidden for this preview. The table and chart retain their original worksheet positions.

ExportChartFormats opens this workbook and writes the three outputs below. It adjusts the chart position in memory for PDF rendering and leaves the original XLSX file unchanged.

Output What it contains Typical use
sales-chart.png The chart alone, 1200 × 700 pixels Dashboard, email, presentation
sales-chart-only.pdf One portrait page containing the chart Printable chart or attachment
sales-report.pdf One portrait page containing the table and chart Business report

Chart PNG — 1200 × 700

Excel chart exported as a 1200 by 700 PNG using Java

PNG exported directly from the workbook: blue columns show actual revenue; orange columns show targets.

Chart-only PDF

The chart-only PDF excludes the source table. This preview is cropped to show the chart.

Chart-only PDF exported from an Excel workbook using Java

Actual sales-chart-only.pdf output, cropped to show the chart.

Complete report PDF

Include the source table above the chart to create a complete PDF report.

PDF report containing the source sales table and Excel chart

Actual sales-report.pdf output, cropped to show the table and chart. The downloadable PDF retains the full portrait page.

Download the PNG and both PDF outputs

Run the complete export example

Use Java 17 or later and Maven. Clone the sample project and run the two programs in order:

Sample project

git clone https://github.com/keikai/axyra-sheets-examples.git
cd axyra-sheets-examples/java/guide-projects/create-charts
mvn compile exec:java -Dexec.mainClass=CreateExcelCharts
mvn exec:java -Dexec.mainClass=ExportChartFormats

CreateExcelCharts writes sales-chart.xlsx. ExportChartFormats opens that file and writes the PNG and both PDF layouts into output/chart-export/. It does not overwrite the source workbook.

Complete Java source: ExportChartFormats.java

The sample program assumes a Sales worksheet, the first chart on that sheet, and the table and chart layout shown above. With those assumptions, pass a different input path and output directory as follows:

mvn exec:java -Dexec.mainClass=ExportChartFormats \
  -Dexec.args="sales-chart.xlsx output/custom-export"

For an existing application, add the library through the installation guide. Examples run in evaluation mode unless you have configured an Axyra license.

Installation and dependency setup

Export an Excel chart to PNG in Java

Open the source workbook, select its Sales worksheet, and export the first chart. Chart indices are zero-based, so index 0 selects that chart:

try (Workbook workbook = Workbook.open(Path.of("sales-chart.xlsx"))) {
    Sheet sales = workbook.sheet("Sales");
    sales.chart(0).setShowDataLabels(false);
    byte[] png = sales.renderChartToPng(0, 1200, 700);
    Files.write(Path.of("sales-chart.png"), png);
}

The result contains the chart without the surrounding worksheet cells. For a different workbook, inspect sales.charts() and choose the intended chart before exporting. The linked Java source includes the imports and code to open and close the workbook.

Choose image dimensions for the destination, then check that the chart labels remain readable at the displayed size. This example uses 1200 × 700 pixels for four categories and a legend.

Export a chart to PDF in Java

To demonstrate both PDF layouts, the example positions the chart below the source table. The chart-only PDF includes only the chart area; the complete report includes both the table and chart. Both layouts use Workbook.renderPdf(). Moving the chart is a layout choice for this example, not a requirement for exporting a chart.

Open the source workbook again and apply the shared settings below. Replace the comment with one or both export snippets from the following subsections. Both snippets use this same open workbook and Sales sheet; they are not standalone programs.

try (Workbook workbook = Workbook.open(Path.of("sales-chart.xlsx"))) {
    Sheet sales = workbook.sheet("Sales");
    sales.chart(0).setAnchor(7, 0, 23, 7);
    sales.chart(0).setShowDataLabels(false);
    sales.setLandscape(false);
    sales.fitToPage(1, 1);
    PdfOptions pdf = PdfOptions.builder().sheetNames("Sales").build();
    // Add the PDF export calls shown below here.
}

The anchor uses zero-based row and column indices, placing the chart at A8:H24. These changes affect the workbook in memory; the original XLSX file is not saved or overwritten.

Chart-only PDF

Inside the configured workbook above, print only the chart area:

sales.setPrintArea("A8:H24");
workbook.renderPdf(Path.of("sales-chart-only.pdf"), pdf);

PDF report with the table and chart

For the complete report, use the same configured workbook and expand the print area to include the source table:

sales.setPrintArea("A1:H24");
workbook.renderPdf(Path.of("sales-report.pdf"), pdf);

sheetNames("Sales") selects the sheet to render. fitToPage(1, 1) keeps this compact example on one page. For a larger report, choose pagination that keeps text readable instead of squeezing every row onto a single page.

Keep labels and layout readable

Use explicit series colors when you want the same palette across outputs. This example hides numeric data labels in all exports and uses the axes and legend to explain the comparison.

Check the chart at the intended display or print size. Leave enough space for the title, legend, and axis labels, and make the fonts used by your report available in the deployment environment. Use the rendering guide for font directories and additional page options.

If you change formula-based chart inputs in manual mode, or open a workbook with outdated formula results, call workbook.recalculate() after those changes and before rendering. The supplied sample uses numeric source values.

PDF page setup and rendering options

FAQ

Does Java need Excel installed to export a chart?

No. Axyra Sheets renders the chart in the Java process, so the same code can run in a server-side report job.

Does the PNG or PDF keep the chart editable?

PNG and PDF exports do not preserve an editable Excel chart. Keep the original XLSX if recipients need to edit the chart or its source data.

Can I export a chart from an existing workbook?

Yes. Open the workbook, select the sheet and chart, then render it. Adjust the chart index and PDF print area to that workbook’s layout.

Export charts from your own Excel workbooks

Already have an Excel workbook with charts? Run the sample project, then adapt ExportChartFormats to your file:

  • Set the source path to your XLSX file and choose an output directory.
  • Replace Sales with your worksheet name.
  • Choose the chart index to export; use sheet.charts() to inspect the available charts.
  • Set each PDF print area to cover the chart, or the table and chart.
  • Keep the existing chart position if it suits your report, or adjust setAnchor() before PDF rendering.

Run the chart export example

Create editable Excel charts in Java

Get code at GitHub