web - maven - java导出Excel

使用java进行导出Excel功能

内部功能实现方法
导出Excel操作
1、在 pom文件中导入 POI jar包

	<dependencies>
		<!-- excel 批量的导入导出  -->
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
    <dependencies>

2、Jsp中的导出方法

  function doexcel() {
             var filename = window.prompt("请输入Excel文件名称","文档");
             window.location.href="<%=basePath %>/cus/doExcel?excelName="+filename;
   }

3、后台接收参数并实现功能



//Excel的导出操作  需要工具包的支持 –   前提:在查询的时候将数据放在了session中
@RequestMapping("doExcel")
public String doExcel(HttpSession session,String excelName) {
       /*
        获取数据 – 进行了强转  -- 
		值存放到session中之后被转化为Object? 
		经过源码的查看 可以确定就是Object 
       */
    List<Customer> list = (List<Customer>)session.getAttribute("customerInfo");
    //标题
    String[] title = {"id","comname","companyperson","comaddress","comphone","camera","present","remark","addtime"};
    //Excel    文件名称
    String fileName = System.currentTimeMillis() + excelName+".xls";
    //sheet名  表格名称 
    String sheetName = "客户信息表";
//设置二维数组  -- list.size() 数据的条数  -- title.length  由标题确定数据的列数
    String[][] content = new String[list.size()][title.length];
//开始进行添加数据
    for (int i = 0; i <list.size(); i++) {

        Customer customer = list.get(i);
        content[i][0] = customer.getId().toString();
        content[i][1] = customer.getComname();
        content[i][2] = customer.getCompanyperson();
        content[i][3] = customer.getComaddress();
        content[i][4] = customer.getComphone();
        content[i][5] = customer.getCamera();
        content[i][6] = customer.getPresent();
        content[i][7] = customer.getRemark();
        content[i][8] = customer.getAddtime().toString();
    }
    //创建HSSWorkbook
    HSSFWorkbook workbook = ExcelUtil.getHSSWorkbook(sheetName,title,content,null);
    //响应客户端
    try {
        //创建存放Excel表格的位置
        File file = new File("F:\\Excel\\" + fileName);
        FileOutputStream fos = new FileOutputStream(file);
        workbook.write(fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    //将session清空
    session.removeAttribute("customerInfo");
    return "customer";
}


通常业务需求都是客户端一个导出按钮,发送请求到服务端,服务端写一个接口导出报表到客户端,客户可以自行下载。无论Struts或者springMVC均可。 @RequestMapping("Export") @ResponseBody public String getAll(HttpServletRequest request,HttpServletResponse response) throws IOException{ //集合为需要导出数据,数据查询得到,这里测试就不写了。 List<User> list=new ArrayList<User>(); // 生成Excel文件 HSSFWorkbook hssfWorkbook = new HSSFWorkbook(); HSSFSheet sheet = hssfWorkbook.createSheet("测试数据"); // 表头 HSSFRow headRow = sheet.createRow(0); headRow.createCell(0).setCellValue("姓名"); headRow.createCell(1).setCellValue("手机号码"); headRow.createCell(2).setCellValue("年龄"); // 表格数据 for (User user : list) { HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1); dataRow.createCell(0).setCellValue(user.getName()); dataRow.createCell(1).setCellValue(user.getPhone()); dataRow.createCell(2).setCellValue(user.getAge()); } // 下载导出(一个流两个头) // 设置头信息 response.setContentType( "application/vnd.ms-excel"); // MIME .jpg .xls .mp3 .avi .txt .exe String filename = "驾驶员数据.xls"; //如果为Struts框架,获得request和response可用ServletActionContext String agent = request .getHeader("user-agent"); filename = FileUtils.encodeDownloadFilename(filename, agent); response.setHeader("Content-Disposition", "attachment;filename=" + filename); ServletOutputStream outputStream = response .getOutputStream(); //输出 hssfWorkbook.write(outputStream); // 关闭 hssfWorkbook.close(); //System.out.println("导出成功"); return "success"; }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值