Java實現(xiàn)多級表頭和復雜表頭的導出功能
前言
大概內(nèi)容:
多級表頭,復雜表頭的導出功能都可以仿照這個例子去編寫
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、實現(xiàn)的效果

二、調(diào)用工具類
業(yè)務層調(diào)用
//假設這是需要導出的數(shù)據(jù) List<MonitorFoldLineTable> data = new ArrayList(); //調(diào)用導出工具類 FileExportUtil.testExcelDemo(data,response);
對應的實體類
public class MonitorFoldLineTable {
private String cdbh;
private String sd;
private Double dcsg;
private Double dcdsf;
private Double dcc;
private Double ljsg;
private Double ljdsf;
private Double ljc;;
private Double bxsg;
private Double bxdsf;
private Double bxc;
private String time;
}三、詳細工具類
直接copy改吧改吧就可以使用,有詳細注釋
import com.iS3.manager.monitoring.domain.vo.MonitorFoldLineTable;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
/**
* Created with IntelliJ IDEA.
*
* @Author: yangyongzhuo
* @Date: 2021/07/21/8:58
* @Description:
*/
public class FileExportUtil {
/**
* @param list 需要寫入excel的數(shù)據(jù) 從數(shù)據(jù)庫或者其他途徑讀取
* @Description: 導出監(jiān)測對比數(shù)據(jù)的工具類
* @return:
* @Author: yangyongzhuo
* @Date: 2021/7/25 9:29
*/
public static void testExcelDemo(List<MonitorFoldLineTable> list, HttpServletResponse response) {
/** 第一步,創(chuàng)建一個Workbook,對應一個Excel文件 */
XSSFWorkbook wb = new XSSFWorkbook();
/** 第二步,在Workbook中添加一個sheet,對應Excel文件中的sheet */
XSSFSheet sheet = wb.createSheet("excel導出標題");
/** 第三步,設置樣式以及字體樣式*/
XSSFCellStyle titleStyle = createTitleCellStyle(wb);
XSSFCellStyle headerStyle = createHeadCellStyle(wb);
XSSFCellStyle contentStyle = createContentCellStyle(wb);
/** 第四步,創(chuàng)建標題 ,合并標題單元格 */
// 行號
int rowNum = 0;
// 創(chuàng)建第一頁的第一行,索引從0開始
XSSFRow row0 = sheet.createRow(rowNum++);
row0.setHeight((short) 600);// 設置行高
String[] row_Text = {"測點編號", "單次變化量", "", "", "累計變化量", "", "", "變形速率", "", "", "監(jiān)測時間"};
for (int i = 0; i < row_Text.length; i++) {
XSSFCell c00 = row0.createCell(i);
c00.setCellValue(row_Text[i]);
c00.setCellStyle(headerStyle);
}
// 合并單元格,參數(shù)依次為起始列,結(jié)束列,起始行,結(jié)束行 (索引0開始)
sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 0));//標題合并單元格操作,4為總列數(shù)
sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 3));//標題合并單元格操作,4為總列數(shù)
sheet.addMergedRegion(new CellRangeAddress(0, 0, 4, 6));//標題合并單元格操作,4為總列數(shù)
sheet.addMergedRegion(new CellRangeAddress(0, 0, 7, 9));//標題合并單元格操作,4為總列數(shù)
sheet.addMergedRegion(new CellRangeAddress(0, 1, 10, 10));//標題合并單元格操作,4為總列數(shù)
//第二行
XSSFRow row2 = sheet.createRow(rowNum++);
row2.setHeight((short) 700);
String[] row_third = {"", "施工監(jiān)測", "第三方監(jiān)測", "前后差值", "施工監(jiān)測", "第三方監(jiān)測", "前后差值", "施工監(jiān)測", "第三方監(jiān)測", "前后差值"};
for (int i = 0; i < row_third.length; i++) {
XSSFCell tempCell = row2.createCell(i);
tempCell.setCellValue(row_third[i]);
tempCell.setCellStyle(headerStyle);
}
for (MonitorFoldLineTable value : list) {
XSSFRow tempRow = sheet.createRow(rowNum++);
tempRow.setHeight((short) 500);
// 循環(huán)單元格填入數(shù)據(jù)
for (int j = 0; j < 12; j++) {
//列寬自適應,j為自適應的列,true就是自適應,false就是不自適應,默認不自適應
sheet.autoSizeColumn(j, true);
XSSFCell tempCell = tempRow.createCell(j);
tempCell.setCellStyle(contentStyle);
String tempValue = "";
switch (j) {
case 0:
tempValue = value.getCdbh();
break;
case 1:
tempValue = value.getSd();
break;
case 2:
tempValue = value.getDcsg().toString();
break;
case 3:
tempValue = value.getDcdsf().toString();
break;
case 4:
tempValue = value.getDcc().toString();
break;
case 5:
tempValue = value.getLjsg().toString();
break;
case 6:
tempValue = value.getLjdsf().toString();
break;
case 7:
tempValue = value.getLjc().toString();
break;
case 8:
tempValue = value.getBxsg().toString();
break;
case 9:
tempValue = value.getBxdsf().toString();
break;
case 10:
tempValue = value.getBxc().toString();
break;
case 11:
tempValue = value.getTime();
break;
}
tempCell.setCellValue(tempValue);
}
}
//導出到瀏覽器下載
buildExcelDocument("監(jiān)測數(shù)據(jù)", wb, response);
}
/**
* @Description: [導出到瀏覽器]
* @Param: [fileName, wb, response]
* @return: void
* @Author: yangyongzhuo
* @Date: 2021/7/25 9:40
*/
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);
}
}
//---------------------------------------以下是封裝的樣式-----------------------------
/**
* 創(chuàng)建標題樣式
*
* @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("黑體"); // 設置字體類型
headerFont1.setFontHeightInPoints((short) 15); // 設置字體大小
cellStyle.setFont(headerFont1); // 為標題樣式設置字體樣式
return cellStyle;
}
/**
* 創(chuàng)建表頭樣式
*
* @param wb
* @return
*/
private static XSSFCellStyle createHeadCellStyle(XSSFWorkbook wb) {
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setWrapText(true);// 設置自動換行
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("黑體"); // 設置字體類型
headerFont.setFontHeightInPoints((short) 12); // 設置字體大小
cellStyle.setFont(headerFont); // 為標題樣式設置字體樣式
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);// 設置自動換行
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;
}
}以上就是Java實現(xiàn)多級表頭和復雜表頭的導出功能的詳細內(nèi)容,更多關(guān)于Java表頭導出的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java Redis Template批量查詢指定鍵值對的實現(xiàn)
本文主要介紹了Java Redis Template批量查詢指定鍵值對的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07
jvm垃圾回收GC調(diào)優(yōu)基礎(chǔ)原理分析
談到調(diào)優(yōu),這一定是針對特定場景、特定目的的事情, 對于 GC 調(diào)優(yōu)來說,首先就需要清楚調(diào)優(yōu)的目標是什么?從性能的角度看,通常關(guān)注三個方面,內(nèi)存占用(footprint)、延時(latency)和吞吐量(throughput)2022-01-01
IntelliJ IDEA中折疊所有Java代碼,再也不怕大段的代碼了
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中折疊所有Java代碼,再也不怕大段的代碼了,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10
Spring Boot不同版本Redis設置JedisConnectionFactory詳解
本文章向大家介紹Spring Boot不同版本Redis設置JedisConnectionFactory,主要內(nèi)容包括1.X 版本、2.X 版本、2.、基本概念、基礎(chǔ)應用、原理機制和需要注意的事項等,并結(jié)合實例形式分析了其使用技巧,希望通過本文能幫助到大家理解應用這部分內(nèi)容2023-09-09
Java實現(xiàn)駝峰與下劃線互轉(zhuǎn)的方法
這篇文章主要為大家詳細介紹了Java實現(xiàn)駝峰與下劃線互轉(zhuǎn)的方法,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-04-04

