java 导出excel封装了一个简单的导出方法。

来源:互联网 发布:h3c批量配置端口模式 编辑:程序博客网 时间:2024/06/05 03:31

创建ExportExcelUtil的通用方法:

import java.io.IOException;import java.io.OutputStream;import java.util.List;import java.util.Map;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFRichTextString;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hssf.util.HSSFColor;public class ExportExcelUtil {    /**     * 这是一个通用的方法,导出excel,暂未实现添加图片的导出     *     * @param title     *            表格标题名     * @param headers     *            表格属性列名数组     * @param dataset     *            需要显示的数据集合,类型为List<Map<String, Object>>     * @param keyList     *            Map集合中的对应的key     * @param out     *            可以将EXCEL文档导出到本地文件或者网络中     */    public static void exportExcel(String title, String[] headers,          List<Map<String, Object>> dataset, String[] keyList, OutputStream out) {       // 声明一个工作薄       HSSFWorkbook workbook = new HSSFWorkbook();       // 生成一个表格       HSSFSheet sheet = workbook.createSheet(title);       // 设置表格默认列宽度为15个字节       sheet.setDefaultColumnWidth((int) 16);       // 生成一个样式       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);       }       //循环放置表格中的值       for(int i = 0; i<dataset.size(); i++){           row = sheet.createRow(i + 1);           //产生编号,1,2,3,4,5...的递增编号,不需要,header去掉编号,这里注释掉就可以            row.createCell(0).setCellValue(i+1+"");           Map<String, Object> obj=dataset.get(i);            for(int j = 0; j < keyList.length; j ++){               if(obj.get(keyList[j]) != null){ row.createCell(j+1).setCellValue(obj.get(keyList[j])+""); }           }       }       try {          workbook.write(out);       } catch (IOException e) {          e.printStackTrace();       }    }}

在这里调用:

/**     * 导出司机信息     * @Title: exportDriver      * @return      * @author ZhangFZ     * @since 2017年7月15日 V 1.0     */    @RequestMapping(value = "/exportDriver", method = RequestMethod.GET)    public String exportDriver(            HttpSession session,HttpServletRequest request,HttpServletResponse response){        String[] cellname={"编号","姓名","人员类别","联系电话","从业资格证号","资格证有效期","身份证号","身份证有效期"};        String[] keyList={"driver","driver_type","phone","certificate_no","certificate_time","personid","personid_time"};        List<Map<String, Object>> list = driverService.getDriverById();        try {            String exportFileName = "司机信息";            response.setContentType("application/vnd.ms-excel");             //根据浏览器类型处理文件名称            String agent = request.getHeader("USER-AGENT").toLowerCase();            if (agent.indexOf("firefox") > -1){//若是火狐                exportFileName = new String(exportFileName.getBytes("UTF-8"), "ISO8859-1");            } else {//其他浏览器                exportFileName = java.net.URLEncoder.encode(exportFileName, "UTF-8");            }            OutputStream out = response.getOutputStream();            //保存导出的excel的名称            response.setHeader("Content-Disposition", "attachment;filename="                    + exportFileName + ".xls");            if (list != null) {                 ExportExcelUtil.exportExcel("司机信息", cellname, list, keyList, out);            }            out.flush();            out.close();        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }          return null;    }

jsp页面请求方法:

//导出司机    function exportDriver(){        lui.confirm("确定要导出选中的人员数据吗?",function(){            location.href="${base}/admin/oil/driver/exportDriver.do";        },function(){            return;        });    }

输入图片说明

原创粉丝点击