poi导出excel
/** * 文件下载 * * @param response * @param list 查询数据集合 * @param filename 文件名前缀 * @param title 属性名称 * @param strTitleE 属性 * @throws Exception */ public static void export(HttpServletResponse response, List list, String filename, List<String> title, String[] strTitleE) throws Exception { int[] widths = {15, 15, 15, 15}; XSSFWorkbook wb = new XSSFWorkbook(); // 设置列宽 XSSFSheet sheet = setSheet(wb, filename, widths); //XSSFSheet sheet = wb.createSheet(filename); // 设置Excel表格头数据 setTitleRowData(wb, sheet, title); setRowData(wb, sheet, list, strTitleE); // 导出Excel(单工作表) exportToExcel(response, wb, filename); }
/** * 设置列宽 <br /> * * @param workbook * @param sheetTitle * @param widths * @return */ public XSSFSheet setSheet(XSSFWorkbook workbook, String sheetTitle, int[] widths) { XSSFSheet sheet = workbook.createSheet(sheetTitle);// 建立新的sheet对象 for (int i = 0; i < widths.length; i++) { sheet.setColumnWidth(i, (int) 256 * widths[i]); } return sheet; }
/** * 设置Excel表格头数据 <br /> * * @param workbook * @param sheet * @param titles * @throws Exception */ public void setTitleRowData(XSSFWorkbook workbook, XSSFSheet sheet, List<String> titles) { XSSFRow rowTitle = sheet.createRow(0);// 建立新行 XSSFCellStyle cellStyle = setCellStyle(workbook); for (int i = 0; i < titles.size(); i++) { XSSFCell cell = rowTitle.createCell(i); // cell.setEncoding(HSSFCell.ENCODING_UTF_16); //设置cell编码解决中文高位字节截断 cell.setCellValue(titles.get(i)); cell.setCellStyle(cellStyle); } }
/** * 为Excel添加行数据 <br /> * * @param workbook * @param sheet * @param lists * @throws Exception */ @SuppressWarnings({"rawtypes", "unchecked"}) public void setRowData(XSSFWorkbook workbook, XSSFSheet sheet, List lists, String[] valueField) throws Exception { if (valueField == null || valueField.length < 1) { setRowData(workbook, sheet, lists); } for (int i = 1; i <= lists.size(); i++) { // HSSFRow row = sheet.createRow((short)i);//建立新行 XSSFRow row = sheet.createRow(i); // 不用short类型,支持大数据量导出 Object obj = lists.get(i - 1); Class clazz = obj.getClass(); int index = 0; for (int j = 0; j < valueField.length; j++) { Method method = clazz.getMethod("get" + valueField[j].substring(0, 1).toUpperCase() + valueField[j].substring(1)); Object objValue = method.invoke(lists.get(i - 1)); XSSFCell cell = row.createCell(index); if (objValue != null) { cell.setCellValue(objValue.toString()); } index++; } } }
/** * 导出Excel(单工作表) * * @param response * @param title * @throws Exception */ public static void exportToExcel(HttpServletResponse response, XSSFWorkbook workbook, String title) throws Exception { try { String time = (new SimpleDateFormat("yyyyMMddHHmm")).format(new Date()); String sFileName = new String((title + "_" + time + ".xls").getBytes("gbk"), "ISO-8859-1"); response.setHeader("Content-Disposition", "attachment;filename=".concat(sFileName)); response.setHeader("Connection", "close"); response.setHeader("Content-Type", "application/vnd.ms-excel"); workbook.write(response.getOutputStream()); } catch (Exception e) { e.printStackTrace(); } finally { workbook.close(); } }
//调用导出的方法 @RequestMapping("/exportList") @ResponseBody public void downDsMapPub(HttpServletResponse response){ //list 表示查询的集合 //titleList 表示导出的字段的中文名 //strTitle 表示导出的字段,必须与实体类上的名字相对应
//fileName 表示导出的文件名 ExcelUtils.export(response,list,fileName,titleList,strTitle); }