上一篇我们说到Java使用POI操作Excel,对Excel文件进行解析,并将数据导入到数据库中,今天这篇来分享一个Java封装的工具类,将数据库查询到的数据进行Excel形式的导出。
具体的说明都在工具类的注释中,调用该工具类的方法需要传几个参数,一个是导出文件的标题,一个是数据库查出的数据集合,还有一个输出流对象,然后调用该方法就能导出Excel文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| public static void exportExcptDetails(String sheetTitle, List<Map<String, String>> columnList, List<DataModel> valueList, OutputStream out) { XSSFWorkbook workBook = new XSSFWorkbook(); XSSFSheet sheet = workBook.createSheet(sheetTitle);
XSSFCellStyle headerStyle = workBook.createCellStyle(); headerStyle.setFillForegroundColor(IndexedColors.BLUE.getIndex()); headerStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND); headerStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); headerStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN); headerStyle.setBorderRight(XSSFCellStyle.BORDER_THIN); headerStyle.setBorderTop(XSSFCellStyle.BORDER_THIN); headerStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
XSSFFont font = workBook.createFont(); font.setColor(HSSFColor.WHITE.index); font.setFontHeightInPoints((short) 11); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); headerStyle.setWrapText(true); headerStyle.setFont(font);
XSSFCellStyle bodystyle = workBook.createCellStyle(); bodystyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); bodystyle.setBorderLeft(XSSFCellStyle.BORDER_THIN); bodystyle.setBorderRight(XSSFCellStyle.BORDER_THIN); bodystyle.setBorderTop(XSSFCellStyle.BORDER_THIN); bodystyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
sheet.setDefaultColumnWidth(20); XSSFDrawing patriarch = sheet.createDrawingPatriarch(); XSSFRow row = sheet.createRow(0); XSSFCell cell = null; Map<String, String> maphead = null;
for (int i = 0; i < columnList.size(); i++) { maphead = columnList.get(i); cell = row.createCell(Integer.parseInt(maphead.get("ORDERID") .toString())); cell.setCellValue(maphead.get("NAME_ZH") == null ? "" : maphead .get("NAME_ZH").toString());
sheet.setColumnWidth( i, (maphead.get("NAME_ZH") == null ? "" : maphead.get( "NAME_ZH").toString()).getBytes().length * 2 * 256); cell.setCellStyle(headerStyle); }
XSSFRow rows = null; String value = ""; String mapValue = ""; DataModel map = null; Map<String, String> mapBody = null;
if (valueList != null) { for (int i = 1; i <= valueList.size(); i++) { map = (DataModel) valueList.get(i - 1); rows = sheet.createRow(i); for (int j = 0; j < columnList.size(); j++) { mapBody = columnList.get(j); cell = rows.createCell(Integer.parseInt(mapBody.get("ORDERID") .toString())); value = mapBody.get("NAME_EN") == null ? "" : mapBody.get( "NAME_EN").toString(); mapValue = map.get(value) == null ? "" : map.get(value) .toString();
cell.setCellValue(mapValue); cell.setCellStyle(bodystyle); } } } try { workBook.write(out); out.flush(); out.close(); } catch (Exception e) { log.error(e.getMessage(), e); e.printStackTrace(); } }
|
类似的还有导出PDF的工具类,以后有用到的场景,再进行分享。