Java實現(xiàn)多級表頭和復(fù)雜表頭的導(dǎo)出功能
前言
大概內(nèi)容:
多級表頭,復(fù)雜表頭的導(dǎo)出功能都可以仿照這個例子去編寫
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、實現(xiàn)的效果
二、調(diào)用工具類
業(yè)務(wù)層調(diào)用
//假設(shè)這是需要導(dǎo)出的數(shù)據(jù) List<MonitorFoldLineTable> data = new ArrayList(); //調(diào)用導(dǎo)出工具類 FileExportUtil.testExcelDemo(data,response);
對應(yīng)的實體類
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: 導(dǎo)出監(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,對應(yīng)一個Excel文件 */ XSSFWorkbook wb = new XSSFWorkbook(); /** 第二步,在Workbook中添加一個sheet,對應(yīng)Excel文件中的sheet */ XSSFSheet sheet = wb.createSheet("excel導(dǎo)出標(biāo)題"); /** 第三步,設(shè)置樣式以及字體樣式*/ XSSFCellStyle titleStyle = createTitleCellStyle(wb); XSSFCellStyle headerStyle = createHeadCellStyle(wb); XSSFCellStyle contentStyle = createContentCellStyle(wb); /** 第四步,創(chuàng)建標(biāo)題 ,合并標(biāo)題單元格 */ // 行號 int rowNum = 0; // 創(chuàng)建第一頁的第一行,索引從0開始 XSSFRow row0 = sheet.createRow(rowNum++); row0.setHeight((short) 600);// 設(shè)置行高 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));//標(biāo)題合并單元格操作,4為總列數(shù) sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 3));//標(biāo)題合并單元格操作,4為總列數(shù) sheet.addMergedRegion(new CellRangeAddress(0, 0, 4, 6));//標(biāo)題合并單元格操作,4為總列數(shù) sheet.addMergedRegion(new CellRangeAddress(0, 0, 7, 9));//標(biāo)題合并單元格操作,4為總列數(shù) sheet.addMergedRegion(new CellRangeAddress(0, 1, 10, 10));//標(biāo)題合并單元格操作,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++) { //列寬自適應(yīng),j為自適應(yīng)的列,true就是自適應(yīng),false就是不自適應(yīng),默認不自適應(yīng) 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); } } //導(dǎo)出到瀏覽器下載 buildExcelDocument("監(jiān)測數(shù)據(jù)", wb, response); } /** * @Description: [導(dǎo)出到瀏覽器] * @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)建標(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; } }
以上就是Java實現(xiàn)多級表頭和復(fù)雜表頭的導(dǎo)出功能的詳細內(nèi)容,更多關(guān)于Java表頭導(dǎo)出的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決Mybatis中result標(biāo)簽識別不了的情況
這篇文章主要介紹了解決Mybatis中result標(biāo)簽識別不了的情況,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。2022-01-01Java Redis Template批量查詢指定鍵值對的實現(xiàn)
本文主要介紹了Java Redis Template批量查詢指定鍵值對的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07jvm垃圾回收GC調(diào)優(yōu)基礎(chǔ)原理分析
談到調(diào)優(yōu),這一定是針對特定場景、特定目的的事情, 對于 GC 調(diào)優(yōu)來說,首先就需要清楚調(diào)優(yōu)的目標(biāo)是什么?從性能的角度看,通常關(guān)注三個方面,內(nèi)存占用(footprint)、延時(latency)和吞吐量(throughput)2022-01-01IntelliJ IDEA中折疊所有Java代碼,再也不怕大段的代碼了
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中折疊所有Java代碼,再也不怕大段的代碼了,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10Spring Boot不同版本Redis設(shè)置JedisConnectionFactory詳解
本文章向大家介紹Spring Boot不同版本Redis設(shè)置JedisConnectionFactory,主要內(nèi)容包括1.X 版本、2.X 版本、2.、基本概念、基礎(chǔ)應(yīng)用、原理機制和需要注意的事項等,并結(jié)合實例形式分析了其使用技巧,希望通過本文能幫助到大家理解應(yīng)用這部分內(nèi)容2023-09-09Java實現(xiàn)駝峰與下劃線互轉(zhuǎn)的方法
這篇文章主要為大家詳細介紹了Java實現(xiàn)駝峰與下劃線互轉(zhuǎn)的方法,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04