Backend Development 9 min read

Java Implementation of PDF and Excel Generation with Data Insertion and Export

This article provides a step‑by‑step guide on using Java to create PDF templates, fill them with dynamic data via iTextPDF, export the PDFs, and similarly generate Excel files with EasyExcel, including full code examples for service and controller layers.

Top Architect
Top Architect
Top Architect
Java Implementation of PDF and Excel Generation with Data Insertion and Export

Overview

The article describes how to satisfy two common business requirements: (1) generate a PDF from a Word‑based template, insert dynamic data, and provide a download; (2) export queried data as an Excel spreadsheet.

1. PDF Generation

First, a PDF template is created by designing a form in Word and saving it as .pdf . Adobe Acrobat DC is then used to "Prepare Form" and configure the data source, ensuring that the fields in the PDF match the Java entity properties.

Required Maven dependency:

<dependency>
  <groupId>com.itextpdf</groupId>
  <artifactId>itextpdf</artifactId>
  <version>5.5.13</version>
</dependency>

The core controller method exportPdf reads the template, fills fields using AcroFields , sets Chinese fonts, and streams the resulting PDF to the client:

@RegisterToSMP(serviceDisplay = "预览页面PDF下载")
@RequestMapping(value = "/DM/gwclwxsq/qygl/exportPDF$m=query.service", method = RequestMethod.POST)
public String exportPdf(@RequestBody GwclwxsqBean gwclwxsqBean, HttpServletResponse response) throws UnsupportedEncodingException {
    // set parser, file name, response headers
    // read PDF template, create PdfStamper, set fields from gwclwxsqBean
    // flatten form, close resources, return null
}

2. Excel Generation

For the Excel export, the article defines a data‑transfer object ExportYqfkdj with Lombok annotations, then implements a service method that queries data, builds a list of DTOs, and writes the file using EasyExcel:

@Data
public class ExportYqfkdj {
    private Integer xuhao;
    private String xingming;
    // other fields ...
    private String tjsj;
}
@Transactional(rollbackFor = {Exception.class})
public DataResult exporYqfkdj(YqfkdjBean yqfkdjBean) throws Exception {
    // query data, populate ExportYqfkdj objects, build file path
    EasyExcel.write(filepath, ExportYqfkdj.class).head(head()).sheet().doWrite(list);
    // set result info and return
}

The controller method streams the generated .xlsx file to the client, handling response headers and byte buffering:

@RegisterToSMP(serviceDisplay = "疫情防控查询导出")
@RequestMapping(value = "/DM/yqfkdj/gr/yqfkdjdc$m=export.service", method = RequestMethod.POST)
public void exportKhfxxx(@RequestBody YqfkdjBean yqfkdjBean, HttpServletResponse resp) throws Exception {
    DataResult result = yqfkdjService.exporYqfkdj(yqfkdjBean);
    // read file, set headers, write buffer to response
}

3. Testing and Result

Both the PDF and Excel generation, data insertion, and export functionalities are fully tested, with screenshots showing successful file creation and download.

The article concludes that the implementation meets the business needs for dynamic document generation in a Java backend environment.

backendJavapdfeasyexcelexceliTextPDFfile-export
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.