今天分享的是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蹲姐。