SpringBoot集成POI導(dǎo)出Execl表格之統(tǒng)一工具類
最近項目需求中有需要導(dǎo)出Execl表格的要求,而且還是大量的數(shù)據(jù),于是把之前的整理了一下,抽成了一個統(tǒng)一的工具類,需要時直接調(diào)用工具類即可,代碼如下:
一、 先看下效果
二、 導(dǎo)入依賴
<properties> ? ? ? ? <poi.version>4.1.2</poi.version> ? ? ? ? <easypoi.version>3.2.0</easypoi.version> ? ? </properties> ? ? ? ? ? ? <!-- easypoi 改jar包,在單體項目沒啥問題,但在微服務(wù)項目中出現(xiàn)問題,因此調(diào)用了下面三個, ? ? ? ? ? ? 大家如果解決了該問題可以告知一下互相學(xué)習(xí)--> <!-- ? ? ? ?<dependency>--> <!-- ? ? ? ? ? ?<groupId>cn.afterturn</groupId>--> <!-- ? ? ? ? ? ?<artifactId>easypoi-base</artifactId>--> <!-- ? ? ? ? ? ?<version>${easypoi.version}</version>--> <!-- ? ? ? ?</dependency>--> ? ? ? ? <!-- poi start --> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.apache.poi</groupId> ? ? ? ? ? ? <artifactId>poi</artifactId> ? ? ? ? ? ? <version>${poi.version}</version> ? ? ? ? </dependency> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.apache.poi</groupId> ? ? ? ? ? ? <artifactId>poi-ooxml</artifactId> ? ? ? ? ? ? <version>${poi.version}</version> ? ? ? ? </dependency> ? ? ? ? <dependency> ? ? ? ? ? ? <groupId>org.apache.poi</groupId> ? ? ? ? ? ? <artifactId>poi-ooxml-schemas</artifactId> ? ? ? ? ? ? <version>${poi.version}</version> ? ? ? ? </dependency> ? ? ? ? <!-- poi end -->
三、統(tǒng)一工具類
集成了xlsx和xls兩種方式導(dǎo)出;
import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFPalette; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFFont; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; ? import javax.servlet.http.HttpServletResponse; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Method; import java.net.URLEncoder; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; ? /** ?* Exexl導(dǎo)出工具類 ?*/ public class ExcelExportUtil { ? ? private static final Logger logger = LoggerFactory.getLogger(ExcelExportUtil.class); ? ? //響應(yīng) ? ? HttpServletResponse response; ? ? // 文件名 ? ? private String fileName; ? ? //文件保存路徑 ? ? private String fileDir; ? ? //sheet名 ? ? private String sheetName; ? ? //表頭字體 ? ? private String titleFontType = "Arial Unicode MS"; ? ? //表頭背景色 ? ? private String titleBackColor = "C1FBEE"; ? ? //表頭字號 ? ? private short titleFontSize = 12; ? ? //添加自動篩選的列 如 A:M ? ? private String address = ""; ? ? //正文字體 ? ? private String contentFontType = "Arial Unicode MS"; ? ? //正文字號 ? ? private short contentFontSize = 12; ? ? //Float類型數(shù)據(jù)小數(shù)位 ? ? private String floatDecimal = "0.00"; ? ? //Double類型數(shù)據(jù)小數(shù)位 ? ? private String doubleDecimal = "0.00"; ? ? //時間類型 ? ? private String parsePattern = "yyyy-MM-dd HH:mm:ss"; ? ? //設(shè)置列的公式 ? ? private String colFormula[] = null; ? ? SimpleDateFormat dateFormat = new SimpleDateFormat(parsePattern); ? ? DecimalFormat floatDecimalFormat = new DecimalFormat(floatDecimal); ? ? DecimalFormat doubleDecimalFormat = new DecimalFormat(doubleDecimal); ? ? ? private HSSFWorkbook workbook = null; ? ? ? public ExcelExportUtil() { ? ? ? ? workbook = new HSSFWorkbook(); ? ? } ? ? public ExcelExportUtil(HttpServletResponse response, String fileName, String sheetName) { ? ? ? ? this.fileName = fileName; ? ? ? ? this.response = response; ? ? ? ? this.sheetName = sheetName; ? ? ? ? workbook = new HSSFWorkbook(); ? ? } ? ? ? /** ? ? ?* 設(shè)置表頭字體. ? ? ?* @param titleFontType ? ? ?*/ ? ? public void setTitleFontType(String titleFontType) { ? ? ? ? this.titleFontType = titleFontType; ? ? } ? ? /** ? ? ?* 設(shè)置表頭背景色. ? ? ?* @param titleBackColor 十六進(jìn)制 ? ? ?*/ ? ? public void setTitleBackColor(String titleBackColor) { ? ? ? ? this.titleBackColor = titleBackColor; ? ? } ? ? /** ? ? ?* 設(shè)置表頭字體大小. ? ? ?* @param titleFontSize ? ? ?*/ ? ? public void setTitleFontSize(short titleFontSize) { ? ? ? ? this.titleFontSize = titleFontSize; ? ? } ? ? /** ? ? ?* 設(shè)置表頭自動篩選欄位,如A:AC. ? ? ?* @param address ? ? ?*/ ? ? public void setAddress(String address) { ? ? ? ? this.address = address; ? ? } ? ? /** ? ? ?* 設(shè)置正文字體. ? ? ?* @param contentFontType ? ? ?*/ ? ? public void setContentFontType(String contentFontType) { ? ? ? ? this.contentFontType = contentFontType; ? ? } ? ? /** ? ? ?* 設(shè)置正文字號. ? ? ?* @param contentFontSize ? ? ?*/ ? ? public void setContentFontSize(short contentFontSize) { ? ? ? ? this.contentFontSize = contentFontSize; ? ? } ? ? /** ? ? ?* 設(shè)置float類型數(shù)據(jù)小數(shù)位 默認(rèn).00 ? ? ?* @param doubleDecimal 如 ".00" ? ? ?*/ ? ? public void setDoubleDecimal(String doubleDecimal) { ? ? ? ? this.doubleDecimal = doubleDecimal; ? ? } ? ? /** ? ? ?* 設(shè)置doubel類型數(shù)據(jù)小數(shù)位 默認(rèn).00 ? ? ?* @param floatDecimalFormat 如 ".00 ? ? ?*/ ? ? public void setFloatDecimalFormat(DecimalFormat floatDecimalFormat) { ? ? ? ? this.floatDecimalFormat = floatDecimalFormat; ? ? } ? ? /** ? ? ?* 設(shè)置列的公式 ? ? ?* @param colFormula 存儲i-1列的公式 涉及到的行號使用@替換 如A@+B@ ? ? ?*/ ? ? public void setColFormula(String[] colFormula) { ? ? ? ? this.colFormula = colFormula; ? ? } ? ? ? /** ? ? ?* 將16進(jìn)制的顏色代碼寫入樣式中來設(shè)置顏色 ? ? ?* ? ? ?* @param style 保證style統(tǒng)一 ? ? ?* @param color 顏色:66FFDD ? ? ?* @param index 索引 8-64 使用時不可重復(fù) ? ? ?* @return ? ? ?*/ ? ? public CellStyle setColor(CellStyle style, String color, short index) { ? ? ? ? if ("".equals(color)) { ? ? ? ? ? ? //轉(zhuǎn)為RGB碼 ? ? ? ? ? ? int r = Integer.parseInt((color.substring(0, 2)), 16); ? //轉(zhuǎn)為16進(jìn)制 ? ? ? ? ? ? int g = Integer.parseInt((color.substring(2, 4)), 16); ? ? ? ? ? ? int b = Integer.parseInt((color.substring(4, 6)), 16); ? ? ? ? ? ? //自定義cell顏色 ? ? ? ? ? ? HSSFPalette palette = workbook.getCustomPalette(); ? ? ? ? ? ? palette.setColorAtIndex((short) index, (byte) r, (byte) g, (byte) b); ? ? ? ? ? ? ? style.setFillPattern(FillPatternType.SOLID_FOREGROUND); ? ? ? ? ? ? style.setFillForegroundColor(index); ? ? ? ? } ? ? ? ? return style; ? ? } ? ? ? ? /** ? ? ?* 設(shè)置字體并加外邊框 ? ? ?* ? ? ?* @param style 樣式 ? ? ?* @param style 字體名 ? ? ?* @param style 大小 ? ? ?* @return ? ? ?*/ ? ? public CellStyle setFontAndBorder(CellStyle style, String fontName, short size) { ? ? ? ? HSSFFont font = workbook.createFont(); ? ? ? ? font.setFontHeightInPoints(size); ? ? ? ? font.setFontName(fontName); ? ? ? ? font.setBold(true); ? ? ? ? style.setFont(font); ? ? ? ? style.setBorderBottom(BorderStyle.THIN); //下邊框 ? ? ? ? style.setBorderLeft(BorderStyle.THIN);//左邊框 ? ? ? ? style.setBorderTop(BorderStyle.THIN);//上邊框 ? ? ? ? style.setBorderRight(BorderStyle.THIN);//右邊框 ? ? ? ? return style; ? ? } ? ? ? /** ? ? ?* SXSSFWorkbook 設(shè)置表頭字體加粗 和 大小 ? ? ?*/ ? ? public CellStyle setTitleNameFont(SXSSFWorkbook workbook, CellStyle style, String fontName, short size){ ? ? ? ? XSSFFont font = (XSSFFont) workbook.createFont(); ? ? ? ? font.setFontHeightInPoints(size); ? ? ? ? font.setFontName(fontName); ? ? ? ? font.setBold(true); ? ? ? ? style.setFont(font); ? ? ? ? return style; ? ? } ? ? ? /** ? ? ?* xlsx方式(適合導(dǎo)出大量數(shù)據(jù),拆分多個sheet) ? ? ?* @param response 響應(yīng)流 ? ? ?* @param fileName 文件名稱 ? ? ?* @param titleColumn 標(biāo)題列名稱對象(如:name) ? ? ?* @param titleName 標(biāo)題列名稱對象描述(如:名字) ? ? ?* @param titleSize 標(biāo)題大小 ? ? ?* @param dataList ?數(shù)據(jù)源 ? ? ?*/ ? ? public void writeBigExcel(HttpServletResponse response, String fileName, String titleColumn[], ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String titleName[], int titleSize[], List<?> dataList) { ? ? ? ? try { ? ? ? ? ? ? SXSSFWorkbook workbook = new SXSSFWorkbook(100); ? ? ? ? ? ? OutputStream out = response.getOutputStream(); ? ? ? ? ? ? String lastFileName = fileName + ".xlsx"; ? ? ? ? ? ? response.setContentType("application/msexcel;charset=UTF-8"); ? ? ? ? ? ? response.setHeader("Content-Disposition", "attachment; filename=" ? ? ? ? ? ? ? ? ? ? + URLEncoder.encode(lastFileName, "UTF-8")); ? ? ? ? ? ? int k = 0; ? ? ? ? ? ? int rowIndex; ? ? ? ? ? ? Sheet sheet = workbook.createSheet(fileName + (k + 1)); ? ? ? ? ? ? //寫入excel的表頭 ? ? ? ? ? ? Row titleNameRow = workbook.getSheet(fileName + (k + 1)).createRow(0); ? ? ? ? ? ? //設(shè)置樣式 ? ? ? ? ? ? CellStyle ?titleStyle = ?workbook.createCellStyle(); ? ? ? ? ? ? titleStyle = ?setTitleNameFont(workbook, titleStyle, "Arial Unicode MS", (short) 12); ? ? ? ? ? ? for (int i = 0; i < titleName.length; i++) { ? ? ? ? ? ? ? ? sheet.setColumnWidth(i, titleSize[i] * 256); ? ?//設(shè)置寬度 ? ? ? ? ? ? ? ? Cell cell = titleNameRow.createCell(i); ? ? ? ? ? ? ? ? cell.setCellStyle(titleStyle); ? ? ? ? ? ? ? ? cell.setCellValue(titleName[i]); ? ? ? ? ? ? } ? ? ? ? ? ? //寫入到excel中 ? ? ? ? ? ? if (dataList != null && dataList.size() > 0) { ? ? ? ? ? ? ? ? if (titleColumn.length > 0) { ? ? ? ? ? ? ? ? ? ? for (int index = 0; index < dataList.size(); index++) { ? ? ? ? ? ? ? ? ? ? ? ? //每個sheet3W條數(shù)據(jù) ? ? ? ? ? ? ? ? ? ? ? ? if (index != 0 && (index) % 30000 == 0) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? k = k + 1; ? ? ? ? ? ? ? ? ? ? ? ? ? ? sheet = workbook.createSheet(fileName + (k + 1)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? //寫入excel的表頭 ? ? ? ? ? ? ? ? ? ? ? ? ? ? titleNameRow = workbook.getSheet(fileName + (k + 1)).createRow(0); ? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = 0; i < titleName.length; i++) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sheet.setColumnWidth(i, titleSize[i] * 256); ? ?//設(shè)置寬度 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Cell cell = titleNameRow.createCell(i); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellStyle(titleStyle); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(titleName[i]); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? if (index < 30000) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? rowIndex = index + 1; ? ? ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? rowIndex = index - 30000 * ((index) / 30000) + 1; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? Object obj = dataList.get(index); ? ? ? ? ? ? ? ? ? ? ? ? Class clazz = obj.getClass(); ? ? ? ? ? ? ? ? ? ? ? ? Row dataRow = workbook.getSheet(fileName + (k + 1)).createRow(rowIndex); ? ? ? ? ? ? ? ? ? ? ? ? for (int columnIndex = 0; columnIndex < titleColumn.length; columnIndex++) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? String title = titleColumn[columnIndex].trim(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (!"".equals(title)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 獲取返回類型 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String UTitle = Character.toUpperCase(title.charAt(0)) + title.substring(1, title.length()); // 使其首字母大寫; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String methodName = "get" + UTitle; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Method method = clazz.getDeclaredMethod(methodName); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String returnType = method.getReturnType().getName(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Object object = method.invoke(obj); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String data = method.invoke(obj) == null ? "" : object.toString(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Cell cell = dataRow.createCell(columnIndex); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (data != null && !"".equals(data)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if ("int".equals(returnType)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(Integer.parseInt(data)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else if ("long".equals(returnType)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(Long.parseLong(data)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else if ("float".equals(returnType)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(floatDecimalFormat.format(Float.parseFloat(data))); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else if ("double".equals(returnType)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(doubleDecimalFormat.format(Double.parseDouble(data))); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else if (Date.class.getName().equals(returnType)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(dateFormat.format(object)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(data); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else { ? //字段為空 檢查該列是否是公式 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (colFormula != null) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String sixBuf = colFormula[columnIndex].replace("@", (rowIndex + 1) + ""); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell = dataRow.createCell(columnIndex); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellFormula(sixBuf); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }else{ ? ? ? ? ? ? ? ? ? ? //titleColumn為空 ?titleName為單列的情況 ? ? ? ? ? ? ? ? ? ? for (int index = 0; index < dataList.size(); index++) { ? ? ? ? ? ? ? ? ? ? ? ? //每個sheet3W條數(shù)據(jù) ? ? ? ? ? ? ? ? ? ? ? ? if (index != 0 && (index) % 30000 == 0) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? k = k + 1; ? ? ? ? ? ? ? ? ? ? ? ? ? ? sheet = workbook.createSheet(fileName + (k + 1)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? //寫入excel的表頭 ? ? ? ? ? ? ? ? ? ? ? ? ? ? titleNameRow = workbook.getSheet(fileName + (k + 1)).createRow(0); ? ? ? ? ? ? ? ? ? ? ? ? ? ? for (int i = 0; i < titleName.length; i++) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sheet.setColumnWidth(i, titleSize[i] * 256); ? ?//設(shè)置寬度 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Cell cell = titleNameRow.createCell(i); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellStyle(titleStyle); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(titleName[i]); ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? if (index < 30000) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? rowIndex = index + 1; ? ? ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? rowIndex = index - 30000 * ((index) / 30000) + 1; ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? Object obj = dataList.get(index); ? ? ? ? ? ? ? ? ? ? ? ? Row dataRow = workbook.getSheet(fileName + (k + 1)).createRow(rowIndex); ? ? ? ? ? ? ? ? ? ? ? ? Cell cell = dataRow.createCell(0); ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(obj.toString()); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? workbook.write(out); ? ? ? ? ? ? out.flush(); ? ? ? ? ? ? out.close(); ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? } ? ? ? /** ? ? ?* 寫excel. ? ? ?* xls方式 ? ? ?* @param titleColumn 對應(yīng)bean的屬性名 ? ? ?* @param titleName ? excel要導(dǎo)出的列名 ? ? ?* @param titleSize ? 列寬 ? ? ?* @param dataList ? ?數(shù)據(jù) ? ? ?*/ ? ? public void writeExcel(String titleColumn[], String titleName[], int titleSize[], List<?> dataList) { ? ? ? ? //添加Worksheet(不添加sheet時生成的xls文件打開時會報錯) ? ? ? ? Sheet sheet = workbook.createSheet(this.sheetName); ? ? ? ? //新建文件 ? ? ? ? OutputStream out = null; ? ? ? ? try { ? ? ? ? ? ? if (fileDir != null) { ? ? ? ? ? ? ? ? //有文件路徑 ? ? ? ? ? ? ? ? out = new FileOutputStream(fileDir); ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? //否則,直接寫到輸出流中 ? ? ? ? ? ? ? ? out = response.getOutputStream(); ? ? ? ? ? ? ? ? fileName = fileName + ".xls"; ? ? ? ? ? ? ? ? response.setContentType("application/msexcel;charset=UTF-8"); ? ? ? ? ? ? ? ? response.setHeader("Content-Disposition", "attachment; filename=" ? ? ? ? ? ? ? ? ? ? ? ? + URLEncoder.encode(fileName, "UTF-8")); ? ? ? ? ? ? } ? ? ? ? ? ? ? //寫入excel的表頭 ? ? ? ? ? ? Row titleNameRow = workbook.getSheet(sheetName).createRow(0); ? ? ? ? ? ? //設(shè)置樣式 ? ? ? ? ? ? CellStyle titleStyle = workbook.createCellStyle(); ? ? ? ? ? ? titleStyle = (HSSFCellStyle) setFontAndBorder(titleStyle, titleFontType, (short) titleFontSize); ? ? ? ? ? ? titleStyle = (HSSFCellStyle) setColor(titleStyle, titleBackColor, (short) 10); ? ? ? ? ? ? ? for (int i = 0; i < titleName.length; i++) { ? ? ? ? ? ? ? ? sheet.setColumnWidth(i, titleSize[i] * 256); ? ?//設(shè)置寬度 ? ? ? ? ? ? ? ? Cell cell = titleNameRow.createCell(i); ? ? ? ? ? ? ? ? cell.setCellStyle(titleStyle); ? ? ? ? ? ? ? ? cell.setCellValue(titleName[i].toString()); ? ? ? ? ? ? } ? ? ? ? ? ? ? //為表頭添加自動篩選 ? ? ? ? ? ? if (!"".equals(address)) { ? ? ? ? ? ? ? ? CellRangeAddress c = (CellRangeAddress) CellRangeAddress.valueOf(address); ? ? ? ? ? ? ? ? sheet.setAutoFilter(c); ? ? ? ? ? ? } ? ? ? ? ? ? ? //通過反射獲取數(shù)據(jù)并寫入到excel中 ? ? ? ? ? ? if (dataList != null && dataList.size() > 0) { ? ? ? ? ? ? ? ? //設(shè)置樣式 ? ? ? ? ? ? ? ? HSSFCellStyle dataStyle = workbook.createCellStyle(); ? ? ? ? ? ? ? ? titleStyle = (HSSFCellStyle) setFontAndBorder(titleStyle, contentFontType, (short) contentFontSize); ? ? ? ? ? ? ? ? ? if (titleColumn.length > 0) { ? ? ? ? ? ? ? ? ? ? for (int rowIndex = 1; rowIndex <= dataList.size(); rowIndex++) { ? ? ? ? ? ? ? ? ? ? ? ? Object obj = dataList.get(rowIndex - 1); ? ? //獲得該對象 ? ? ? ? ? ? ? ? ? ? ? ? Class clsss = obj.getClass(); ? ? //獲得該對對象的class實例 ? ? ? ? ? ? ? ? ? ? ? ? Row dataRow = workbook.getSheet(sheetName).createRow(rowIndex); ? ? ? ? ? ? ? ? ? ? ? ? for (int columnIndex = 0; columnIndex < titleColumn.length; columnIndex++) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? String title = titleColumn[columnIndex].toString().trim(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (!"".equals(title)) { ?//字段不為空 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //使首字母大寫 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String UTitle = Character.toUpperCase(title.charAt(0)) + title.substring(1, title.length()); // 使其首字母大寫; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String methodName = "get" + UTitle; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // 設(shè)置要執(zhí)行的方法 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Method method = clsss.getDeclaredMethod(methodName); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //獲取返回類型 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String returnType = method.getReturnType().getName(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Object object = method.invoke(obj); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String data = method.invoke(obj) == null ? "" : object.toString(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Cell cell = dataRow.createCell(columnIndex); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (data != null && !"".equals(data)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if ("int".equals(returnType)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(Integer.parseInt(data)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else if ("long".equals(returnType)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(Long.parseLong(data)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else if ("float".equals(returnType)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(floatDecimalFormat.format(Float.parseFloat(data))); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else if ("double".equals(returnType)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(doubleDecimalFormat.format(Double.parseDouble(data))); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else if (Date.class.getName().equals(returnType)) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(object)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellValue(data); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? } else { ? //字段為空 檢查該列是否是公式 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (colFormula != null) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String sixBuf = colFormula[columnIndex].replace("@", (rowIndex + 1) + ""); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Cell cell = dataRow.createCell(columnIndex); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cell.setCellFormula(sixBuf); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? workbook.write(out); ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } finally { ? ? ? ? ? ? if (out != null) { ? ? ? ? ? ? ? ? try { ? ? ? ? ? ? ? ? ? ? out.close(); ? ? ? ? ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? ? ? ? ? logger.debug("導(dǎo)出寫Excel異常"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? } ? }
四、調(diào)用
此處我沒寫成static靜態(tài)方法調(diào)用
@RequestMapping("/export") public void export(HttpServletResponse response) { ? ? ? ? List<Object> dataList = new ArrayList<>(); ? ? ? ? User user1 = new User("張三", 23, "男", 173); ? ? ? ? User user2 = new User("李四", 20, "男", 183); ? ? ? ? User user3 = new User("王五", 25, "男", 165); ? ? ? ? User user4 = new User("小花", 18, "女", 165); ? ? ? ? dataList.add(user1); ? ? ? ? dataList.add(user2); ? ? ? ? dataList.add(user3); ? ? ? ? dataList.add(user4); ? ? ? ? List<String> list = new ArrayList<>(); ? ? ? ? list.add("張三"); ? ? ? ? list.add("李四"); ? ? ? ? list.add("王五"); ? ? ? ? try { ? ? ? ? ? ? //導(dǎo)出xlsx格式 ? ? ? ? ? ? ExcelExportUtil excelExport1 = new ExcelExportUtil(); ? ? ? ? ? ? excelExport1.writeBigExcel(response, "用戶信息導(dǎo)出數(shù)據(jù)1", new String[]{"name", "age", "sex", "height"} ? ? ? ? ? ? ? ? ? ? , new String[]{"姓名", "年齡", "性別", "身高"} ? ? ? ? ? ? ? ? ? ? , new int[]{30, 30, 30, 30}, dataList); ? ? ? ? ? ? //導(dǎo)出xsl格式 // ? ? ? ? ? ?ExcelExportUtil excelExport2 = new ExcelExportUtil(response, "用戶信息導(dǎo)出數(shù)據(jù)2", "sheet1"); // ? ? ? ? ? ?excelExport2.writeExcel(new String[]{"name", "age", "sex", "height"} // ? ? ? ? ? ? ? ? ? ?, new String[]{"姓名", "年齡", "性別", "身高"} // ? ? ? ? ? ? ? ? ? ?, new int[]{30, 30, 30, 30}, dataList); ? ? ? ? ? ? ? //單列的導(dǎo)出 // ? ? ? ? ? ?ExcelExportUtil excelExport3 = new ExcelExportUtil(); // ? ? ? ? ? ?excelExport3.writeBigExcel(response, "測試", new String[]{} // ? ? ? ? ? ? ? ? ? ?, new String[]{"報警信息"} // ? ? ? ? ? ? ? ? ? ?, new int[]{30}, list); ? ? ? ? } catch (Exception e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java 數(shù)據(jù)結(jié)構(gòu)與算法系列精講之哈希算法實現(xiàn)
哈希表本質(zhì)是一種(key,value)結(jié)構(gòu),由此我們可以聯(lián)想到,能不能把哈希表的key映射成數(shù)組的索引index呢?如果這樣做的話那么查詢相當(dāng)于直接查詢索引,查詢時間復(fù)雜度為O(1),其實這也正是當(dāng)key為int型時的做法,將key通過某種做法映射成index,從而轉(zhuǎn)換成數(shù)組結(jié)構(gòu)2022-02-02mybatis實現(xiàn)獲取入?yún)⑹荓ist和Map的取值
這篇文章主要介紹了mybatis實現(xiàn)獲取入?yún)⑹荓ist和Map的取值問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06java獲取鍵盤輸入的數(shù)字,并進(jìn)行排序的方法
今天小編就為大家分享一篇java獲取鍵盤輸入的數(shù)字,并進(jìn)行排序的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07java計算自然數(shù)中的水仙花數(shù)的方法分享
這篇文章主要介紹了java計算自然數(shù)中的水仙花數(shù)的方法,需要的朋友可以參考下2014-03-03