• 简单完成Excel导出


    Excel的导出只需要三步即可完成:

    1,完成excel的工具类编写,直接复制即可

    package cn.com.bcl.util;
    
    import java.util.List;
    
    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFCellStyle;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    
    
    public class ExcelUtil {
    
        /**
         * 导出Excel
         * @param sheetName sheet名称
         * @param title 标题
         * @param list 内容
         * @param wb HSSFWorkbook对象
         * @return
         */
        public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,List<String[]>list, HSSFWorkbook wb){
    
            // 第一步,创建一个HSSFWorkbook,对应一个Excel文件
            if(wb == null){
                wb = new HSSFWorkbook();
            }
    
            // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
            HSSFSheet sheet = wb.createSheet(sheetName);
    
            // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
            HSSFRow row = sheet.createRow(0);
    
            // 第四步,创建单元格,并设置值表头 设置表头居中
            HSSFCellStyle style = wb.createCellStyle();
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
    
            //声明列对象
            HSSFCell cell = null;
    
            //创建标题
            for(int i=0;i<title.length;i++){
                cell = row.createCell(i);
                cell.setCellValue(title[i]);
                cell.setCellStyle(style);
            }
    
            //创建内容
            for(int i=0;i<list.size();i++){
                row = sheet.createRow(i + 1);
                for(int j=0;j<list.get(i).length;j++){
                    //将内容按顺序赋给对应的列对象
                    row.createCell(j).setCellValue(list.get(i)[j]);
                }
            }
            return wb;
        }
    }

    2,业务逻辑代码

    @Controller
    @RequestMapping(value = "/channellog")
    public class ChannelLogController extends BaseController {
    
      @Resource(name = "channelLogService")
      private ChannelLogService channelLogService;
    
        /**
         * 导出excel
         * 
         * @param page
         * @throws Exception
         */
        @RequestMapping(value = "/exportExcel")
        public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
            logBefore(logger, Jurisdiction.getUsername() + "列表channelLog");
          
            List<Map<String, Object>> varList = channelLogService.dataList(); // 列出channel_log列表
    
            String fileName = "操作日志" + System.currentTimeMillis() + ".xls";
            String sheetName = "操作日志";
            String[] title = new String[] { "序号", "影厅", "操作内容", "通道号", "开始时间", "使用时长(分钟)" };
         //内容封装
            List<String[]> contents = new ArrayList<>();
            for (int i = 0; i < varList.size(); i++) {
                String[] str = new String[title.length];
                Map<String, Object> map = varList.get(i);
                str[0] = String.valueOf(i+1);
                str[1] = String.valueOf(map.get("hallName"));
                str[2] = String.valueOf(map.get("oprationName"));
                str[3] = String.valueOf(map.get("channelNo"));
                str[4] = String.valueOf(map.get("startTime"));
                str[5] = String.valueOf(map.get("useTime") == null ? "" : map.get("useTime"));
                contents.add(str);
            }
            // 创建HSSFWorkbook
            HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, contents, null);
            try {
                this.setResponseHeader(response, fileName);
                OutputStream os = response.getOutputStream();
                wb.write(os);
                os.flush();
                os.close();
                wb.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        // 发送响应流方法
        private void setResponseHeader(HttpServletResponse response, String fileName) {
            try {
                try {
                    fileName = new String(fileName.getBytes(), "ISO8859-1");
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                response.setContentType("application/octet-stream");
                response.setHeader("Content-Disposition", "attachment;filename="" + fileName+""");
                response.addHeader("Pargam", "no-cache");
                response.addHeader("Cache-Control", "no-cache");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

      在这一步也只需要完成:a>查询出结果集;b>对结果集进行封装为指定的格式即可

    3,js页面需要注意的点

    $("#exportExcel").click(function(){
        $("#Form").attr("action", "channellog/exportExcel.do");
        $("#Form").submit();
    })

     导出结果展示:

  • 相关阅读:
    podfile 文件写法
    九大内置对象
    动态网页开发基础
    表单效验
    使用jQuery快速高效制作网页交互特效
    使用jQuery操作DOM
    jQuery选择器
    初始jQuery
    javaScript基础及初始面向对象
    JavaScript操作DOM对象
  • 原文地址:https://www.cnblogs.com/bbgs-xc/p/10531160.html
Copyright © 2020-2023  润新知