在開發中我們經常會用到生成excel,下面是spring項目前後端分離導出excel工具類,接口中直接引用即可。
首先引入poi-jar包:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
ExcelPoiUtil:
import java.util.List;
import org.apache.poi.hssf.usermodel.DVConstraint;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellRangeAddressList;
public class ExcelPoiUtil {
/**
*
* @param fileName 文件名稱
* @param sheetName sheet頁名稱
* @param titleList 列名
* @param titleCodeList 列名對應的code
* @param parpamtsList 可選擇參數列(需與列名一比一)
*/
public static HSSFWorkbook createExcel(String fileName,String sheetName,List<String> titleList,List<String> titleCodeList,List<List<String>> parpamtsList) {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = setSheetBaseInfoExcel(sheetName,36,30,wb);
//設置前三行凍結
sheet.createFreezePane(0,3,0,3);
//此處是設置 第一行 合併單元格的個數(標題行)
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, (titleList.size()-1)));
// 產生表格標題行
HSSFRow rowm = sheet.createRow(0);
HSSFCell cellTiltle = rowm.createCell(0);
HSSFCellStyle columnTopStyle = getColumnTopStyle(wb);//獲取列頭樣式對象
cellTiltle.setCellStyle(columnTopStyle);
cellTiltle.setCellValue(sheetName);
rowm.setHeightInPoints(25);
//隱藏第二行,並填寫code值
HSSFRow row_titleCode = sheet.createRow(1);
for(int i=0;i<titleCodeList.size();i++) {
HSSFCell cellTiltleCode = row_titleCode.createCell(i);
cellTiltleCode.setCellValue(titleCodeList.get(i));
}
row_titleCode.setZeroHeight(true);
//設置第三行標題 標題列
HSSFRow row_titleName = sheet.createRow(2);
HSSFCellStyle style = getStyle(wb);
for (int i=0;i<titleList.size();i++){
HSSFCell cell_titleName = row_titleName.createCell(i);
cell_titleName.setCellValue(titleList.get(i));
cell_titleName.setCellStyle(style);
}
//設置默認下拉內容
if(parpamtsList!=null && parpamtsList.size()>0) {
for(int i=0;i<parpamtsList.size();i++) {
if(parpamtsList.get(i)==null || parpamtsList.get(i).size()<1) {
continue;
}
//設置下拉控制的範圍
CellRangeAddressList regions = new CellRangeAddressList(3, 999, i, i);
// 生成下拉框內容
String[] strings = new String[parpamtsList.get(i).size()];
parpamtsList.get(i).toArray(strings);
DVConstraint constraint = DVConstraint.createExplicitListConstraint(strings);
// 綁定下拉框和作用區域
HSSFDataValidation data_validation = new HSSFDataValidation(regions, constraint);
// 對sheet頁生效
sheet.addValidationData(data_validation);
}
}
return wb;
}
/**設計excel定義列數、列寬和標頭信息*/
private static HSSFSheet setSheetBaseInfoExcel(String excelName,int columWith,int rowHight,HSSFWorkbook wb){
HSSFSheet sheet = wb.createSheet(excelName);
sheet.setDefaultColumnWidth(columWith);
sheet.setDefaultRowHeightInPoints(rowHight);
// String columWiths[] = columWithMsg.split(",");
// for (int i=0;i<columWiths.length;i++){
// sheet.setColumnWidth(i,Integer.valueOf(columWiths[i])*512);
// }
return sheet;
}
/*
* 列表首頁的大title樣式
*/
private static HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {
// 設置字體
HSSFFont font = workbook.createFont();
//設置字體大小
font.setFontHeightInPoints((short)16);
//字體加粗
// font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//設置字體名字
font.setFontName("Courier New");
//設置樣式;
HSSFCellStyle style = workbook.createCellStyle();
//設置底邊框;
// style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//設置底邊框顏色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//設置左邊框;
// style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//設置左邊框顏色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//設置右邊框;
// style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//設置右邊框顏色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//設置頂邊框;
// style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//設置頂邊框顏色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在樣式用應用設置的字體;
style.setFont(font);
//設置自動換行;
style.setWrapText(false);
//設置水平對齊的樣式爲居中對齊;
// style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//設置垂直對齊的樣式爲居中對齊;
// style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//設置背景顏色
// style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
return style;
}
/*
* 標題 列的樣式
*/
private static HSSFCellStyle getStyle(HSSFWorkbook workbook) {
// 設置字體
HSSFFont font = workbook.createFont();
//設置字體大小
font.setFontHeightInPoints((short)12);
//字體加粗
// font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//設置字體名字
font.setFontName("Courier New");
//設置樣式;
HSSFCellStyle style = workbook.createCellStyle();
//設置底邊框;
// style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//設置底邊框顏色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//設置左邊框;
// style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//設置左邊框顏色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//設置右邊框;
// style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//設置右邊框顏色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//設置頂邊框;
// style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//設置頂邊框顏色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在樣式用應用設置的字體;
style.setFont(font);
//設置自動換行;
style.setWrapText(false);
//設置水平對齊的樣式爲居中對齊;
// style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//設置垂直對齊的樣式爲居中對齊;
// style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//設置背景顏色
// style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
return style;
}
}