SpringBoot使用POI進行Excel下載
本文實例為大家分享了SpringBoot使用POI進行Excel下載的具體代碼,供大家參考,具體內(nèi)容如下
使用poi處理Excel特別方便,此處將處理Excel的代碼分享出來。
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");
// 解決導出文件名中文亂碼
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition","attachment;filename="+new String("工資模版".getBytes("UTF-8"),"ISO-8859-1")+".xls");
workbook.write(response.getOutputStream());
}
//導入為模版
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);
//設置首行標題標題
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);
}
請求這個接口,下載下來就是Excel文件。寫的比較簡單,不過看代碼基本就能看懂。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java8中CompletableFuture使用場景與實現(xiàn)原理
CompletableFuture是java8引入的新類,該類實現(xiàn)了Future接口和 CompletionStage接口,封裝了future、forkjoin相關(guān)類來執(zhí)行異步,這篇文章主要給大家介紹了關(guān)于Java8中CompletableFuture使用場景與實現(xiàn)原理的相關(guān)資料,需要的朋友可以參考下2022-02-02
SpringBoot封裝自己的Starter的實現(xiàn)方法
這篇文章主要介紹了SpringBoot封裝自己的Starter的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
快速解決springboot在yml配置了啟動端口但啟動還是8080問題
這篇文章主要介紹了快速解決springboot在yml配置了啟動端口但啟動還是8080問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-03-03
關(guān)于SpringCloud灰度發(fā)布的實現(xiàn)
這篇文章主要介紹了關(guān)于SpringCloud灰度發(fā)布的實現(xiàn),灰度發(fā)布又稱金絲雀發(fā)布,是在系統(tǒng)升級的時候能夠平滑過渡的一種發(fā)布方式,灰度發(fā)布可以保證整體系統(tǒng)的穩(wěn)定,在初始灰度的時候就可以發(fā)現(xiàn)、調(diào)整問題,以保證其影響度,需要的朋友可以參考下2023-08-08
Java中JUC包(java.util.concurrent)下的常用子類
相信大家已經(jīng)對并發(fā)機制中出現(xiàn)的很多的常見知識點進行了總結(jié),下面這篇文章主要給大家介紹了關(guān)于Java中JUC包(java.util.concurrent)下的常用子類的相關(guān)資料,文中通過圖文以及示例代碼介紹的非常詳細,需要的朋友可以參考下2022-12-12
Mybatis-plus設置某個字段值為null的方法總結(jié)
mybatis-plus以下簡稱mp,目前應該也算是主流的一款數(shù)據(jù)訪問層應用框架,下面這篇文章主要給大家介紹了關(guān)于Mybatis-plus設置某個字段值為null的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-07-07

