通過一個實例演示怎樣通過POI設置Excel單元格的邊框、字體、顏色、大小、下划線、合並、對齊方式。
Excel文件如下:
package my.excel;
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CellFormatExcel {
public static void main(String[] args) {
try {
// 創建Excel表格工作簿
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("表格單元格格式化");
//============================
// 設置單元格的字體
//============================
Row ztRow = sheet.createRow((short)0);
Cell ztCell = ztRow.createCell(0);
ztCell.setCellValue("中國");
// 創建單元格樣式對象
XSSFCellStyle ztStyle = (XSSFCellStyle) wb.createCellStyle();
// 創建字體對象
Font ztFont = wb.createFont();
ztFont.setItalic(true); // 設置字體為斜體字
ztFont.setColor(Font.COLOR_RED); // 將字體設置為“紅色”
ztFont.setFontHeightInPoints((short)22); // 將字體大小設置為18px
ztFont.setFontName("華文行楷"); // 將“華文行楷”字體應用到當前單元格上
ztFont.setUnderline(Font.U_DOUBLE); // 添加(Font.U_SINGLE單條下划線/Font.U_DOUBLE雙條下划線)
// ztFont.setStrikeout(true); // 是否添加刪除線
ztStyle.setFont(ztFont); // 將字體應用到樣式上面
ztCell.setCellStyle(ztStyle); // 樣式應用到該單元格上
//============================
// 設置單元格邊框
//============================
Row borderRow = sheet.createRow(2);
Cell borderCell = borderRow.createCell(1);
borderCell.setCellValue("中國");
// 創建單元格樣式對象
XSSFCellStyle borderStyle = (XSSFCellStyle)wb.createCellStyle();
// 設置單元格邊框樣式
// CellStyle.BORDER_DOUBLE 雙邊線
// CellStyle.BORDER_THIN 細邊線
// CellStyle.BORDER_MEDIUM 中等邊線
// CellStyle.BORDER_DASHED 虛線邊線
// CellStyle.BORDER_HAIR 小圓點虛線邊線
// CellStyle.BORDER_THICK 粗邊線
borderStyle.setBorderBottom(CellStyle.BORDER_THICK);
borderStyle.setBorderTop(CellStyle.BORDER_DASHED);
borderStyle.setBorderLeft(CellStyle.BORDER_DOUBLE);
borderStyle.setBorderRight(CellStyle.BORDER_THIN);
// 設置單元格邊框顏色
borderStyle.setBottomBorderColor(new XSSFColor(java.awt.Color.RED));
borderStyle.setTopBorderColor(new XSSFColor(java.awt.Color.GREEN));
borderStyle.setLeftBorderColor(new XSSFColor(java.awt.Color.BLUE));
borderCell.setCellStyle(borderStyle);
//============================
// 設置單元內容的對齊方式
//============================
Row alignRow = sheet.createRow(4);
Cell alignCell = alignRow.createCell(1);
alignCell.setCellValue("中國");
// 創建單元格樣式對象
XSSFCellStyle alignStyle = (XSSFCellStyle)wb.createCellStyle();
// 設置單元格內容水平對其方式
// XSSFCellStyle.ALIGN_CENTER 居中對齊
// XSSFCellStyle.ALIGN_LEFT 左對齊
// XSSFCellStyle.ALIGN_RIGHT 右對齊
alignStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
// 設置單元格內容垂直對其方式
// XSSFCellStyle.VERTICAL_TOP 上對齊
// XSSFCellStyle.VERTICAL_CENTER 中對齊
// XSSFCellStyle.VERTICAL_BOTTOM 下對齊
alignStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
alignCell.setCellStyle(alignStyle);
//============================
// 設置單元格的高度和寬度
//============================
Row sizeRow = sheet.createRow(6);
sizeRow.setHeightInPoints(30); // 設置行的高度
Cell sizeCell = sizeRow.createCell(1);
String sizeCellValue = "《Java編程思想》"; // 字符串的長度為10,表示該字符串中有10個字符,忽略中英文
sizeCell.setCellValue(sizeCellValue);
// 設置單元格的長度為sizeCellVlue的長度。而sheet.setColumnWidth使用sizeCellVlue的字節數
// sizeCellValue.getBytes().length == 16
sheet.setColumnWidth(1, (sizeCellValue.getBytes().length) * 256 );
//============================
// 設置單元格自動換行
//============================
Row wrapRow = sheet.createRow(8);
Cell wrapCell = wrapRow.createCell(2);
wrapCell.setCellValue("寶劍鋒從磨礪出,梅花香自苦寒來");
// 創建單元格樣式對象
XSSFCellStyle wrapStyle = (XSSFCellStyle)wb.createCellStyle();
wrapStyle.setWrapText(true); // 設置單元格內容是否自動換行
wrapCell.setCellStyle(wrapStyle);
//============================
// 合並單元格列
//============================
Row regionRow = sheet.createRow(12);
Cell regionCell = regionRow.createCell(0);
regionCell.setCellValue("寶劍鋒從磨礪出,梅花香自苦寒來");
// 合並第十三行中的A、B、C三列
CellRangeAddress region = new CellRangeAddress(12, 12, 0, 2); // 參數都是從O開始
sheet.addMergedRegion(region);
//============================
// 合並單元格行和列
//============================
Row regionRow2 = sheet.createRow(13);
Cell regionCell2 = regionRow2.createCell(3);
String region2Value = "寶劍鋒從磨礪出,梅花香自苦寒來。"
+ "采得百花成蜜后,為誰辛苦為誰甜。"
+ "操千曲而后曉聲,觀千劍而后識器。"
+ "察己則可以知人,察今則可以知古。";
regionCell2.setCellValue(region2Value);
// 合並第十三行中的A、B、C三列
CellRangeAddress region2 = new CellRangeAddress(13, 17, 3, 7); // 參數都是從O開始
sheet.addMergedRegion(region2);
XSSFCellStyle region2Style = (XSSFCellStyle)wb.createCellStyle();
region2Style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
region2Style.setWrapText(true); // 設置單元格內容是否自動換行
regionCell2.setCellStyle(region2Style);
//============================
// 將Excel文件寫入到磁盤上
//============================
FileOutputStream is = new FileOutputStream("document/CellFormatExcel.xlsx");
wb.write(is);
is.close();
System.out.println("寫入成功,運行結束!");
} catch(Exception e) {
e.printStackTrace();
}
}
}