日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

ashx获取客户端excel文件_Spring Boot实现导出Excel功能

發(fā)布時間:2024/10/5 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ashx获取客户端excel文件_Spring Boot实现导出Excel功能 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

同樣的,看你要導(dǎo)出的Excel后綴是xls格式還是xlsx格式,不同的格式代碼稍微有些不同,下面是導(dǎo)出xlsx格式的Excel。

Maven:

org.apache.poi poi 3.9org.apache.poi poi-ooxml 3.9

Controller層

/** * 導(dǎo)出Excel * @param pageable * @param date * @param response * @throws IOException */ @GetMapping("test/export") public void exportDormCheckScore(@PageableDefault(size = 1000000) Pageable pageable, @RequestParam(value = "date",required = false) String date, HttpServletResponse response) throws IOException { Page page=testService.findTestList(pageable,date); //分頁查詢出來的數(shù)據(jù) List list=page.getContent(); //獲取數(shù)據(jù) XSSFWorkbook wb = ExcelUtils.createdTestExcel(list); //生成Excel文件 //響應(yīng)到客戶端 this.setResponseHeader(response, "測試" + System.currentTimeMillis() + ".xlsx"); //Excel文件命名 OutputStream os = response.getOutputStream(); wb.write(os); os.flush(); os.close(); } /** * 設(shè)置響應(yīng)流 * * @param response * @param fileName */ public void setResponseHeader(HttpServletResponse response, String fileName) throws IOException { fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1"); response.setCharacterEncoding("utf-8"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName); }

ExcelUtilscreatedTestExcel生成Excel方法:

/** * 創(chuàng)建excel文件 * * @param list * @return */public static XSSFWorkbook createdTestExcel(List list) {XSSFWorkbook workbook = new XSSFWorkbook();XSSFSheet sheet = workbook.createSheet();CellStyle headsStyle = workbook.createCellStyle();// 創(chuàng)建一個居中格式headsStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//設(shè)置表頭背景顏色headsStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index);headsStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//下邊框headsStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//左邊框headsStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//上邊框headsStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//右邊框headsStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);XSSFFont font = workbook.createFont();font.setFontName("黑體");//設(shè)置字體大小font.setFontHeightInPoints((short) 12);headsStyle.setFont(font);XSSFCellStyle borderStyle = workbook.createCellStyle();borderStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//下邊框borderStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);//左邊框borderStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//上邊框borderStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//右邊框borderStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);XSSFFont borderFont = workbook.createFont();borderFont.setFontName("黑體");//設(shè)置字體大小borderFont.setFontHeightInPoints((short) 11);borderStyle.setFont(borderFont);XSSFRow row = sheet.createRow(0);Integer columnNum = 0;//第一行表頭字段for (ExportTestColumnsEnum column : ExportTestColumnsEnum.values()) {XSSFCell cell = row.createCell(columnNum);cell.setCellValue(column.getColumnName());cell.setCellStyle(headsStyle);sheet.setColumnWidth(columnNum, 20 * 256);columnNum++;}Integer dataColumnNum = 1;//計算序號的計數(shù)器int count=1; //序號//開始生成數(shù)據(jù)for (ReturnVO returnVO : list) {XSSFRow rows = sheet.createRow(dataColumnNum);doSetColumnValue(rows, borderStyle, 0, count); //序號count++;doSetColumnValue(rows, borderStyle, 1, returnVO.getName()); //姓名doSetColumnValue(rows, borderStyle, 2, returnVO.getAge()); //年齡doSetColumnValue(rows, borderStyle, 3, returnVO.getDate()); //出身日期doSetColumnValue(rows, borderStyle, 4,returnVO.getHeight()); //身高dataColumnNum++;}return workbook;}

表頭字段枚舉類ExportTestColumnsEnum

@Getter@AllArgsConstructorpublic enum ExportTestColumnsEnum { /** * 列 */ ROW1("序號"), ROW2("姓名"), ROW3("年齡"), ROW4("性別"), ROW5("出身日期"), ROW16("身高"), ; private String columnName; }

上面就生成了xlsx格式的Excel文件,至于如何生成xls格式的Excel文件也很簡單,只需要把XSSFWorkbook、XSSFSheet、XSSFRow這些以“XSSF”為前綴的改為“HSSF”就可以生成xls版本的Excel文件了。

總結(jié)

以上是生活随笔為你收集整理的ashx获取客户端excel文件_Spring Boot实现导出Excel功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。