您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在SpringBoot中利用POI下載Excel文件,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
1.maven引用
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency>
2.service邏輯代碼
/** * 獲取下載模版 */ public void salaryTemplate(HttpServletResponse response)throws Exception{ HSSFWorkbook workbook = new HSSFWorkbook(); exportExcel(workbook); response.setHeader("Content-type","application/vnd.ms-excel"); // 解決導(dǎo)出文件名中文亂碼 response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Disposition","attachment;filename="+new String("工資模版".getBytes("UTF-8"),"ISO-8859-1")+".xls"); workbook.write(response.getOutputStream()); } //導(dǎo)入為模版 private void exportExcel(HSSFWorkbook workbook) throws Exception { //創(chuàng)建創(chuàng)建sheet HSSFSheet sheet = workbook.createSheet("工資"); //創(chuàng)建單元格樣式 CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index); //設(shè)置首行標(biāo)題標(biāo)題 HSSFRow headerRow = sheet.createRow(0); headerRow.createCell(0).setCellStyle(cellStyle); headerRow.createCell(0).setCellValue("工號"); headerRow.createCell(1).setCellStyle(cellStyle); headerRow.createCell(1).setCellValue("姓名"); headerRow.createCell(2).setCellStyle(cellStyle); headerRow.createCell(2).setCellValue("年齡"); //創(chuàng)建三行數(shù)據(jù) HSSFRow row; for (int i = 0; i <4; i++) { row = sheet.createRow(i + 1); row.createCell(0).setCellStyle(cellStyle); row.createCell(0).setCellValue(i); row.createCell(1).setCellStyle(cellStyle); row.createCell(1).setCellValue("張三"); row.createCell(2).setCellStyle(cellStyle); row.createCell(2).setCellValue(19); } }
3.controller
@GetMapping("/salary/template") public void salaryTemplate(HttpServletResponse response)throws Exception{ salaryService.salaryTemplate(response); }
上述就是小編為大家分享的怎么在SpringBoot中利用POI下載Excel文件了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。