Cách sử dụng Font cho một ô trong Excel – Apache poi

Trong bài viết này, chúng ta sẽ cùng nhau tìm hiểu cách sử dụng Apache POI để tùy biến font của một ô (cell) trong file excel. Trong nội bộ bài viết mình sẽ cung cấp một số ví dụ về chỉnh sửa kiểu chữ in đậm, in nghiêng, gạch dưới, chiều cao, màu sắc font chữ.

Dependency

Đầu tiên, chúng ta cần thêm phụ thuộc POI vào tệp pom.xml của dự án. Để làm việc với Microsoft Excel 2007+, chúng ta nên sử dụng poi-ooxml:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>

Ví dụ chỉnh sửa font, chiều cao và màu sắc

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Main {

    public static void main(String... args) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("fonts-example");
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("DeftBlog");

        Font font = workbook.createFont();
        font.setFontHeightInPoints((short)24);
        font.setFontName("Arial");
        font.setColor(IndexedColors.BLUE.getIndex());

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);

        try (OutputStream fileOut = new FileOutputStream("example.xlsx")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Output

apache-font-1

Ví dụ chỉnh font tô đậm và gạch dưới

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Main {

    public static void main(String... args) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("fonts-example");

        // Create Cell 1
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("DeftBlog");

        Font font = workbook.createFont();
        font.setBold(true);
        font.setUnderline(Font.U_SINGLE);

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);

        // Create Cell 1
        row = sheet.createRow(2);
        cell = row.createCell(0);
        cell.setCellValue("DeftBlog");

        font = workbook.createFont();
        font.setBold(true);
        font.setUnderline(Font.U_DOUBLE);

        cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);

        // Create Cell 3
        row = sheet.createRow(4);
        cell = row.createCell(0);
        cell.setCellValue("DeftBlog");

        font = workbook.createFont();
        font.setBold(true);
        font.setUnderline(Font.U_SINGLE_ACCOUNTING);

        cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);

        // Create Cell 4
        row = sheet.createRow(6);
        cell = row.createCell(0);
        cell.setCellValue("DeftBlog");

        font = workbook.createFont();
        font.setBold(true);
        font.setUnderline(Font.U_DOUBLE_ACCOUNTING);

        cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);

        try (OutputStream fileOut = new FileOutputStream("example.xlsx")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Output

apache-font-2

Ví dụ chữ in nghiêng, và bị gạch ngang

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Main {

    public static void main(String... args) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("fonts-example");
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("DeftBlog");

        Font font = workbook.createFont();
        font.setItalic(true);
        font.setStrikeout(true);

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setFont(font);
        cell.setCellStyle(cellStyle);

        try (OutputStream fileOut = new FileOutputStream("example.xlsx")) {
            workbook.write(fileOut);
            workbook.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Output

 

Ví dụ xoay chiều của văn bản

import java.io.File;
import java.io.FileOutputStream;

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;

public class TextDirection {
   public static void main(String[] args)throws Exception {
      XSSFWorkbook workbook = new XSSFWorkbook(); 
      XSSFSheet spreadsheet = workbook.createSheet("Text direction");
      XSSFRow row = spreadsheet.createRow(2);
      XSSFCellStyle myStyle = workbook.createCellStyle();
      myStyle.setRotation((short) 0);
      XSSFCell cell = row.createCell(1);
      cell.setCellValue("0D angle");
      cell.setCellStyle(myStyle);

      //30 degrees
      myStyle = workbook.createCellStyle();
      myStyle.setRotation((short) 30);
      cell = row.createCell(3);
      cell.setCellValue("30D angle");
      cell.setCellStyle(myStyle);

      //90 degrees
      myStyle = workbook.createCellStyle();
      myStyle.setRotation((short) 90);
      cell = row.createCell(5);
      cell.setCellValue("90D angle");
      cell.setCellStyle(myStyle);

      //120 degrees
      myStyle = workbook.createCellStyle();
      myStyle.setRotation((short) 120);
      cell = row.createCell(7);
      cell.setCellValue("120D angle");
      cell.setCellStyle(myStyle);

      //270 degrees
      myStyle = workbook.createCellStyle();
      myStyle.setRotation((short) 270);
      cell = row.createCell(9);
      cell.setCellValue("270D angle");
      cell.setCellStyle(myStyle);

      //360 degrees
      myStyle = workbook.createCellStyle();
      myStyle.setRotation((short) 360);
      cell = row.createCell(12);
      cell.setCellValue("360D angle");
      cell.setCellStyle(myStyle);
      
      FileOutputStream out = new FileOutputStream(new File("textdirection.xlsx"));
      workbook.write(out);
      out.close();
      System.out.println("textdirection.xlsx written successfully");
   }
}

Output

apache-font-4

Nguồn

https://simplesolution.dev/java-code-examples/apache-poi-excel-cells-fonts/

https://www.tutorialspoint.com/apache_poi/apache_poi_fonts.htm

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
1
0
Would love your thoughts, please comment.x
()
x