Underline Excel Cell Content- Java POI Example Program

Underline Cell Contents - Introduction

Time for another formatting tutorial using POI in Java. In this example, we will explain how to underline Excel cell contents, using Apache POI in Java. We will introduce you to the setUnderline method and describe various type of underlines, you can add to your cell text when working with POI. As per our earlier tutorials, we will cover both XLS and XLSX formats styling in this post. We will use POI v3.8 for the examples, so make sure you have the right JAR files in your classpath.

XLS - Cell Text Underline - Java Program Example


As discussed earlier, you can use setUnderline method in HSSFFont class to underline the contents of a cell. There are different values that you can pass to this method:
  • U_SINGLE - single line underline
  • U_DOUBLE - double underline
  • U_SINGLE_ACCOUNTING - accounting style single line underline
  • U_DOUBLE_ACCOUNTNG - accounting style double line underline.
We will cover all of them in this example and see sample outputs. First, the Java program

import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
public class UnderlineCellContent {  
        public static void main(String[] args) throws Exception{
                /* Create Workbook and Worksheet */
                HSSFWorkbook my_workbook = new HSSFWorkbook();
                HSSFSheet my_sheet = my_workbook.createSheet("Cell Underline");
                /* Get access to HSSFCellStyle */
                HSSFCellStyle my_style = my_workbook.createCellStyle();
                HSSFCellStyle my_style_2 = my_workbook.createCellStyle();
                /* Create HSSFFont object from the workbook */
                HSSFFont my_font=my_workbook.createFont();
                HSSFFont my_font_2=my_workbook.createFont();
                /* Set font to Italic */
                my_font.setItalic(true);                
                                
                /* Create a row in the sheet */
                Row row = my_sheet.createRow(0);
                /* Create a cell */
                Cell cell = row.createCell(0);
                cell.setCellValue("Single line underlined and italics");
                my_font.setUnderline(HSSFFont.U_SINGLE);
                my_style.setFont(my_font);
                /* Attach the style to the cell */
                cell.setCellStyle(my_style);
                
                /* Create another cell for double line underline test */
                cell = row.createCell(1);
                cell.setCellValue("Double line underlined, normal font");
                my_font_2.setUnderline(HSSFFont.U_DOUBLE);
                my_style_2 .setFont(my_font_2);
                cell.setCellStyle(my_style_2);
                
                /* Write changes to the workbook */
                FileOutputStream out = new FileOutputStream(new File("C:\\cell_underline_example.xls"));
                my_workbook.write(out);
                out.close();
        }
}

You can try changing the type of underline to U_SINGLE_ACCOUNTING and U_DOUBLE_ACCOUNTING and examine the output.

XLSX- Cell Underline Example - Java Program


You can use the Java program below to underline cell contents for XLSX format files. Here, we use setUnderline in XSSFFont class.
import java.io.*;
import org.apache.poi.xssf.usermodel.*; 
import org.apache.poi.ss.usermodel.*;
public class UnderlineCellContentXLSX {  
        public static void main(String[] args) throws Exception{
                /* Create Workbook and Worksheet XLSX Format */
                XSSFWorkbook my_workbook = new XSSFWorkbook();
                XSSFSheet my_sheet = my_workbook.createSheet("Cell Underline);
                /* Get access to XSSFCellStyle */
                XSSFCellStyle my_style = my_workbook.createCellStyle();
                XSSFCellStyle my_style_2 = my_workbook.createCellStyle();
                /* Create XSSFFont object from the workbook */
                XSSFFont my_font=my_workbook.createFont();
                XSSFFont my_font_2=my_workbook.createFont();
                
                /* Set font to Italic */
                my_font.setItalic(true);
                
                /* Create a row in the sheet */
                Row row = my_sheet.createRow(0);
                /* Create a cell */
                Cell cell = row.createCell(0);
                cell.setCellValue("Single line underlined and italics");
                my_font.setUnderline(XSSFFont.U_SINGLE);
                my_style.setFont(my_font);
                /* Attach the style to the cell */
                cell.setCellStyle(my_style);
                
                /* Create another cell for double line underline test */
                cell = row.createCell(1);
                cell.setCellValue("Double line underlined, normal font");
                my_font_2.setUnderline(XSSFFont.U_DOUBLE);
                my_style_2 .setFont(my_font_2);
                cell.setCellStyle(my_style_2);
                
                /* Write changes to the workbook */
                FileOutputStream out = new FileOutputStream(new File("C:\\Underline_Cell_Text.xlsx"));
                my_workbook.write(out);
                out.close();
                
        }
}

Single / Double Underline - Output Example


Here is a sample output for one of the programs. As you can see, the underline styles are incorporated.

Single / Double Underline - Excel Apache POI - Java Program Output
Single / Double Underline - Excel Apache POI - Java Program Output


No comments:

Post a Comment