• Excel导出文件流下载


    Controller.cs

    @CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,  
                RequestMethod.POST, RequestMethod.DELETE, RequestMethod.OPTIONS,  
                RequestMethod.HEAD, RequestMethod.PUT, RequestMethod.PATCH}, origins="*")
        @RequestMapping("/ot")
        @ResponseBody
        public void ot(String key, HttpServletResponse response) {
            try {
                String storeName = key+".xlsx";
                XSSFWorkbook workBook = excelService.outputExcel(key);
                ByteArrayOutputStream bt = new ByteArrayOutputStream();
                workBook.write(bt);
                workBook.close();
                bt.flush();
                ByteArrayInputStream bis = new ByteArrayInputStream(bt.toByteArray());
                response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");  
                response.setHeader("Content-disposition", "attachment; filename="  
                    + new String(storeName.getBytes("utf-8"), "ISO8859-1")); 
                //设置输出长度
                response.setHeader("Content-Length", String.valueOf(bt.size()));
                BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
                byte[] buff = new byte[2048];  
                int bytesRead;  
                while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
                  bos.write(buff, 0, bytesRead);  
                }  
                //关闭流
                bis.close();  
                bos.close();  
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    Service.cs

    public XSSFWorkbook outputExcel(String key){
            String json = "{status:'OK', msg: '导出完毕!'}";
            
            String schame = key.split("\.")[0];
            String tablename = key.split("\.")[1];
            XSSFWorkbook workBook = new XSSFWorkbook();
            XSSFSheet sheet = workBook.createSheet();
            workBook.setSheetName(0,"Sheet1");
            XSSFRow titleRow = sheet.createRow(0);
            titleRow.createCell(0).setCellValue(tablename);
            Jedis jedis = new Jedis("10.8.4.94");
            List<String> tables = new ArrayList<String>();
            jedis.select(2);
            try {
                tables = jedis.lrange("db_tables", 0, -1);
            } catch (Exception e) {
                // TODO: handle exception
            }finally{
                jedis.close();
            }
            
            for(String s:tables){
                JSONObject obj = JSON.parseObject(s);
                if(obj.getString("owner").equals(schame) && obj.getString("table_name").equals(tablename)){
                    titleRow.createCell(1).setCellValue(obj.getString("comment"));
                }
            }
            
            XSSFRow secondRow = sheet.createRow(1);
            secondRow.createCell(0).setCellValue("字段英文名");
            secondRow.createCell(1).setCellValue("字段类型");
            secondRow.createCell(2).setCellValue("注释");
            secondRow.createCell(3).setCellValue("是否主键");
            secondRow.createCell(4).setCellValue("是否非空");
            secondRow.createCell(5).setCellValue("默认值");
            
            jedis = new Jedis("10.8.4.94");
            List<String> columns = new ArrayList<String>();
            jedis.select(2);
            try {
                columns = jedis.lrange(key, 0, -1);
            } catch (Exception e) {
                // TODO: handle exception
            }finally{
                jedis.close();
            }
            
            int k = 2;
            for(String s: columns){
                JSONObject obj = JSON.parseObject(s);
                XSSFRow row = sheet.createRow(k);
                row.createCell(0).setCellValue(obj.getString("column_name"));
                row.createCell(1).setCellValue(obj.getString("data_type").equals("VARCHAR2")?(obj.getString("data_type")+"("+obj.getString("data_length")+")"):obj.getString("data_type"));
                row.createCell(2).setCellValue(obj.getString("comment"));
                row.createCell(3).setCellValue(obj.getString("pk"));
                row.createCell(4).setCellValue(obj.getString("nullable"));
                row.createCell(5).setCellValue(obj.getString("data_default"));
                k++;
            }
            
            return workBook;
        }
  • 相关阅读:
    解决gsoap中文乱码的问题
    30个高质量的旅游网站设计
    Windows Azure功能更新:弹性伸缩(autoscale)、监控报警、移动服务及网站服务商用、新的虚拟机镜像
    工作站服务没有启动 解决办法
    直方图与匹配
    横瓜先生关于如何利用MYSQL数据库设计CMS系统处理100亿级TB规模的数据量
    IOS设计模式学习(6)生成器
    关于页面上某些点击事件无效的问题
    lll
    android sqlite使用之模糊查询数据库数据的三种方式
  • 原文地址:https://www.cnblogs.com/wpcnblog/p/9158783.html
Copyright © 2020-2023  润新知