基本上每个系统或多或少都有一些导出功能,我之前做的系统是针对每个功能定制一个导出,而且我看网上的也大多是这么做的,这样就存在一个代码冗余的问题,而且增加工作量,今天整理了一下,系统中所有的导出都可以引用(注意我这里说的是excel,word的暂时还没整理),并且支持导出图片,上代码。
1. jar包准备
如果你是新手请参考 https://blog.csdn.net/fulishafulisha/article/details/80152805 ,如果使用的maven
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.10-FINAL</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.10-FINAL</version> </dependency>
2. 工具类
import lombok.extern.slf4j.Slf4j; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.regex.Matcher; /** * Excel工具类 * * @author yunfei */ @Slf4j public class ExeclUtil { //正则判断是否是数字 private static Pattern p = Pattern.compile("^//d+(//.//d+)?$"); /** * @param title * @param headers * @param dataset */ public static InputStream exportExcel(String title, String[] headers, List<Object[]> dataset) { InputStream is; ByteArrayOutputStream os = new ByteArrayOutputStream(); // 声明一个工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); // 生成一个表格 HSSFSheet sheet = workbook.createSheet(title); // 设置表格默认列宽度为15个字节 sheet.setDefaultColumnWidth(15); // 生成一个样式 HSSFCellStyle style = workbook.createCellStyle(); // 设置这些样式 style.setFillForegroundColor(HSSFColor.SKY_BLUE.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_CENTER); // 生成一个字体 HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.VIOLET.index); font.setFontHeightInPoints((short) 12); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 把字体应用到当前的样式 style.setFont(font); // 生成并设置另一个样式 HSSFCellStyle style2 = workbook.createCellStyle(); style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); style2.setBorderLeft(HSSFCellStyle.BORDER_THIN); style2.setBorderRight(HSSFCellStyle.BORDER_THIN); style2.setBorderTop(HSSFCellStyle.BORDER_THIN); style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成另一个字体 HSSFFont font2 = workbook.createFont(); font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 把字体应用到当前的样式 style2.setFont(font2); // 声明一个画图的顶级管理器 HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); // 定义注释的大小和位置,详见文档 HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5)); // 设置注释内容 comment.setString(new HSSFRichTextString("可以在POI中添加注释!")); // 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容. comment.setAuthor("leno"); // 产生表格标题行 HSSFRow row = sheet.createRow(0); for (int i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(style); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } // 遍历集合数据,产生数据行 int index = 0; for (Object[] o : dataset) { index++; row = sheet.createRow(index); for (int i = 0; i < o.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(style2); try { Object value = o[i]; if (value == null) { value = ""; } // 判断值的类型后进行强制类型转换 String textValue = null; if (value instanceof Boolean) { boolean bValue = (Boolean) value; textValue = "男"; if (!bValue) { textValue = "女"; } } else if (value instanceof Date) { Date date = (Date) value; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); textValue = sdf.format(date); } else if (value instanceof byte[]) { // 有图片时,设置行高为60px; row.setHeightInPoints(60); // 设置图片所在列宽度为80px,注意这里单位的一个换算 sheet.setColumnWidth(i, (int) (35.7 * 80)); // sheet.autoSizeColumn(i); byte[] bsValue = (byte[]) value; HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) 7, index, (short) 7, index); anchor.setAnchorType(2); patriarch.createPicture(anchor, workbook.addPicture(bsValue, HSSFWorkbook.PICTURE_TYPE_JPEG)); } else { // 其它数据类型都当作字符串简单处理 textValue = value.toString(); } // 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成 if (textValue != null) { Matcher matcher = p.matcher(textValue); if (matcher.matches()) { // 是数字当作double处理 cell.setCellValue(Double.parseDouble(textValue)); } else { HSSFRichTextString richString = new HSSFRichTextString( textValue); HSSFFont font3 = workbook.createFont(); font3.setColor(HSSFColor.BLUE.index); richString.applyFont(font3); cell.setCellValue(richString); } } } catch (Exception e) { log.error(e.getMessage(), e); e.printStackTrace(); } finally { // 清理资源 } } } try { workbook.write(os); } catch (IOException e) { log.error(e.getMessage(), e); e.printStackTrace(); } is = new ByteArrayInputStream(os.toByteArray()); return is; } /** * 导出excel * * @param response * @param headArray 表头 * @param contentList 内容 * @param fileName 文件名 * @throws Exception */ public static void exportExcel(HttpServletResponse response, String[] headArray, List contentList, String fileName) throws Exception { // 读到流中 InputStream inStream = exportExcel(fileName, headArray, contentList); // 设置输出的格式 // response.reset(); response.setContentType("application/octet-stream"); response.addHeader("Access-Control-Expose-Headers","Content-Disposition"); response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); // response.addHeader("Access-Control-Allow-Credentials","true"); // 循环取出流中的数据 byte[] b = new byte[100]; int len; try { while ((len = inStream.read(b)) > 0) { response.getOutputStream().write(b, 0, len); } inStream.close(); } catch (IOException e) { log.error(e.getMessage(), e); e.printStackTrace(); } } }
3. 测试使用
public void exportTransaction(String userId, String orderNo, String dateFrom, String dateEnd, String orderStatus, String payment, String userOrAgentId, HttpServletResponse response) throws Exception { ResultData<List<OrderViewVO>> resultData = umrahApi.getOrderList(userId, orderNo, dateFrom, dateEnd, orderStatus, payment, userOrAgentId); List<OrderViewVO> orderViewVOList = resultData.getData(); log.info("Order数据共:{} 条",orderViewVOList.size()); String[] headers = {"Order Number","Book Date","User ID","Order Status","Departure Date"}; List<Object[]> objects = new ArrayList<>(); for (OrderViewVO entity : orderViewVOList) { Object[] o = {entity.getOrderNumber,...省略 }; objects.add(o); } ExeclUtil.exportExcel(response, headers, objects, "transactions.xls"); }
原文地址:https://www.cnblogs.com/wangjianguang/p/10075291.html
时间: 2024-10-31 00:03:02