java poi實現(xiàn)Excel多級表頭導(dǎo)出方式(多級表頭,復(fù)雜表頭)
java poi實現(xiàn)Excel多級表頭導(dǎo)出(多級表頭,復(fù)雜表頭)
最近碰到一個導(dǎo)出,比較繁瑣,也查詢了許多博客,在其中一篇博客的基礎(chǔ)上修改,實現(xiàn)了最終想要的效果。
話不多說,直接上效果圖:

上代碼
1.主代碼
List<Map<String, Object>> convertListMap = toListConvertListMap(tableTitleList, list);
// 第一步,創(chuàng)建一個Workbook,對應(yīng)一個Excel文件
XSSFWorkbook wb = new XSSFWorkbook();
// 第二步,在Workbook中添加一個sheet,對應(yīng)Excel文件中的sheet
XSSFSheet sheet = wb.createSheet("sheet");
// 第三步,設(shè)置樣式以及字體樣式
XSSFCellStyle titleStyle = createTitleCellStyle(wb);
XSSFCellStyle headerStyle = createHeadCellStyle(wb);
XSSFCellStyle contentStyle = createContentCellStyle(wb);
// 行號
int rowNum = 0;
// 創(chuàng)建第一頁的第一行,索引從0開始
XSSFRow row0 = sheet.createRow(rowNum++);
row0.setHeight((short) 600);// 設(shè)置行高
sheet.setColumnWidth(2, 8000);
sheet.setColumnWidth(3, 8000);
//第二行
XSSFRow row2 = sheet.createRow(rowNum++);
row2.setHeight((short) 700);
// todo: 第三行
XSSFRow row3 = sheet.createRow(rowNum++);
row3.setHeight((short) 700);
String[] row_one = null;
String[] row_two = null;
String[] row_three = null;
int cellSize = 0;
if (tableTitleList.size() == 2) { // 全部
row_one = new String[]{"序號", "所屬模塊", "單位名稱", "情況統(tǒng)計", "合計", "", "",
"測試類型A", "", "", "", "", "", "", "", "", "", "", "",
"測試類型B", "", "", "", "", "", "", "", "", "", "", "",};
row_two = new String[]{"", "", "", "", "合計A", "合計B", "合計C",
"測試類型A1", "", "", "",
"測試類型A2", "", "", "",
"測試類型A3", "", "", "",
"測試類型B1", "", "", "",
"測試類型B2", "", "", "",
"測試類型B3", "", "", ""};
row_three = new String[]{"", "", "", "", "", "", "",
"起床", "洗漱", "吃飯", "上班",
"起床", "洗漱", "吃飯", "上班",
"起床", "洗漱", "吃飯", "上班",
"起床", "洗漱", "吃飯", "上班",
"起床", "洗漱", "吃飯", "上班",
"起床", "洗漱", "吃飯", "上班",};
// 合并單元格
extracted(sheet);
}
// 創(chuàng)建第一行單元格
for (int i = 0; i < row_one.length; i++) {
XSSFCell c00 = row0.createCell(i);
c00.setCellValue(row_one[i]);
c00.setCellStyle(headerStyle);
}
// 創(chuàng)建第二行單元格
for (int i = 0; i < row_two.length; i++) {
XSSFCell tempCell = row2.createCell(i);
tempCell.setCellValue(row_two[i]);
tempCell.setCellStyle(headerStyle);
}
// 創(chuàng)建第三行單元格
for (int i = 0; i < row_three.length; i++) {
XSSFCell tempCell = row3.createCell(i);
tempCell.setCellValue(row_three[i]);
tempCell.setCellStyle(headerStyle);
}
// 設(shè)置表頭單元格的寬度
tableTitleStyleColumnwidth(sheet);
// todo: 填充數(shù)據(jù)
approvalMethodfillInData(convertListMap, sheet, contentStyle, rowNum, row_three, tableTitleList);
// approvalMethodfillInData(convertListMap, sheet, contentStyle, rowNum, cellSize, tableTitleList);
//導(dǎo)出到瀏覽器下載
buildExcelDocument("監(jiān)測數(shù)據(jù).xlsx", wb, response);2.合并單元格
private static void extracted(XSSFSheet sheet) {
// todo: 合并參數(shù)分別為: 起始行,結(jié)束行,起始列,結(jié)束列
// 將第一列 的 第一行到第三行合并
sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 0));
// 將第二列的 第一行到第三行合并
sheet.addMergedRegion(new CellRangeAddress(0, 2, 1, 1));
// 將第三列的 第一行到第三行合并
sheet.addMergedRegion(new CellRangeAddress(0, 2, 2, 2));
sheet.addMergedRegion(new CellRangeAddress(0, 2, 3, 3));
// 合計
sheet.addMergedRegion(new CellRangeAddress(0, 0, 4, 6));
// 合計下的單元格
sheet.addMergedRegion(new CellRangeAddress(1, 2, 4, 4));
sheet.addMergedRegion(new CellRangeAddress(1, 2, 5, 5));
sheet.addMergedRegion(new CellRangeAddress(1, 2, 6, 6));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 7, 18));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 19, 30));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 7, 10));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 11, 14));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 15, 18));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 19, 22));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 23, 26));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 27, 30));
}3.設(shè)置表頭單元格的寬度
private static void tableTitleStyleColumnwidth(XSSFSheet sheet) {
sheet.setColumnWidth(2, 8000);
sheet.setColumnWidth(3, 8000);
sheet.setColumnWidth(7, 3000);
sheet.setColumnWidth(8, 3000);
sheet.setColumnWidth(9, 3000);
sheet.setColumnWidth(10, 3000);
sheet.setColumnWidth(11, 3000);
sheet.setColumnWidth(12, 3000);
sheet.setColumnWidth(13, 3000);
sheet.setColumnWidth(14, 3000);
sheet.setColumnWidth(15, 3000);
sheet.setColumnWidth(16, 3000);
sheet.setColumnWidth(17, 3000);
sheet.setColumnWidth(18, 3000);
sheet.setColumnWidth(19, 3000);
sheet.setColumnWidth(20, 3000);
sheet.setColumnWidth(21, 3000);
sheet.setColumnWidth(22, 3000);
sheet.setColumnWidth(23, 3000);
sheet.setColumnWidth(24, 3000);
sheet.setColumnWidth(25, 3000);
sheet.setColumnWidth(26, 3000);
sheet.setColumnWidth(27, 3000);
sheet.setColumnWidth(28, 3000);
sheet.setColumnWidth(29, 3000);
sheet.setColumnWidth(30, 3000);
}4.填充數(shù)據(jù)
(注:我這里的數(shù)據(jù)格式是List<Map<String, Object>>類型,可以根據(jù)自己的實際情況來封裝數(shù)據(jù))
private static void approvalMethodfillInData(List<Map<String, Object>> convertListMap, XSSFSheet sheet, XSSFCellStyle contentStyle, int rowNum, String[] cellSize, List<Object> tableTitleList) {
for (Map<String, Object> map : convertListMap) {
XSSFRow tempRow = sheet.createRow(rowNum++);
tempRow.setHeight((short) 500);
// 判斷選擇類型
if (tableTitleList.size() == 2 || "enterprise".equals(tableTitleList.get(0).toString())) {
// 循環(huán)單元格填入數(shù)據(jù)
for (int i = 0; i < cellSize.length; i++) {
//列寬自適應(yīng),j為自適應(yīng)的列,true就是自適應(yīng),false就是不自適應(yīng),默認(rèn)不自適應(yīng)
sheet.autoSizeColumn(i, true);
XSSFCell tempCell = tempRow.createCell(i);
tempCell.setCellStyle(contentStyle);
String tempValue = "";
switch (i) {
case 0:
tempValue = map.get("indexNum").toString();
break;
case 1:
tempValue = map.get("platName").toString();
break;
case 2:
tempValue = map.get("deptName").toString();
break;
case 3:
tempValue = map.get("type").toString();
break;
case 4:
tempValue = map.get("totalProNum").toString();
break;
case 5:
tempValue = map.get("totalBidNum").toString();
break;
case 6:
tempValue = map.get("totalAmount").toString();
break;
case 7:
tempValue = map.get("enterpriseEngineeringProNum").toString();
break;
case 8:
tempValue = map.get("enterpriseEngineeringBidNum").toString();
break;
case 9:
tempValue = map.get("enterprisePurchaseAmount").toString();
break;
case 10:
tempValue = map.get("planEnterprisePurchaseAmount").toString();
break;
case 11:
tempValue = map.get("enterprisePurchaseProNum").toString();
break;
case 12:
tempValue = map.get("enterprisePurchaseBidNum").toString();
break;
case 13:
tempValue = map.get("enterprisePurchaseAmount").toString();
break;
case 14:
tempValue = map.get("planEnterprisePurchaseAmount").toString();
break;
case 15:
tempValue = map.get("enterpriseServiceProNum").toString();
break;
case 16:
tempValue = map.get("enterpriseServiceBidNum").toString();
break;
case 17:
tempValue = map.get("enterpriseServiceAmount").toString();
break;
case 18:
tempValue = map.get("planEnterpriseServiceAmount").toString();
break;
case 19:
tempValue = map.get("groupEngineeringProNum").toString();
break;
case 20:
tempValue = map.get("grouppurchaseBidNum").toString();
break;
case 21:
tempValue = map.get("groupPurchaseAmount").toString();
break;
case 22:
tempValue = map.get("palnGroupPurchaseAmount").toString();
break;
case 23:
tempValue = map.get("groupPurchaseProNum").toString();
break;
case 24:
tempValue = map.get("groupEngineeringBidNum").toString();
break;
case 25:
tempValue = map.get("groupEngineeringAmount").toString();
break;
case 26:
tempValue = map.get("planGroupEngineeringAmount").toString();
break;
case 27:
tempValue = map.get("groupServiceProNum").toString();
break;
case 28:
tempValue = map.get("groupServiceBidNum").toString();
break;
case 29:
tempValue = map.get("groupServiceAmount").toString();
break;
case 30:
tempValue = map.get("planGroupServiceAmount").toString();
break;
}
tempCell.setCellValue(tempValue);
}
}
}5.web下載,導(dǎo)出到瀏覽器
private static void buildExcelDocument(String fileName, Workbook wb, HttpServletResponse response) {
try {
response.setContentType("application/octet-stream");
// 可自行定義編碼格式
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
//清除jsp編譯html文件的空白,防止excel出現(xiàn)空行
response.flushBuffer();
//寫出
wb.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(wb);
}
}6.提供表頭樣式,內(nèi)容樣式以及標(biāo)題樣式
/**
* 創(chuàng)建標(biāo)題樣式
*
* @param wb
* @return
*/
private static XSSFCellStyle createTitleCellStyle(XSSFWorkbook wb) {
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直對齊
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// cellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());//背景顏色
XSSFFont headerFont1 = (XSSFFont) wb.createFont(); // 創(chuàng)建字體樣式
headerFont1.setBold(true); //字體加粗
headerFont1.setFontName("黑體"); // 設(shè)置字體類型
headerFont1.setFontHeightInPoints((short) 15); // 設(shè)置字體大小
cellStyle.setFont(headerFont1); // 為標(biāo)題樣式設(shè)置字體樣式
return cellStyle;
}
/**
* 創(chuàng)建表頭樣式
*
* @param wb
* @return
*/
private static XSSFCellStyle createHeadCellStyle(XSSFWorkbook wb) {
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setWrapText(true);// 設(shè)置自動換行
cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景顏色
cellStyle.setAlignment(HorizontalAlignment.CENTER); //水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //垂直對齊
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// cellStyle.setBottomBorderColor(IndexedColors.BLACK.index);
cellStyle.setBorderBottom(BorderStyle.THIN); //下邊框
cellStyle.setBorderLeft(BorderStyle.THIN); //左邊框
cellStyle.setBorderRight(BorderStyle.THIN); //右邊框
cellStyle.setBorderTop(BorderStyle.THIN); //上邊框
XSSFFont headerFont = (XSSFFont) wb.createFont(); // 創(chuàng)建字體樣式
headerFont.setBold(true); //字體加粗
headerFont.setFontName("黑體"); // 設(shè)置字體類型
headerFont.setFontHeightInPoints((short) 12); // 設(shè)置字體大小
cellStyle.setFont(headerFont); // 為標(biāo)題樣式設(shè)置字體樣式
return cellStyle;
}
/**
* 創(chuàng)建內(nèi)容樣式
*
* @param wb
* @return
*/
private static XSSFCellStyle createContentCellStyle(XSSFWorkbook wb) {
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
cellStyle.setAlignment(HorizontalAlignment.CENTER);// 水平居中
cellStyle.setWrapText(true);// 設(shè)置自動換行
cellStyle.setBorderBottom(BorderStyle.THIN); //下邊框
cellStyle.setBorderLeft(BorderStyle.THIN); //左邊框
cellStyle.setBorderRight(BorderStyle.THIN); //右邊框
cellStyle.setBorderTop(BorderStyle.THIN); //上邊框
// 生成12號字體
XSSFFont font = wb.createFont();
font.setColor((short) 8);
font.setFontHeightInPoints((short) 12);
cellStyle.setFont(font);
return cellStyle;
}到此,大功告成!
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot啟動過程(四)之Spring Boot內(nèi)嵌Tomcat啟動
這篇文章主要介紹了Spring Boot啟動過程(四)之Spring Boot內(nèi)嵌Tomcat啟動的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-04-04
使用jd-gui反編譯修改jar包里的.class并重新生成新jar問題
這篇文章主要介紹了使用jd-gui反編譯修改jar包里的.class并重新生成新jar問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05
Spring AI內(nèi)置DeepSeek的詳細(xì)步驟
Spring AI 最新快照版已經(jīng)內(nèi)置 DeepSeek 了,所以以后項目中對接 DeepSeek 就方便多了,但因為快照版會有很多 Bug,所以今天咱們就來看穩(wěn)定版的 Spring AI 如何對接 DeepSeek 滿血版,感興趣的小伙伴跟著小編一起來看看吧2025-02-02
java實現(xiàn)圖片角度旋轉(zhuǎn)并獲得圖片信息
這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)圖片角度旋轉(zhuǎn)并獲得圖片信息,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-02-02
IDEA2020.1構(gòu)建Spring5.2.x源碼的方法
這篇文章主要介紹了IDEA2020.1構(gòu)建Spring5.2.x源碼的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
SpringBoot實現(xiàn)返回值數(shù)據(jù)脫敏的步驟詳解
這篇文章主要給大家介紹一下SpringBoot實現(xiàn)返回值數(shù)據(jù)脫敏的步驟,文章通過代碼示例介紹的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下2023-07-07
Java開發(fā)者必備10大數(shù)據(jù)工具和框架
這篇文章主要為大家詳細(xì)介紹了Java開發(fā)者必備10大數(shù)據(jù)工具和框架,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06

