JAVA使用POI中XSSF方法導(dǎo)出excel

今天分享的是POI方法導(dǎo)出excel舞终,這兩個(gè)月時(shí)間我的大部分工作都是導(dǎo)出報(bào)表,今天就給大家分享excel報(bào)表的導(dǎo)出癣猾。

HSSF 提供讀寫舊版本Excel的功能敛劝,而XSSF提供讀寫新版本Excel格式檔案的功能,所以在項(xiàng)目中我們大部分使用的XSSF的方法來導(dǎo)出Excel纷宇,畢竟我們沒法要求客戶使用的是哪個(gè)版本的office夸盟。但是新版本的兼容舊版的office,所以我建議使用XSSF方法來做Excel的導(dǎo)出像捶。

POI全稱為Apache POI上陕,是Apache軟件基金會的開放源碼函式庫,提供Java程序?qū)icrosoft Office格式檔案讀和寫的功能拓春。
官方主頁http://poi.apache.org/index.html释簿,
API文檔http://poi.apache.org/apidocs/index.html

MAVEN引入POI架包

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

使用代碼

import com.google.common.collect.Lists;
import com.hand.hap.core.BaseConstants;
import com.hand.hap.core.IRequest;
import com.hand.hap.system.service.impl.BaseServiceImpl;
import ect.co.util.InitDataStyle;
import ect.inv.dto.InvJointBill;
import ect.inv.mapper.InvJointBillMapper;
import ect.inv.service.IInvJoinBillService;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class InvJointBillServiceImpl extends BaseServiceImpl<InvJointBill> implements IInvJoinBillService {
    @Autowired
    private InvJointBillMapper jointBillMapper;
    /*sheet頁1名稱*/
    private final static String SHEET1_NAME = "sheet1";
    /*寬度*/
    public static final int pageWidth = 12;

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat(BaseConstants.DATE_FORMAT);//日期轉(zhuǎn)換器

    @Override
    public XSSFWorkbook exportExcel(IRequest iRequest, InvJointBill invJointBill) {
        InvJointBill jointBillPart=jointBillMapper.selectBill(invJointBill);
        /*結(jié)算日期*/
        String  settlementDate="";
        if(invJointBill.getSettlementDate()!=null){
            settlementDate=simpleDateFormat.format(invJointBill.getSettlementDate());
        }
         //創(chuàng)建工作簿對象
        XSSFWorkbook xwork = new XSSFWorkbook();
       //創(chuàng)建工作表,這里的sheet1根據(jù)自己的實(shí)際需求更改
        XSSFSheet sheet = xwork.createSheet(SHEET1_NAME);
        //設(shè)置字體
        XSSFFont font = xwork.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short)12);

        //設(shè)置下邊框,并使高度居中
        CellStyle rowHB = InitDataStyle.getBorderBC(xwork);
        rowHB.setFont(font);

        //下左右設(shè)置邊框硼莽,并使高度居中
        CellStyle rowHLBR = InitDataStyle.getBorderLBR(xwork);
        rowHLBR.setVerticalAlignment(VerticalAlignment.CENTER);
        rowHLBR.setFont(font);
        //下右設(shè)置邊框庶溶,并使高度居中
        CellStyle rowHBR = InitDataStyle.getBorderBR(xwork);
        rowHBR.setVerticalAlignment(VerticalAlignment.CENTER);
        rowHBR.setFont(font);
        //下設(shè)置邊框,并使高度居中
        CellStyle rowHLB = InitDataStyle.getBorderLB(xwork);
        rowHLB.setVerticalAlignment(VerticalAlignment.CENTER);
        rowHLB.setFont(font);
         //下右設(shè)置邊框懂鸵,并使高度居中渐尿,水平居中
        CellStyle rowHBCR = InitDataStyle.getBorderBCR(xwork);
        rowHBCR.setVerticalAlignment(VerticalAlignment.CENTER);
        rowHBCR.setFont(font);
         //下設(shè)置邊框,并使高度居中矾瑰,水平居中
        CellStyle rowHBC = InitDataStyle.getBorderBC(xwork);
        rowHBC.setVerticalAlignment(VerticalAlignment.CENTER);
        rowHBC.setFont(font);
        //水平居中,四邊設(shè)置邊框隘擎,并設(shè)置自動換行
        XSSFCellStyle mediumStyle = InitDataStyle.getHorizontalCenterBorder(xwork);
        mediumStyle.setWrapText(true);
        mediumStyle.setFont(font);
       //水平居中殴穴,四周設(shè)置邊框
        XSSFCellStyle LeftStyle = InitDataStyle.getHorizontalCenterBorder(xwork);
        LeftStyle.setFont(font);
        /*標(biāo)題*/
        XSSFRow head = sheet.createRow(0);
        XSSFCell cell = head.createCell(0);
        //給單元格設(shè)置值(以下不再重復(fù))
        cell.setCellValue("結(jié)算單(協(xié)議號:" + invJointBill.getConNum() + ")");
        //設(shè)置單元格樣式(以下不再重復(fù))
        cell.setCellStyle(InitDataStyle.getHorizontalCenterBoldHead(xwork));
        //設(shè)置單元格跨列(以下不再重復(fù))
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, pageWidth));
        /*結(jié)算日期*/
        XSSFRow row2 = sheet.createRow(2);
        XSSFCell cell27 = row2.createCell(7);
        cell27.setCellValue("結(jié)算日期:"+settlementDate);
        cell27.setCellStyle(InitDataStyle.getRight(xwork));
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 7, pageWidth));
        /*甲方*/
        XSSFRow row3 = sheet.createRow(3);
        XSSFCell cell31 = row3.createCell(0);
        cell31.setCellValue("甲方:"+jointBillPart.getPartNameA());
        cell31.setCellStyle(InitDataStyle.getNormal(xwork));
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, pageWidth-2));
        /*乙方*/
        XSSFRow row4 = sheet.createRow(4);
        XSSFCell cell41 = row4.createCell(0);
        cell41.setCellValue("乙方:"+jointBillPart.getPartNameB());
        cell41.setCellStyle(InitDataStyle.getNormal(xwork));
        sheet.addMergedRegion(new CellRangeAddress(4, 4, 0, pageWidth-2));
        /*列表*/
        XSSFRow listhead = sheet.createRow(6);
        List<String> colNameList = Lists.newArrayList(
                "甲乙雙方簽訂\r\n合同數(shù)量(噸)", "乙方支付甲方\r\n履約保證金(元)", "甲方退還乙方\r\n履約保證金(元)", "乙方實(shí)際\r\n交付皮棉數(shù)量(噸)",
                "甲方撥付\r\n乙方資金(元)", "資金利息(元)", "甲方銷售回款(元)", "乙方還款(元)", "乙方已支付\r\n管理費(fèi)(元)", "沖抵乙方應(yīng)付\r\n資金利息(元)",
                "沖抵乙方應(yīng)付\r\n代墊費(fèi)用(元)", "甲方支付乙方\r\n平倉利潤(元)", "甲方應(yīng)支付\r\n乙方棉款(元)"

        );
        int num=initDate(sheet,xwork,invJointBill,7,mediumStyle);
        /*備注*/
        XSSFRow rowN = sheet.createRow(num);
        XSSFCell cellN1 = rowN.createCell(0);
        cellN1.setCellValue("備注:資金利息明細(xì)見隨附資金利息結(jié)算單");
        cellN1.setCellStyle(LeftStyle);
        XSSFCellStyle style=InitDataStyle.getBorderBR(xwork);
        for (int i=1;i<=pageWidth;i++){
            XSSFCell cellNB = rowN.createCell(i);
            if (i == pageWidth) {
                cellNB.setCellStyle(style);
            } else {
                cellNB.setCellStyle(style);
            }
        }
        sheet.addMergedRegion(new CellRangeAddress(num, num, 0, pageWidth));
        /*甲方蓋章*/
        XSSFRow rowN1 = sheet.createRow(num+3);
        XSSFCell cellN11 = rowN1.createCell(0);
        cellN11.setCellValue("甲方(蓋章確認(rèn)):");
        cellN11.setCellStyle(InitDataStyle.getNormal(xwork));
        sheet.addMergedRegion(new CellRangeAddress(num+3, num+3, 0, 1));
        /*乙方蓋章*/
        XSSFCell cellN12 = rowN1.createCell(6);
        cellN12.setCellValue("乙方(蓋章確認(rèn)):");
        cellN12.setCellStyle(InitDataStyle.getNormal(xwork));
        sheet.addMergedRegion(new CellRangeAddress(num+3, num+3, 6, 7));
        /*總經(jīng)理*/
        XSSFRow rowN2 = sheet.createRow(num+6);
        XSSFCell cellN21 = rowN2.createCell(0);
        cellN21.setCellValue("總經(jīng)理:");
        cellN21.setCellStyle(InitDataStyle.getNormal(xwork));
        sheet.addMergedRegion(new CellRangeAddress(num+6, num+6, 0, 1));
        /*財(cái)務(wù)總監(jiān)*/
        XSSFRow rowN3 = sheet.createRow(num+8);
        XSSFCell cellN31 = rowN3.createCell(0);
        cellN31.setCellValue("財(cái)務(wù)總監(jiān):");
        cellN31.setCellStyle(InitDataStyle.getNormal(xwork));
        sheet.addMergedRegion(new CellRangeAddress(num+8, num+8, 0, 1));
        /*副總經(jīng)理*/
        XSSFRow rowN4 = sheet.createRow(num+10);
        XSSFCell cellN41 = rowN4.createCell(0);
        cellN41.setCellValue("副總經(jīng)理:");
        cellN41.setCellStyle(InitDataStyle.getNormal(xwork));
        sheet.addMergedRegion(new CellRangeAddress(num+10, num+10, 0, 1));
        /*副總經(jīng)理*/
        XSSFRow rowN5 = sheet.createRow(num+12);
        XSSFCell cellN51 = rowN5.createCell(0);
        cellN51.setCellValue("棉花部負(fù)責(zé)人:");
        cellN51.setCellStyle(InitDataStyle.getNormal(xwork));
        sheet.addMergedRegion(new CellRangeAddress(num+12, num+12, 0, 1));
        /*副總經(jīng)理*/
        XSSFRow rowN6 = sheet.createRow(num+14);
        XSSFCell cellN61 = rowN6.createCell(0);
        cellN61.setCellValue("財(cái)務(wù)審核:");
        cellN61.setCellStyle(InitDataStyle.getNormal(xwork));
        sheet.addMergedRegion(new CellRangeAddress(num+14, num+14, 0, 1));
        /*副總經(jīng)理*/
        XSSFRow rowN7 = sheet.createRow(num+16);
        XSSFCell cellN71 = rowN7.createCell(0);
        cellN71.setCellValue("制表人:");
        cellN71.setCellStyle(InitDataStyle.getNormal(xwork));
        sheet.addMergedRegion(new CellRangeAddress(num+16, num+16, 0, 1));
        for (int i = 0; i < colNameList.size(); i++) {
            listhead.createCell(i).setCellValue(new XSSFRichTextString(colNameList.get(i)));
            listhead.getCell(i).setCellStyle(mediumStyle);
        }
        //自動適應(yīng)列寬
        for (int i = 0; i < colNameList.size(); i++) {
            sheet.autoSizeColumn(i, true);
        }
        return xwork;
    }

    /**
     * 初始化數(shù)據(jù),往報(bào)表中添加數(shù)據(jù)
     * @param sheet
     * @param xwork
     * @param invJointBill
     * @param rowNum
     * @return
     */
    private int initDate(XSSFSheet sheet,XSSFWorkbook xwork,InvJointBill invJointBill,int rowNum,XSSFCellStyle mediumStyle){
        List<InvJointBill> invJointBillList=jointBillMapper.selectBills(invJointBill);
        XSSFFont font = xwork.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short)12);
        for(InvJointBill jointBill:invJointBillList){
            XSSFRow rowRD = sheet.createRow(rowNum);
            //甲乙雙方簽訂合同數(shù)量(噸)
            XSSFCell cell1 = rowRD.createCell(0);
            cell1.setCellValue(jointBill.getConQty()==null?"":jointBill.getConQty().toString());
            cell1.setCellStyle(mediumStyle);
            /*乙方支付甲方履約保證金(元)*/
            XSSFCell cell0 = rowRD.createCell(1);
            cell0.setCellValue("");
            cell0.setCellStyle(mediumStyle);
            /*甲方退還乙方履約保證金(元)*/
            XSSFCell cell2 = rowRD.createCell(2);
            cell2.setCellValue("");
            cell2.setCellStyle(mediumStyle);
            //乙方實(shí)際交付皮棉數(shù)量(噸)
            XSSFCell cell3 = rowRD.createCell(3);
            cell3.setCellValue(jointBill.getBatchWeight()==null?"":jointBill.getBatchWeight().toString());
            cell3.setCellStyle(mediumStyle);
            //甲方撥付乙方資金(元)
            XSSFCell cell4 = rowRD.createCell(4);
            BigDecimal appAmt=jointBill.getAppAmt()==null?new BigDecimal(0):jointBill.getAppAmt();
            cell4.setCellValue(jointBill.getAppAmt()==null?"":jointBill.getAppAmt().toString());
            cell4.setCellStyle(mediumStyle);
            //資金利息(元)
            XSSFCell cell5 = rowRD.createCell(5);
            BigDecimal interSumAmount=jointBill.getInterestSumAmount()==null?new BigDecimal(0):jointBill.getInterestSumAmount();
            cell5.setCellValue(jointBill.getInterestSumAmount()==null?"":jointBill.getInterestSumAmount().toString());
            cell5.setCellStyle(mediumStyle);
            //甲方銷售回款(元)
            XSSFCell cell6 = rowRD.createCell(6);
            BigDecimal acctedAmt=jointBill.getAcctedAmt()==null?new BigDecimal(0):jointBill.getAcctedAmt();
            cell6.setCellValue(jointBill.getAcctedAmt()==null?"":jointBill.getAcctedAmt().toString());
            cell6.setCellStyle(mediumStyle);
            //乙方還款(元)
            XSSFCell cell7= rowRD.createCell(7);
            cell7.setCellValue(jointBill.getNum()==null?"":jointBill.getNum().toString());
            cell7.setCellStyle(mediumStyle);
            //乙方已支付管理費(fèi)(元)
            XSSFCell cell8= rowRD.createCell(8);
            BigDecimal weight=jointBill.getBatchWeight()==null?new BigDecimal(0):
                    jointBill.getBatchWeight().multiply(new BigDecimal(100));
            cell8.setCellValue(jointBill.getBatchWeight()==null?"":
                    jointBill.getBatchWeight().multiply(new BigDecimal(100)).toString());
            cell8.setCellStyle(mediumStyle);
            //沖抵乙方應(yīng)付資金利息(元)
            XSSFCell cell9= rowRD.createCell(9);
            cell9.setCellValue(jointBill.getBatchWeight()==null||jointBill.getPrice()==null?"":jointBill.getBatchWeight().multiply(jointBill.getPrice()).toString());
            cell9.setCellStyle(mediumStyle);
            //沖抵乙方應(yīng)付代墊費(fèi)用(元)
            XSSFCell cell10= rowRD.createCell(10);
            BigDecimal f8=new BigDecimal(0);
            if(jointBill.getSumExp()!=null&&invJointBill.getTaxDefferrent()!=null){
                f8=jointBill.getSumExp().add(invJointBill.getTaxDefferrent());
                cell10.setCellValue(jointBill.getSumExp().add(invJointBill.getTaxDefferrent()).toString());
            }else if(jointBill.getSumExp()==null&&invJointBill.getTaxDefferrent()!=null){
                f8=invJointBill.getTaxDefferrent();
                cell10.setCellValue(invJointBill.getTaxDefferrent().toString());
            }else if(jointBill.getSumExp()!=null&&invJointBill.getTaxDefferrent()==null) {
                f8=jointBill.getSumExp();
                cell10.setCellValue(jointBill.getSumExp().toString());
            }else{
                cell10.setCellValue("");
            }
            cell10.setCellStyle(mediumStyle);
            //甲方支付乙方平倉利潤(元)
            XSSFCell cell11= rowRD.createCell(11);
            BigDecimal L8=invJointBill.getClosingProfit()==null?new BigDecimal(0):invJointBill.getClosingProfit();
            cell11.setCellValue(invJointBill.getClosingProfit()==null?"":invJointBill.getClosingProfit().toString());
            cell11.setCellStyle(mediumStyle);
            //甲方應(yīng)支付乙方棉款(元)
            XSSFCell cell12= rowRD.createCell(12);
            cell12.setCellValue(acctedAmt.subtract(appAmt)
                    .subtract(weight)
                    .subtract(interSumAmount)
                    .subtract(f8)
                    .add(L8).toString());
            cell12.setCellStyle(mediumStyle);
            rowNum++;
        }
        return rowNum;
    }
}

XSSF樣式公共類

package ect.co.util;

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

public class InitDataStyle {

    //水平居中 字體加粗
    public static XSSFCellStyle getHorizontalCenterBoldHead(XSSFWorkbook wb){
        XSSFCellStyle cellStyle = getBold(wb,20);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        return cellStyle;
    }

    //水平居中 字體加粗
    public static XSSFCellStyle getHorizontalCenterBoldHead2(XSSFWorkbook wb){
        XSSFCellStyle cellStyle = getBold(wb,14);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        return cellStyle;
    }


    //水平居中 字體加粗
    public static XSSFCellStyle getHorizontalCenterBoldHead2Blue(XSSFWorkbook wb){
        XSSFCellStyle cellStyle = getBold(wb,14);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return cellStyle;
    }

    //水平居中
    public static XSSFCellStyle getHorizontalCenter(XSSFWorkbook wb){
        XSSFCellStyle cellStyle = getNormal(wb);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        return cellStyle;
    }

    //水平居中
    public static XSSFCellStyle getHorizontalCenterBlue(XSSFWorkbook wb){
        XSSFCellStyle cellStyle = getNormal(wb);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        return cellStyle;
    }

    public static XSSFCellStyle getNormal(XSSFWorkbook wb){
        XSSFCellStyle cellStyle = wb.createCellStyle();
        Font font=wb.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short)12);
        cellStyle.setFont(font);
        return cellStyle;
    }

    //右對齊
    public static XSSFCellStyle getRight(XSSFWorkbook wb){
        XSSFCellStyle cellStyle = getNormal(wb);
        cellStyle.setAlignment(HorizontalAlignment.RIGHT);
        return cellStyle;
    }

    public static XSSFCellStyle getNormalWrap(XSSFWorkbook wb){
        XSSFCellStyle cellStyle = wb.createCellStyle();
        Font font=wb.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short)12);
        cellStyle.setFont(font);
        cellStyle.setWrapText(true);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        return cellStyle;
    }


    //加粗
    public static XSSFCellStyle getBold(XSSFWorkbook wb,int fontSize){
        XSSFCellStyle cellStyle = wb.createCellStyle();
        Font font=wb.createFont();
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short)fontSize);
        cellStyle.setFont(font);
        return cellStyle;
    }

    // 左上邊框
    public static XSSFCellStyle getBorderLT(XSSFWorkbook wb){
        XSSFCellStyle cellStyle=getNormal(wb);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        return  cellStyle;
    }

    // 右上邊框
    public static XSSFCellStyle getBorderTR(XSSFWorkbook wb){
        XSSFCellStyle cellStyle=getNormal(wb);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        return  cellStyle;
    }

    //上邊框
    public static XSSFCellStyle getBorderT(XSSFWorkbook wb){
        XSSFCellStyle cellStyle=getNormal(wb);
        cellStyle.setBorderTop(BorderStyle.THIN);
        return cellStyle;
    }

    //左邊框
    public static XSSFCellStyle getBorderL(XSSFWorkbook wb){
        XSSFCellStyle cellStyle=getNormal(wb);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        return cellStyle;
    }

    //右邊框
    public static XSSFCellStyle getBorderR(XSSFWorkbook wb){
        XSSFCellStyle cellStyle=getNormal(wb);
        cellStyle.setBorderRight(BorderStyle.THIN);
        return cellStyle;
    }

    //左下邊框
    public static XSSFCellStyle getBorderLB(XSSFWorkbook wb){
        XSSFCellStyle cellStyle=getNormal(wb);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        return  cellStyle;
    }

    //下邊框
    public static XSSFCellStyle getBorderB(XSSFWorkbook wb){
        XSSFCellStyle cellStyle=getNormal(wb);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        return  cellStyle;
    }

    //下邊框
    public static XSSFCellStyle getBorderLBR(XSSFWorkbook wb){
        XSSFCellStyle cellStyle=getNormal(wb);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        return  cellStyle;
    }

    //下邊框
    public static XSSFCellStyle getBorderBRight(XSSFWorkbook wb){
        XSSFCellStyle cellStyle=getNormal(wb);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setAlignment(HorizontalAlignment.RIGHT);
        return  cellStyle;
    }


    //下邊框,居中
    public static XSSFCellStyle getBorderBC(XSSFWorkbook wb){
        XSSFCellStyle cellStyle=getNormal(wb);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        return  cellStyle;
    }


    //右下邊框
    public static XSSFCellStyle getBorderBR(XSSFWorkbook wb){
        XSSFCellStyle cellStyle=getNormal(wb);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        return  cellStyle;
    }

    //右下邊框货葬,居中
    public static XSSFCellStyle getBorderBCR(XSSFWorkbook wb){
        XSSFCellStyle cellStyle=getNormal(wb);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        return  cellStyle;
    }

    //藍(lán)色字體
    public static XSSFCellStyle getBlue(XSSFWorkbook wb){
        XSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
        return cellStyle;
    }

    //藍(lán)色字體
    public static XSSFCellStyle getCenterBlue(XSSFWorkbook wb){
        XSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
        return cellStyle;
    }

    //水平居中 字體加粗加大
    public static XSSFCellStyle getHorizontalCenterBoldHeadBig(XSSFWorkbook wb){
        XSSFCellStyle cellStyle = getBold(wb,16);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        return cellStyle;
    }

    //水平居中 四周邊框
    public static XSSFCellStyle getHorizontalCenterBorder(XSSFWorkbook wb){
        XSSFCellStyle cellStyle = getNormal(wb);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        return cellStyle;
    }
}

效果

注意事項(xiàng)以及感悟

不要循環(huán)創(chuàng)建XSSFCellStyle采幌,因?yàn)閜oi對XSSFCellStyle的個(gè)數(shù)會有限定,所以相同的XSSFCellStyle在每一個(gè)類中最好只創(chuàng)建一個(gè)震桶,然后讓其他單元格去引用休傍。
poi導(dǎo)出excel技術(shù)本身是不難的,難的是用這些技術(shù)去創(chuàng)造出自己需要的Excel蹲姐。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末磨取,一起剝皮案震驚了整個(gè)濱河市人柿,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌忙厌,老刑警劉巖凫岖,帶你破解...
    沈念sama閱讀 211,743評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異逢净,居然都是意外死亡哥放,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,296評論 3 385
  • 文/潘曉璐 我一進(jìn)店門爹土,熙熙樓的掌柜王于貴愁眉苦臉地迎上來甥雕,“玉大人,你說我怎么就攤上這事胀茵∩缏叮” “怎么了?”我有些...
    開封第一講書人閱讀 157,285評論 0 348
  • 文/不壞的土叔 我叫張陵宰掉,是天一觀的道長呵哨。 經(jīng)常有香客問我,道長轨奄,這世上最難降的妖魔是什么孟害? 我笑而不...
    開封第一講書人閱讀 56,485評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮挪拟,結(jié)果婚禮上挨务,老公的妹妹穿的比我還像新娘。我一直安慰自己玉组,他們只是感情好谎柄,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,581評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著惯雳,像睡著了一般朝巫。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上石景,一...
    開封第一講書人閱讀 49,821評論 1 290
  • 那天劈猿,我揣著相機(jī)與錄音,去河邊找鬼潮孽。 笑死揪荣,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的往史。 我是一名探鬼主播仗颈,決...
    沈念sama閱讀 38,960評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼椎例!你這毒婦竟也來了挨决?” 一聲冷哼從身側(cè)響起请祖,我...
    開封第一講書人閱讀 37,719評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎凰棉,沒想到半個(gè)月后损拢,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,186評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡撒犀,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,516評論 2 327
  • 正文 我和宋清朗相戀三年福压,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片或舞。...
    茶點(diǎn)故事閱讀 38,650評論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡荆姆,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出映凳,到底是詐尸還是另有隱情胆筒,我是刑警寧澤,帶...
    沈念sama閱讀 34,329評論 4 330
  • 正文 年R本政府宣布诈豌,位于F島的核電站仆救,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏矫渔。R本人自食惡果不足惜彤蔽,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,936評論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望庙洼。 院中可真熱鬧顿痪,春花似錦、人聲如沸油够。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,757評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽石咬。三九已至揩悄,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間鬼悠,已是汗流浹背虏束。 一陣腳步聲響...
    開封第一講書人閱讀 31,991評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留厦章,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,370評論 2 360
  • 正文 我出身青樓照藻,卻偏偏與公主長得像袜啃,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子幸缕,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,527評論 2 349

推薦閱讀更多精彩內(nèi)容

  • 顏值不夠~讀書來湊 Day8 《三觀易碎》 王瀟 part8 致女兒書 ~媽媽的話 媽媽愛孩子群发,孩子也愛媽媽晰韵,這是...
    忘憂草212閱讀 246評論 0 0
  • 想寫就寫------ 光與影 走進(jìn)陽光的時(shí)候 輝煌絢麗 我只是看見了自己斜長的影子 走進(jìn)煙波浩淼的湖畔 楊柳...
    我是宣閱讀 259評論 11 20