Open Source Java Library for Microsoft® Spreadsheet Documents

Create, Read, Edit and Convert Microsoft Excel Spreadsheet files in Java applications via Open Source API.

What is DOCX4J?

DOCX4J is similar to Microsoft's OpenXML SDK, but for Java. DOCX4J is JAXB-based open source(Apache v2) library for manipulating Microsoft Office file formats. It provides the functionality to read, write, edit & and save XLSX file format.

Using the API you can generate Spreadsheet documents, edit them, format the text & paragraphs, insert charts, insert tables & images and manage other form elements, and much more. Basically, its emphasis is on power, if the format supports it you can do it using the API.

Previous Next

Getting Started with DOCX4J

First of all, you need to have the Java Development Kit (JDK) installed on your system. Referencing DOCX4J in your Maven-based Java project is even simpler. All you need is to add the following dependency in your pom.xml and let your IDE fetch and reference the DOCX4J Jar files.

DOCX4J Maven Dependency

<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-Internal</artifactId>
<version>8.0.0</version>
</dependency>

<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
<version>8.0.0</version>
</dependency>

<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-MOXy</artifactId>
<version>8.0.0</version>
</dependency>
    

Java Library to Read & Write XLSX

It is a powerful library to Create & Manipulate existing as well as new XLSX file formats. It enables the developers to access and read data from a specific sheet inside a spreadsheet. Usually, a spreadsheet contains several worksheets. If a user is interested to read data from only one sheet and skip the other sheets. By using the following steps, you can create Microsoft Excel in Java

Create Excel Easily

  1. Initialize object of SpreadsheetMLPackage
  2. Create worksheet
  3. Get sheet data
  4. Save document

Create Excel Free using Java

// Create spreadsheet package
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
// Create worksheet
WorksheetPart sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
SheetData sheetData = sheet.getContents().getSheetData();
// Save
pkg.save(new File("FileFormat.xlsx"));                
                  

Work with Spreadsheet Cells using Java API

DOCX4J allows you to access the cell and set its value by coordinates. You can also create a new cell and set up a formula in it. You can also configure the cell to occupy various types of data, such as date, time, and number with leading zeros.

Add Content in Excel Cell - Java

// Create spreadsheet package
SpreadsheetMLPackage pkg = SpreadsheetMLPackage.createPackage();
// Create worksheet
WorksheetPart sheet = pkg.createWorksheetPart(new PartName("/xl/worksheets/sheet1.xml"), "Sheet1", 1);
SheetData sheetData = sheet.getContents().getSheetData();
// Add Data
Row row = Context.getsmlObjectFactory().createRow();
Cell cell = Context.getsmlObjectFactory().createCell();
cell.setV("1234");
row.getC().add(cell);
CTXstringWhitespace ctx = Context.getsmlObjectFactory().createCTXstringWhitespace();
ctx.setValue("Open Source Java Library for Spreadsheet Documents");
CTRst ctrst = new CTRst();
ctrst.setT(ctx);
cell.setT(STCellType.INLINE_STR);
cell.setIs(ctrst);
row.getC().add(cell);
sheetData.getRow().add(row);
// Save
pkg.save(new File("FileFormat.xlsx"));                
                  
 English