Java Excel导出表格样式调整工具类

需要的Maven仓库依赖如下:

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

实现导出Excel的表格样式调整工具类代码如下:

复制代码
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;


public class ExcelStyleUtilFor2003 {

    /**
     * 样式居中
     */
    public static void center(CellStyle cellStyle) {
     //水平居中 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
     //垂直居中
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); } /** * 单元格合并 * firstRow :合并的开始行 * lastRow:合并的结束行 * firstCol: 合并的开始列 * lastColL: 合并的结束列 */ public static void mergeCell(Sheet wbSheet, int firstRow, int lastRow, int firstCol, int lastCol) {
     // 合并单元格 起始行号 终止行号 起始列号 终止列号 wbSheet.addMergedRegion(
new CellRangeAddress(firstRow, lastRow, firstCol, lastCol)); } /** * 标题样式 :加粗,垂直居中 */ public static CellStyle getTitleStyle(Workbook wb, Boolean isBold, int FontISize) { // 标题样式(加粗,垂直居中) CellStyle cellStyle = wb.createCellStyle(); center(cellStyle); Font font = wb.createFont();
     //加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
     //设置标题字体大小
font.setFontHeightInPoints((short) FontISize); cellStyle.setFont(font); return cellStyle; } /** * 表头样式 */ public static CellStyle getHeadStyle(Workbook wb, int fontSize) { CellStyle cellStyle = wb.createCellStyle();
     //设置表头的背景颜色 cellStyle.setFillForegroundColor(HSSFColor.AQUA.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
     //设置表头的背景颜色 cellStyle.setFillBackgroundColor(HSSFColor.AQUA.index);
center(cellStyle); //设置字体 Font font = setFont(wb, fontSize); cellStyle.setFont(font); return cellStyle; } /** * 通用样式: 居中,设置字体大小 */ public static CellStyle getBodyStyle(Workbook wb, int fontSize) { CellStyle cellStyle = wb.createCellStyle(); // 设置单元格样式 center(cellStyle); Font font = setFont(wb, fontSize); cellStyle.setFont(font); return cellStyle; } /** * 设置单元格字体居中、并设置字体颜色 */ public static CellStyle getFontStyle(Workbook wb, int fontSize, short color) { CellStyle cellStyle = wb.createCellStyle(); Font font = setFont(wb, fontSize, color); center(cellStyle); cellStyle.setFont(font); return cellStyle; }

 


 

 

 

/**
     * 设置单元格字体
     */
    public static Font setFont(Workbook wb, int fontSize, short color) {
        Font font = wb.createFont();
        font.setColor(color);
        font.setFontHeightInPoints((short) fontSize);
        return font;
    }

    public static Font setFont(Workbook wb, int fontSize) {
        //设置字体
        Font font = wb.createFont();
        font.setFontHeightInPoints((short) fontSize);
        return font;
    }

    /**
     * 设置cell边框*/
    public static CellStyle setCellBorder(Workbook workbook){
        CellStyle cellStyle = workbook.createCellStyle();
     //设置了边框属性 下边框
        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
     //左边框 cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
     //上边框 cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
     //右边框 cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//设置边框颜色黑色 cellStyle.setTopBorderColor(HSSFColor.BLACK.index); cellStyle.setBottomBorderColor(HSSFColor.BLACK.index); cellStyle.setLeftBorderColor(HSSFColor.BLACK.index); cellStyle.setRightBorderColor(HSSFColor.BLACK.index); return cellStyle; } }
复制代码

 





 

posted on   胡子就不刮  阅读(554)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示