Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Apply Different Styles to a Cell in a Spreadsheet using Java?

Improve Article
Save Article
  • Last Updated : 27 Apr, 2021
Improve Article
Save Article

Apache POI is a powerful API that enables the user to create, manipulate, and display various file formats based on Microsoft Office using java programs. Using POI, one should be able to perform create, modify, and display/read operations on the following file formats. For Example, Java doesn’t provide built-in support for working with Excel files, so we need to look for open-source APIs for the job. It is an open-source library developed and distributed by Apache Software Foundation to design or modify Microsoft Office files using Java program. It contains classes and methods to decode the user input data or a file into MS Office documents. Here different styles can be applied like fonts, colors, cell merging, alignments, etc. to a cell in Excel using this concept in the Java program.

Apache POI Architecture: It consists of various components that make an architecture to form a working system:

  • POIFS (Poor Obfuscation Implementation File System): This component is the basic factor of all other POI elements. It is used to read different files explicitly.
  • HSSF (Horrible Spreadsheet Format): It is used to read and write xls format of MS-Excel files.
  • XSSF (XML Spreadsheet Format): It is used for xlsx file format of MS-Excel.
  • HPSF (Horrible Property Set Format): It is used to extract property sets of the MS-Office files.
  • HWPF (Horrible Word Processor Format): It is used to read and write doc extension files of MS-Word.
  • XWPF (XML Word Processor Format): It is used to read and write Docx extension files of MS-Word.
  • HSLF (Horrible Slide Layout Format): It is used to read, create, and edit PowerPoint presentations.
  • HDGF (Horrible Diagram Format): It contains classes and methods for MS-Visio binary files.
  • HPBF (Horrible Publisher Format): It is used to read and write MS-Publisher files.

A Jar file is a Zip archive containing one or multiple java class files. This makes the usage of libraries handier. Directories and Jar files are added to the build path and available to the ClassLoader at runtime to find particular classes inside it. We generally use .jar files to distribute Java applications or libraries, in the form of Java class files and associated metadata and resources (text, images, etc.).

 One can say JAR = JavaARchive


  • Step 1: Import the necessary .jar files like HSSF, XML and add them to your build path.
  • Step 2: Create a workbook using “new XSSFWorkbook()” in which we have to create the spreadsheet or the Excel file using “workbook.createSheet(‘Sheet1’)” in which we are going to apply different styles.

  • Step 3: Apply the styles, here merge the cells using the command spreadsheet.addMergedRegion. We have to provide the range addresses of rows and columns as well as its parameter.
  • Step 4: Now the next style in determining the cell alignment. For this, we have two commands
    • “style1.setAlignment(XSSFCellStyle.ALIGN_LEFT)” for determining the alignment and
    • “style1.setVerticalAlignment(XSSFCellStyle.VERTICAL_TOP)” for determining vertical alignment
  • Step 5: For applying border to a cell, we can use “setBorderBottom/Left/Top/Right(XSSFCleeName.BorderName)”.
  • Step 6: Change the border name in the parameter to go through different border styles.
  • Step 7: For filling colors and adding patterns, first set the background color of the cell using “setFillBackgroundColor(HSSFColor.COLOR_NAME.index)”.
  • Step 8: Then set the pattern whichever you like by “setFillPattern(XSSFCellStyle.PATTERN_NAME)”.
  • Step 9: Finally, set the alignment using”setAlignment(XSSFCellStyle.ALIGN_TYPE);

Implementation: Carrying out the above procedural steps over the empty Excel file at the local directory already created.

  1. Create a Spread Sheet by creating an object of XSSFSheet
  2. Creating a row in the above XSSFSheet using createRow() method.
    • Later on, setting the height of a row
  3. Creating an object of type XSSFCell and typecasting above row created to it.
  4. Setting cell values.
  5. Merging the cells.
  6. Aligning the cells.
  7. Justify the alignment.
  8. Bordering the cells.
  9. Filling colors in the cells.
  10. Creating a new file in the local directory by creating the object of FileOutputStream.
  11. Write to the above workbook created in the initial step.
  12. Close the connection of the file.



// Java Program to apply different styles
// to a cell in a spreadsheet
// Importing java input/output classes
// Importing Apache POI modules
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
// Class- for styling cells
public class GFG {
    // Main driver method
    public static void main(String[] args) throws Exception
        // Create a Work Book
        XSSFWorkbook workbook = new XSSFWorkbook();
        // Step 1: Create a Spread Sheet by
        // creating an object of XSSFSheet
        XSSFSheet spreadsheet
            = workbook.createSheet("Sheet1");
        // Step 2(a): Creating a row in above XSSFSheet
        // using createRow() method
        XSSFRow row = spreadsheet.createRow((short)1);
        // Step 2(b): Setting height of a row
        // Step 3: Creating an object of type XSSFCell and
        // typecasting above row created to it
        XSSFCell cell = (XSSFCell)row.createCell((short)1);
        // Step 4: Setting cell values
        cell.setCellValue("Merged cells");
        // Step 5: MERGING CELLS
        // This statement for merging cells
        spreadsheet.addMergedRegion(new CellRangeAddress(
            1, // first row (0-based)
            1, // last row (0-based)
            1, // first column (0-based)
            4 // last column (0-based)
        // Step 6: CELL Alignment
        row = spreadsheet.createRow(5);
        cell = (XSSFCell)row.createCell(0);
        // 6(a) Top Left alignment
        XSSFCellStyle style1 = workbook.createCellStyle();
        spreadsheet.setColumnWidth(0, 8000);
        cell.setCellValue("Hi, I'm top left indent");
        row = spreadsheet.createRow(6);
        cell = (XSSFCell)row.createCell(1);
        // 6(b) Center Align Cell Contents
        XSSFCellStyle style2 = workbook.createCellStyle();
        cell.setCellValue("I'm Center Aligned indent");
        row = spreadsheet.createRow(7);
        cell = (XSSFCell)row.createCell(2);
        // 6(c) Bottom Right alignment
        XSSFCellStyle style3 = workbook.createCellStyle();
        cell.setCellValue("I'm Bottom Right indent");
        row = spreadsheet.createRow(8);
        cell = (XSSFCell)row.createCell(3);
        // Step 7: Justifying Alignment
        XSSFCellStyle style4 = workbook.createCellStyle();
            "I'm Justify indent nice to meet you");
        // Step 8: CELL BORDER
        row = spreadsheet.createRow((short)10);
        cell = (XSSFCell)row.createCell((short)1);
        XSSFCellStyle style5 = workbook.createCellStyle();
        // Step 9: Fill Colors
        // 9(a) Background color
        row = spreadsheet.createRow((short)10);
        cell = (XSSFCell)row.createCell((short)1);
        XSSFCellStyle style6 = workbook.createCellStyle();
        spreadsheet.setColumnWidth(1, 8000);
        cell.setCellValue("FILL HORIZONTAL CROSS HATCH");
        // 9(b) Foreground color
        row = spreadsheet.createRow((short)12);
        cell = (XSSFCell)row.createCell((short)1);
        XSSFCellStyle style7 = workbook.createCellStyle();
        cell.setCellValue("THIN VERTICAL STRIPE");
        // Step 10: Creating a new file in the local
        // directory by creating object of FileOutputStream
        FileOutputStream out = new FileOutputStream(
            new File("C:/poiexcel/stlingcells.xlsx"));
        // Step 11: Write to above workbook created in
        // initial step
        // Step 12: Close the file connection
        // Display message for console window when
        // program is successfully executed
        System.out.println("gfg.xlsx success");






My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!