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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Springboot导出excel工具类

發(fā)布時間:2023/12/14 javascript 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Springboot导出excel工具类 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1、簡介

這次是使用XSSFWorkbook方法導出。

2、導入依賴

<!--poi依賴--><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.17</version></dependency><dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.3.1</version></dependency>

3、工具類

import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.xssf.usermodel.*;import javax.servlet.http.HttpServletResponse; import java.io.OutputStream; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List;public class ExcelUtils {public static <T> void setExcelProperty(String fileNameParam, HttpServletResponse response, List listTitle, List<T> listContent){SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");OutputStream os = null;XSSFWorkbook xWorkbook = null;try {//定義表格導出時默認文件名 時間戳String fileName = fileNameParam + ".xlsx";os = response.getOutputStream();response.reset();//作用:在前端作用顯示為調用瀏覽器下載彈窗response.setHeader("Content-disposition", "attachment; filename = " + URLEncoder.encode(fileName, "UTF-8"));/*response.setHeader("Content-disposition", "attachment; filename = " + new String(fileName.getBytes(fileName), "ISO8859-1"));*/response.setContentType("application/octet-streem");//創(chuàng)建表格工作空間xWorkbook = new XSSFWorkbook();//創(chuàng)建一個新表格XSSFSheet xSheet = xWorkbook.createSheet(fileNameParam);//set Sheet頁頭部setSheetHeader(xWorkbook, xSheet, listTitle);//set Sheet頁內容setSheetContent(xWorkbook, xSheet, listContent);xWorkbook.write(os);} catch (Exception e) {e.printStackTrace();} finally {if (null != os) {try {os.close();} catch (Exception e) {e.printStackTrace();}}if (null != xWorkbook) {try {xWorkbook.close();} catch (Exception e) {e.printStackTrace();}}}}/*** 配置Excel表格的頂部信息,* @param xWorkbook* @param xSheet*/private static void setSheetHeader(XSSFWorkbook xWorkbook, XSSFSheet xSheet, List listTitle) {//設置表格的寬度 xSheet.setColumnWidth(0, 20 * 256); 中的數(shù)字 20 自行設置為自己適用的/*xSheet.setColumnWidth(0, 20 * 256);xSheet.setColumnWidth(1, 15 * 256);xSheet.setColumnWidth(2, 15 * 256);xSheet.setColumnWidth(3, 20 * 256);*///創(chuàng)建表格的樣式CellStyle cs = xWorkbook.createCellStyle();//設置水平、垂直居中cs.setAlignment(CellStyle.ALIGN_CENTER);cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);//設置字體Font headerFont = xWorkbook.createFont();headerFont.setFontHeightInPoints((short) 12);/*headerFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);*/headerFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);headerFont.setFontName("宋體");cs.setFont(headerFont);cs.setWrapText(true);//是否自動換行//創(chuàng)建一行XSSFRow xRow0 = xSheet.createRow(0);for(int i = 0; i < listTitle.size(); i++){//設置每一列XSSFCell xCell0 = xRow0.createCell(i);xCell0.setCellStyle(cs);xCell0.setCellValue(listTitle.get(i).toString());}}/*** 配置(賦值)表格內容部分* @param xWorkbook* @param xSheet* @param listContent* @throws Exception*/private static <T> void setSheetContent(XSSFWorkbook xWorkbook, XSSFSheet xSheet, List<T> listContent) throws Exception {//創(chuàng)建內容樣式(頭部以下的樣式)CellStyle cs = xWorkbook.createCellStyle();cs.setWrapText(true);//設置水平垂直居中cs.setAlignment(CellStyle.ALIGN_CENTER);cs.setVerticalAlignment(CellStyle.VERTICAL_CENTER);if (null != listContent && listContent.size() > 0) {try {for (int i = 0; i < listContent.size(); i++) {XSSFRow xRow = xSheet.createRow(i + 1);//獲取類屬性Field[] fields = listContent.get(i).getClass().getDeclaredFields();Method method;for (int j = 0; j < fields.length; j++){String str = fields[j].getName();System.out.println(str);//獲取完成get方法 首字母大寫如:getIdmethod = listContent.get(i).getClass().getMethod("get" + str.substring(0,1).toUpperCase() + str.substring(1));String value = (String) method.invoke(listContent.get(i));System.out.println("value" + value);//循環(huán)設置每列的值XSSFCell xCell = xRow.createCell(j);xCell.setCellStyle(cs);xCell.setCellValue(value);}}}catch (NoSuchMethodException e){System.out.println("檢查類set、get命名是否正確:" + e.getStackTrace());}catch (InvocationTargetException e){System.out.println(e.getMessage());}catch (IllegalAccessException e){System.out.println(e.getMessage());}}}}

4、控制層Controller

@ApiOperation(value = "導出信息")@GetMapping("/exportSoft")public void exportSoft(HttpServletResponse response) {//數(shù)據庫查詢要導出的數(shù)據List<SoftListEntity> softList = softService.listAll();String date = new SimpleDateFormat("yyyyMMddHHmm").format(new Date());//定義導出的excel名字String excelName = "軟著信息表" + date;//獲取需要轉出的excel表頭的map字段LinkedHashMap<String, String> fieldMap = new LinkedHashMap<>();fieldMap.put("id", "ID");fieldMap.put("company", "所屬公司");fieldMap.put("softName", "軟著名稱");fieldMap.put("versionNo", "版本號");fieldMap.put("registerNo", "登記號");fieldMap.put("applicationTime", "申請時間");fieldMap.put("location", "位置");fieldMap.put("shorthand", "簡寫");fieldMap.put("entryTime", "錄入時間");//導出用戶相關信息ExportExcelUtils.export(excelName, softList, fieldMap, response);}

總結

以上是生活随笔為你收集整理的Springboot导出excel工具类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。