赞
踩
之前写了一篇使用poi
进行docx
模板导出的文章,最近呢也使用POI
实现excel文件的导出与下载,特此记录。
Springboot —— 根据docx填充生成word文件,并导出pdf
再新建的springboot工程项目中,引入下列的依赖即可
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.5.0</version>
</dependency>
编写工具类,主要用于表头的创建、单元格样式的定义、以及数据格式的转换等。
import lombok.extern.slf4j.Slf4j; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFRichTextString; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.io.FileOutputStream; import java.net.URLEncoder; import java.util.List; import java.util.Optional; @Slf4j public final class ExcelExport { /** * description: 依据excel模板,导出数据excel * @param sheetName sheet 页名称 * @param headers 数据header部分 * @param dataList table 数据集合 * @param destFile excel模板文件路径 */ public static void export(String sheetName, String[] headers, List<List<Object>> dataList, File destFile) throws Exception { SXSSFWorkbook workbook = new SXSSFWorkbook(); createSheet(sheetName, headers, dataList, workbook); workbook.write(new FileOutputStream(destFile)); } /** * 根据header与data的集合,动态地创建并生成excel数据流进行下载操作 * description: 导出excel --- 支持web * @param sheetName sheet表名字 * @param headers 表头 * @param dataList 表数据 * @param fileName 导出文件名 * @param response */ public static void export(String sheetName , String[] headers , List<List<Object>> dataList ,String fileName , HttpServletResponse response) throws Exception { SXSSFWorkbook workbook = new SXSSFWorkbook(); createSheet(sheetName, headers, dataList, workbook); response.reset(); //response.setContentType("application/vnd.ms-excel; charset=utf-8"); response.setContentType("application/octet-stream; charset=utf-8"); response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8")); workbook.write(response.getOutputStream()); // 删除临时文件 workbook.dispose(); } /** * description: 创建sheet表格 * @param sheetName 表sheet 名字 * @param headers 表头 * @param dataList 表数据 * @param wb */ public static void createSheet(String sheetName , String[] headers , List<List<Object>> dataList , SXSSFWorkbook wb) { SXSSFSheet sheet = wb.createSheet(sheetName); // 设置表头和单元格格式 CellStyle headStyle = setHeaderStyle(wb); CellStyle bodyStyle = setBodyStyle(wb); // 创建表头和单元格数据 createHeader(headers, sheet, headStyle); createBody(dataList, sheet, bodyStyle,wb); } /** * description: 创建表头 * @param headers * @param sheet * @param headStyle */ private static void createHeader(String[] headers, SXSSFSheet sheet, CellStyle headStyle) { SXSSFRow row = sheet.createRow(0); row.setHeightInPoints(16F); for (int i = 0; i < headers.length; i++) { // 创建单元格 SXSSFCell cell = row.createCell(i); cell.setCellStyle(headStyle); XSSFRichTextString text = new XSSFRichTextString(headers[i]); cell.setCellValue(text); sheet.trackAllColumnsForAutoSizing(); sheet.autoSizeColumn(i); } } /** * description: 表格中填充数据 * @param dataList * @param sheet * @param bodyStyle * @param wb 主要是更改文本格式 */ private static void createBody(List<List<Object>> dataList, SXSSFSheet sheet, CellStyle bodyStyle,SXSSFWorkbook wb) { if (dataList == null) { return; } for (int i = 0; i < dataList.size(); i++) { // 从第二行开始,第一行做表头 SXSSFRow row = sheet.createRow(i+1); List<Object> rowList = dataList.get(i); if (rowList == null) { continue; } for (int j = 0; j < rowList.size(); j++) { SXSSFCell cell = row.createCell(j); Object data = rowList.get(j); // 如果数据是 Double 类型,则需要保证金额数转换,否则导出数据显示为科学计数法 如: 8E7 if(data instanceof Double){ DataFormat format= wb.createDataFormat(); bodyStyle.setDataFormat(format.getFormat("#,##0.00")); // 千位符 // bodyStyle.setDataFormat(format.getFormat("#0.00")); // 小数 // bodyStyle.setDataFormat(format.getFormat("0.00%")); // 百分比 数据必须是小数,如:0.59 -> 59% cell.setCellStyle(bodyStyle); cell.setCellValue(Double.parseDouble(String.valueOf(data))); }else{ String textStr = Optional.ofNullable(rowList.get(j)).orElse("").toString(); XSSFRichTextString text = new XSSFRichTextString(textStr); cell.setCellValue(text); } sheet.trackAllColumnsForAutoSizing(); //设置内容列为列的最大值 sheet.autoSizeColumn(j); } } } /** * description: 设置单元格内容样式 * @param wb * @return HSSFCellStyle */ private static CellStyle setBodyStyle(SXSSFWorkbook wb) { // 设置表格单元格格式 CellStyle style = wb.createCellStyle(); style.setFillForegroundColor(HSSFColor.WHITE.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 设置字体格式 Font font = wb.createFont(); font.setFontName("宋体"); font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); style.setFont(font); return style; } /** * description: 设置表头样式 * @param wb * @return * @return HSSFCellStyle */ private static CellStyle setHeaderStyle(SXSSFWorkbook wb) { // 设置表格单元格格式 CellStyle style = wb.createCellStyle(); style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); style.setBorderRight(BorderStyle.THIN); style.setRightBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setBorderLeft(BorderStyle.THIN); style.setLeftBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setBorderTop(BorderStyle.THIN); style.setTopBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); style.setBorderBottom(BorderStyle.THIN); style.setBottomBorderColor(IndexedColors.GREY_50_PERCENT.getIndex()); // style.setFillBackgroundColor(IndexedColors.LIGHT_YELLOW.getIndex()); style.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); // 设置字体格式 Font font = wb.createFont(); font.setFontName("宋体"); font.setFontHeightInPoints((short) 10); style.setFont(font); return style; } }
@GetMapping("/test-xls")
public void contractIntrQryPayxls(HttpServletResponse response) throws Exception {
String[] headers = new String[]{"编号","名称","金额"};
List<List<Object>> dataLists = new ArrayList<>();
for (int i = 0; i < 10; i++) {
List<Object> data = new ArrayList<>();
data.add("xj_"+i);
data.add("xiangjiao "+i);
data.add(10000.00*i);
dataLists.add(data);
}
ExcelExport.export("sheetName",headers,dataLists,"xiangjiao测试.xls",response);
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。