亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot實(shí)現(xiàn)excel文件生成和下載

瀏覽:109日期:2022-06-16 10:59:50

使用SpringBoot實(shí)現(xiàn)excel生成和下載,生成模板如下

SpringBoot實(shí)現(xiàn)excel文件生成和下載

controller

@RequestMapping(value = { '/downloadExcelTemplate' }, method = RequestMethod.GET) public String downloadExcelTemplate(HttpSession httpSession, HttpServletResponse response) { try { dealExcelService.downloadExcelTemplate(response); return 'success'; } catch (Exception e) { logger.error('downloadExcelTemplate_error', e); return 'failure'; }}

service

public void downloadExcelTemplate(HttpServletResponse response) throws Exception { //文件名 SimpleDateFormat format3 = new SimpleDateFormat('yyyyMMddHHmm'); String fileName = new String(('文件名' + format3.format(new Date()) + '導(dǎo)入模板').getBytes(), 'ISO8859_1'); //配置請求頭 ServletOutputStream outputStream = response.getOutputStream(); // 組裝附件名稱和格式 response.setHeader('Content-disposition', 'attachment; filename=' + fileName + '.xlsx'); // 創(chuàng)建一個workbook 對應(yīng)一個excel應(yīng)用文件 XSSFWorkbook workBook = new XSSFWorkbook(); // 在workbook中添加一個sheet,對應(yīng)Excel文件中的sheet XSSFSheet sheet = workBook.createSheet('模板'); ExportUtil exportUtil = new ExportUtil(workBook, sheet); XSSFCellStyle headStyle = exportUtil.getHeadStyle(); XSSFCellStyle bodyStyle = exportUtil.getBodyStyle2(); // 構(gòu)建表頭 XSSFRow headRow = ExportUtil.createRow(sheet, 0); XSSFCell cell; String[] titles = {'表頭一', '表頭二', '表頭三'}; int index = 0; for (String title : titles) { cell = ExportUtil.createCell(headRow, index); cell.setCellStyle(headStyle); cell.setCellValue(title); index++; } try { workBook.write(outputStream); outputStream.flush(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } finally { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } }}

ExportUtil導(dǎo)出工具類

package com.shengsheng.utils; import org.apache.poi.hssf.util.HSSFColor;import org.apache.poi.ss.util.CellRangeAddress;import org.apache.poi.xssf.usermodel.*; /** * excel 表格導(dǎo)出工具類 * * @author shengshenglalala */public class ExportUtil { private XSSFWorkbook wb; private XSSFSheet sheet; /** * @param wb * @param sheet */ public ExportUtil(XSSFWorkbook wb, XSSFSheet sheet) { this.wb = wb; this.sheet = sheet; } /** * 合并單元格后給合并后的單元格加邊框 * * @param region * @param cs */ public void setRegionStyle(CellRangeAddress region, XSSFCellStyle cs) { int toprowNum = region.getFirstRow(); for (int i = toprowNum; i <= region.getLastRow(); i++) { XSSFRow row = sheet.getRow(i); for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++) { XSSFCell cell = row.getCell(j); cell.setCellStyle(cs); } } } /** * 設(shè)置表頭的單元格樣式 * * @return */ public XSSFCellStyle getHeadStyle() { // 創(chuàng)建單元格樣式 XSSFCellStyle cellStyle = wb.createCellStyle(); // // 設(shè)置單元格的背景顏色為淡藍(lán)色 cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index); cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); // 設(shè)置單元格居中對齊 cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 設(shè)置單元格垂直居中對齊 cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); // 創(chuàng)建單元格內(nèi)容顯示不下時自動換行 // cellStyle.setWrapText(true); // 設(shè)置單元格字體樣式 XSSFFont font = wb.createFont(); // 設(shè)置字體加粗 font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); font.setFontName('宋體'); // font.setFontHeight((short) 200); cellStyle.setFont(font); // 設(shè)置單元格邊框?yàn)榧?xì)線條// cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);// cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);// cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);// cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN); return cellStyle; } /** * 設(shè)置表體的單元格樣式 * * @return */ public XSSFCellStyle getBodyStyle2() { // 創(chuàng)建單元格樣式 // 創(chuàng)建單元格樣式 XSSFCellStyle cellStyle = wb.createCellStyle(); // 創(chuàng)建單元格內(nèi)容顯示不下時自動換行 // cellStyle.setWrapText(true); // 設(shè)置單元格字體樣式 XSSFFont font = wb.createFont(); // 設(shè)置字體加粗 // font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); font.setFontName('宋體'); font.setFontHeight((short) 200); font.setColor(HSSFColor.BLACK.index); cellStyle.setFont(font); // 設(shè)置單元格邊框?yàn)榧?xì)線條 return cellStyle; } /** * 沒有行,就創(chuàng)建行 * * @param sheet * @param index * @return */ public static XSSFRow createRow(XSSFSheet sheet, Integer index) { XSSFRow row = sheet.getRow(index); if (row == null) { return sheet.createRow(index); } return row; } /** * 如果沒有列,就創(chuàng)建列 * * @param row * @param index * @return */ public static XSSFCell createCell(XSSFRow row, Integer index) { XSSFCell cell = row.getCell(index); if (cell == null) { return row.createCell(index); } return cell; }}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: excel
相關(guān)文章:
主站蜘蛛池模板: 老司机成人福利视频在线观看免费 | 国产一级毛片欧美视频 | 国产高清精品自在久久 | 一级毛片免费视频日本 | 精品一区二区三区在线观看视频 | 久久99国产精一区二区三区 | 欧美一区二区高清 | 91影院在线观看 | 丁香激情综合 | 久久久久网站 | 1024手机最新手机在线 | 色综合久久丁香婷婷 | 国产精品综合久成人 | 国产一区二区三区免费播放 | 亚洲国产精品日韩在线观看 | 欧美色黄毛片 | 国产精品三级a三级三级午夜 | 国产永久免费高清动作片www | 青青草一区国产97 | 国产精品性 | 九九免费精品视频 | 免费观看一区二区 | 真人肉体一级毛片 | 国产视频在线一区 | 国产精品白丝喷水在线观看 | 美女免费观看一区二区三区 | 最近在线更新中文字幕3 | 成人手机看片 | 久热99这里只有精品视频6 | 日本高清免费不卡视频 | 黄色片在线观看网址 | 免费国产精品视频 | 濑亚美莉vs黑人欧美视频 | 韩国一级毛片视频免费观看 | 精品国自产拍天天拍2021 | 久久911 | 麻豆污视频| 一级片一级毛片 | 美女黄色毛片 | 国产伦码精品一区二区 | 一级片二级片 |