欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java excel导出至本地 前端+后台完整版

程序员文章站 2024-03-20 21:13:58
...

java excel导出至本地 前端+后台

**前端**
**jsp:**  样式可自行找
**js:**
   <script type="text/javascript">
  			  $(function () {
  			      $("#daochule").click(function () { //绑定导出按钮
					var content=encodeURIComponent($("#content").val());//中文参数可进行编码
						console.log(content);
     		       window.location.href = "/visionlite/operatlog/getLogListDown?uid=" //拼接参数,中文可先行编码
          			      + $("#uid").val() + "&content=" + content + "&startTime=" + $("#startTime").val() + "&endTime=" +  $("#endTime").val();
        })
    })
	</script>
  

后台:

   @RequestMapping
     @ResponseBody
    public void getLogListDown(HttpServletRequest request, String uid, String content, Date startTime, Date endTime, HttpServletResponse response) {
      
      //数据库查询数据
        HashMap<String, Object> map = logService.getLogListDown(pmap, uid, content, startTime, endTime, adminInfo.getInstid(), adminInfo.getManagerType(), adminInfo.getSkill());
        List<LogInfo> adminList = (List<LogInfo>) map.get("list");
        ArrayList<HashMap<String, String>> mapList = new ArrayList<>();
        //循环每行数据
        for (int i = 0; i < adminList.size(); i++) {
            LogInfo logInfo = adminList.get(i);
            HashMap<String, String> temp = new HashMap<>();
            temp.put("0", logInfo.getUid());
            temp.put("1", logInfo.getContent());
            temp.put("2", DateUtil.toString(logInfo.getCreatetime()));
            mapList.add(temp);
        }
      //excel 数据标题
        List<String> string = new ArrayList<String>();
        string.add("操作人工号");
        string.add("操作内容");
        string.add("操作日期");
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String time = sdf.format(date);
       // String localAdress = GlobalVariable.realRootPath + "/WEB-INF/down" + "/" + time + ".xls";
       //可抽取为公共方法
        ExcelExportUtil.createExcel(mapList, string, response,  "操作日志");
        //可缓存至本地
//        File saveDir = null;
//        try {
//            saveDir = new File(localAdress);
//            saveDir.delete();
//        } catch (Exception e) {
//            Log.error(localAdress + " 文件删除失败:" + e.getMessage());
//        }
   }

ExcelExportUtil 公共类


 /**
			     * @功能:导出excel list 参数集合
			     * string 标题头
			     * response
			     * fileName 文件名
			  	   */
			  	  public static void createExcel(ArrayList<HashMap<String, String>> list, List<String> string, 	HttpServletResponse response, String fileName) {
			        // 创建excel
			        HSSFWorkbook wk = new HSSFWorkbook();
			        // 创建一张工作表
			        HSSFSheet sheet = wk.createSheet();
			        // 2
			        sheet.setColumnWidth(0, 5000);
			        HSSFRow row = sheet.createRow(0);
			        // 创建第一行的第一个单元格
			        // 想单元格写值
			        HSSFCell cell;
			        Log.debug("--------下载excel 传入的标题头部:" + string.toString());
			        for (int i = 0; i < string.size(); i++) {
			            cell = row.createCell((short) i);
			            cell.setCellValue(string.get(i));
			        }
			        // 创建第一行
			        for (short i = 0; i < list.size(); i++) {
			            HashMap<String, String> map = list.get(i);
			            row = sheet.createRow(i + 1);
			            for (int j = 0; j < string.size(); j++) {
			                row.createCell(j).setCellValue(map.get(String.valueOf(j)));
			            }
			        }
			        try {
			            /**
			             * 弹出下载选择路径框
			             */
			            response.setContentType("application/octet-stream;charset=UTF-8");
			            Log.debug("--------下载excel 传入的文件名称:" + fileName + ".xls");
			            response.setHeader("Content-disposition", "attachment;filename=" + 	                      java.net.URLEncoder.encode(fileName, "UTF-8") + ".xls");//默认Excel名称
			            response.flushBuffer();
			            wk.write(response.getOutputStream());
			
	            //GlobalVariable.realRootPath + "/WEB-INF/down"+"/"+time+".xls"
	//            Log.debug("--------下载excel 传入的临时文件存储地址:" + localAdress);
	//            wk.write(new FileOutputStream(new File(localAdress)));
	            wk.close();
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	        }
	    }
	  

相关标签: java