springboot_poi思路

开发工具:eclipse

jar包管理:maven

框架使用:springboot+poi+thymeleaf

1.首先导入pom.xml

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.16</version>
</dependency>

2.前端查询数据库

展示要导出的列表

3.点击导出通过js提交页面对应的标签值

例如:function export(){

window.location.href = resqURL;

}

注意!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

页面不能通过ajax访问url。 
必须通过页面window.location.href = resqURL;属性来访问

resqURL带参

//前端
<td id="sadd" name="oo">sadd</td>
//js
var oo=$("#sadd").text();
window.location.href = "ha?sadd="+oo;
//后端
    @RequestMapping("ha")
    public String ha(Model m,@RequestParam String sadd) {
        String oosda=sadd;
        System.out.println(oosda);
        m.addAttribute("name", "hah");
        return "hello";
    }

 

 

我在这个地方吃了好大的亏,,,唉

4.后台到controller层接受参数定制表头初始化数据

@RequestMapping("export")
    public ResponseEntity<byte[]> userExcel() throws Exception {
        /*所有表头*/
        Map<String,Map<String,String>> titleMaps=new LinkedHashMap<String,Map<String,String>>();
        /*所有数据*/
        Map<String,List<Map>> results=new HashMap<String,List<Map>>();
        /*每个表格的列标题*/
        Map<String,String> title=new LinkedHashMap<String, String>();
        title.put("uid", "Id");
        title.put("username", "用户名");
        title.put("password", "密码");
        //行初始化为空值
        List<Map> nullRows=new ArrayList<Map>();
        Map nullMap=new HashMap();
        nullMap.put("uid", "");
        nullMap.put("username", "");
        nullMap.put("password", "");
        nullRows.add(nullMap);
        //文件名
        String filename="用户信息";
        titleMaps.put(filename, title);
        //从数据库查询数据
        List<Map> collentList=userService.userExcel();        
        results.put(filename, collentList);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
           ExcelUtilNew.exportExcelDocument(titleMaps, results,out);
        System.out.println("我来了");
         HttpHeaders headers = new HttpHeaders();
         headers.add("Content-Disposition", "attchement;filename=" + URLEncoder.encode(filename, "utf-8")+".xlsx");
        return new ResponseEntity<byte[]>(out.toByteArray(), headers, HttpStatus.CREATED);
        
    }

3.调用poi工具类导出excel

/**
     * 导出Excel文档
     * 
     * @throws FileNotFoundException
     */
    public static void exportExcelDocument(Map<String, String> titleMap, List<Map> result, OutputStream out)
            throws Exception {
        Map<String, Map<String, String>> titleMaps = new HashMap<String, Map<String, String>>();
        titleMaps.put("Sheet1", titleMap);
        Map<String, List<Map>> results = new HashMap<String, List<Map>>();
        results.put("Sheet1", result);
        exportExcelDocument(titleMaps, results, out);
    }

    public static void exportExcelDocument(Map<String, Map<String, String>> titleMaps, Map<String, List<Map>> results,
            OutputStream out) throws Exception {
        // 声明一个工作薄
        Workbook workbook = new HSSFWorkbook();

        for (String s : titleMaps.keySet()) {
            Map<String, String> titleMap = titleMaps.get(s);
            List<Map> result = results.get(s);

            // 生成一个表格
            Sheet sheet = workbook.createSheet((s == null || "".equals(s)) ? "未命名" : s);
            // 网格线
            sheet.setDisplayGridlines(true);
            // 设置默认表宽
            sheet.setDefaultColumnWidth(20);
            // 产生表格标题行
            Row row = sheet.createRow(0);
            // 生成一个样式
            CellStyle titleStyle = workbook.createCellStyle();
            // 设置这些样式
            titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
            titleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);

            // 生成一个字体
            Font titleFont = workbook.createFont();
            titleFont.setFontName("Arial");
            titleFont.setFontHeightInPoints((short) 12);
            titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
            // 把字体应用到当前的样式
            titleStyle.setFont(titleFont);
            int count = 0;
            for (String key : titleMap.keySet()) {
                Cell cell = row.createCell(count);
                cell.setCellStyle(titleStyle);
                cell.setCellValue(titleMap.get(key));
                count++;
            }
            if (result != null) {
                for (int i = 0, iSize = result.size(); i < iSize; i++) {
                    Map<String, Object> resultMap = result.get(i);
                    // 生成一个字体
                    Font contentFont = workbook.createFont();
                    contentFont.setFontName("宋体");
                    contentFont.setFontHeightInPoints((short) 10);
                    contentFont.setBoldweight(Font.BOLDWEIGHT_NORMAL);
                    // 生成一个样式
                    CellStyle contentStyle = workbook.createCellStyle();
                    contentStyle.setAlignment(CellStyle.ALIGN_LEFT);
                    contentStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
                    row = sheet.createRow(i + 1);
                    // 把字体添加到样式中去
                    contentStyle.setFont(contentFont);
                    count = 0;
                    for (String key : titleMap.keySet()) {
                        Cell cell = row.createCell(count);
                        cell.setCellStyle(contentStyle);
                        cell.setCellValue(String.valueOf(resultMap.get(key)));
                        count++;
                    }
                }
            }

        }
        workbook.write(out);
        out.flush();
        out.close();
    }

4.结束总结:

通过springmvc封装的下载实现ResponseEntity将字符串输出成文件下载。

ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);

 

转载于:https://www.cnblogs.com/DIVEY/p/10213312.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值