国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

SpringBoot實現excel文件生成和下載

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

使用SpringBoot實現excel生成和下載,生成模板如下

SpringBoot實現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()) + '導入模板').getBytes(), 'ISO8859_1'); //配置請求頭 ServletOutputStream outputStream = response.getOutputStream(); // 組裝附件名稱和格式 response.setHeader('Content-disposition', 'attachment; filename=' + fileName + '.xlsx'); // 創建一個workbook 對應一個excel應用文件 XSSFWorkbook workBook = new XSSFWorkbook(); // 在workbook中添加一個sheet,對應Excel文件中的sheet XSSFSheet sheet = workBook.createSheet('模板'); ExportUtil exportUtil = new ExportUtil(workBook, sheet); XSSFCellStyle headStyle = exportUtil.getHeadStyle(); XSSFCellStyle bodyStyle = exportUtil.getBodyStyle2(); // 構建表頭 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導出工具類

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 表格導出工具類 * * @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); } } } /** * 設置表頭的單元格樣式 * * @return */ public XSSFCellStyle getHeadStyle() { // 創建單元格樣式 XSSFCellStyle cellStyle = wb.createCellStyle(); // // 設置單元格的背景顏色為淡藍色 cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index); cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); // 設置單元格居中對齊 cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER); // 設置單元格垂直居中對齊 cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER); // 創建單元格內容顯示不下時自動換行 // cellStyle.setWrapText(true); // 設置單元格字體樣式 XSSFFont font = wb.createFont(); // 設置字體加粗 font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); font.setFontName('宋體'); // font.setFontHeight((short) 200); cellStyle.setFont(font); // 設置單元格邊框為細線條// cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);// cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);// cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);// cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN); return cellStyle; } /** * 設置表體的單元格樣式 * * @return */ public XSSFCellStyle getBodyStyle2() { // 創建單元格樣式 // 創建單元格樣式 XSSFCellStyle cellStyle = wb.createCellStyle(); // 創建單元格內容顯示不下時自動換行 // cellStyle.setWrapText(true); // 設置單元格字體樣式 XSSFFont font = wb.createFont(); // 設置字體加粗 // font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); font.setFontName('宋體'); font.setFontHeight((short) 200); font.setColor(HSSFColor.BLACK.index); cellStyle.setFont(font); // 設置單元格邊框為細線條 return cellStyle; } /** * 沒有行,就創建行 * * @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; } /** * 如果沒有列,就創建列 * * @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; }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: excel
相關文章:
主站蜘蛛池模板: 成人国产一区二区三区精品 | a理论片 | 国产乱子伦露脸对白在线小说 | 日韩免费黄色片 | 亚洲高清视频在线观看 | 欧美一级毛片免费看高清 | 成人国产永久福利看片 | 国产成人精品日本亚洲专 | 国产成人午夜片在线观看 | 免费国产午夜高清在线视频 | avtt加勒比手机版天堂网 | 美女被靠视频免费网站不需要会员 | 精品丝袜国产自在线拍亚洲 | 日韩字幕一中文在线综合 | 成人免费视频社区 | 国产精品情侣久久婷婷文字 | 日韩欧美综合在线二区三区 | 日本一区二区高清免费不卡 | 国产精品99久久久 | 欧美做a一级视频免费观看 欧美做爱毛片 | 亚洲孕交 | 四色6677最新永久网站 | 亚洲狠狠ady亚洲精品大秀 | 永久免费毛片在线播放 | 亚洲精品二区中文字幕 | 九九re6精品视频在线观看 | 亚洲精品综合在线 | 爽爽爽爽爽爽爽成人免费观看 | 失禁h啪肉尿出来高h男男 | 久久精品国产99久久72 | 国产日韩精品一区在线观看播放 | 黄色影院在线观看视频 | 日本在线免费播放 | 亚洲一区二区三区一品精 | 网红主播大尺度精品福利视频 | 国产片91| 亚洲国内精品自在线影视 | 在线视频精品视频 | 操美女大逼视频 | 97精品在线视频 | 中国老太性色xxxxxhd |