zoukankan      html  css  js  c++  java
  • 数据的导入导出

    一.Excel的数据导入

    A:OCUpload实现文件的上传

    1、 js文件复制到项目中

    2、 在页面中引入一键上传js文件

    <script type="text/javascript" src="../js/ocupload/jquery.ocupload-1.1.2.js"></script>

    必须引入jQuery

    3、在页面中提供任意一个元素

    <input id="mybutton" type="button" value="upload">

    4、调用一键上传插件提供的upload方法,作用是动态修改页面HTML代码

    $(function(){
            $("#mybutton").upload({
                name:'myFile',
                action:'xx.action'
            });
        });

    action指定访问的后台action的位置.服务端解析上传的文件.

    B:Apache POI解析Excel文件

    1.poi的maven坐标引入

            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi</artifactId>
                <version>${poi.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml</artifactId>
                <version>${poi.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.poi</groupId>
                <artifactId>poi-ooxml-schemas</artifactId>
                <version>${poi.version}</version>
            </dependency>

    2.页面方法调用(例)

                    $("#button-import").upload({
                        name:"areaFile",
                        action:"../../areaAction_importXls.action"
                    });

     3.后台处理

        private File areaFile;
        
        public void setAreaFile(File areaFile) {
            this.areaFile = areaFile;
        }
        
        @Autowired
        private AreaService service;
    
        @Action(value="areaAction_importXls")
        public String importXls() throws Exception {
            //通过输入流加载excle文件
            HSSFWorkbook workbook= new HSSFWorkbook(new FileInputStream(areaFile));
            //获得第一个sheet表单
            HSSFSheet sheetAt = workbook.getSheetAt(0);
            //定义一个集合,遍历所有行,获得数据后通过构造方法生成area对象,在把area对象添加到集合中
            List<Area> list = new ArrayList<>();
            for (Row row : sheetAt) {
                if(row.getRowNum() == 0){
                    continue;
                }
                String id = row.getCell(0).getStringCellValue();
                String provice = row.getCell(1).getStringCellValue();
                String city = row.getCell(2).getStringCellValue();
                String district = row.getCell(3).getStringCellValue();
                String postcode = row.getCell(4).getStringCellValue();
                Area area = new Area(id,provice,city,district,postcode);
                list.add(area);
            }
            //调用service的保存方法
            service.save(list);
            return NONE;
        }

     二.Excel文件的导出(例)

    1.修改页面导出按钮,绑定方法

                function doExport(){
                    //发送ajax请求,通过action查询所有分区,通过poi导出到Excel中,通过流进行下载
                    window.location.href="../../subAreaAction_exportXls.action";
                }

    2、通过POI将查询到的分区数据写到Excel文件中,通过输出流将文件写回客户端进行文件下载

    @Action(value="subareaAction_exportXls")
        public String exportXls() throws IOException{
            //1、查询所有分区数据
            List<SubArea> list = service.findAll();
            //2、基于POI在内存中创建一个Excel文件
            HSSFWorkbook excel = new HSSFWorkbook();
            //3、在Excel文件中创建一个sheet页
            HSSFSheet sheet = excel.createSheet("分区数据统计");
            //4、在标签页中创建行
            HSSFRow title = sheet.createRow(0);
            //5、在行中创建列
            title.createCell(0).setCellValue("编号");
            title.createCell(1).setCellValue("分区起始编号");
            title.createCell(2).setCellValue("分区结束编号");
            title.createCell(3).setCellValue("分区关键字");
            title.createCell(4).setCellValue("辅助关键字");
            title.createCell(5).setCellValue("区域信息");
            for(SubArea subarea : list){
                //每个分区对象对应一行数据
                HSSFRow data = sheet.createRow(sheet.getLastRowNum() + 1);
                //在行中创建列
                data.createCell(0).setCellValue(subarea.getId());
                data.createCell(1).setCellValue(subarea.getStartNum());
                data.createCell(2).setCellValue(subarea.getEndNum());
                data.createCell(3).setCellValue(subarea.getKeyWords());
                data.createCell(4).setCellValue(subarea.getAssistKeyWords());
                data.createCell(5).setCellValue(subarea.getArea().getName());
            }
            
            //6、通过输出流写回Excel文件到浏览器,文件下载需要一个流(输出流)、两个头(设置头信息)
            ServletOutputStream out = ServletActionContext.getResponse().getOutputStream();
            String filename = "分区数据统计结果.xls";
            String agent = ServletActionContext.getRequest().getHeader("User-Agent");//客户端使用的浏览器类型
            //处理中文文件名
            filename = FileUtils.encodeDownloadFilename(filename, agent);
            String mimeType = ServletActionContext.getServletContext().getMimeType(filename);
            ServletActionContext.getResponse().setContentType(mimeType);
            ServletActionContext.getResponse().setHeader("content-disposition", "attachment;filename=" + filename);
            excel.write(out);
            return NONE;
        }
  • 相关阅读:
    广域网(ppp协议、HDLC协议)
    0120. Triangle (M)
    0589. N-ary Tree Preorder Traversal (E)
    0377. Combination Sum IV (M)
    1074. Number of Submatrices That Sum to Target (H)
    1209. Remove All Adjacent Duplicates in String II (M)
    0509. Fibonacci Number (E)
    0086. Partition List (M)
    0667. Beautiful Arrangement II (M)
    1302. Deepest Leaves Sum (M)
  • 原文地址:https://www.cnblogs.com/cocosili/p/7117845.html
Copyright © 2011-2022 走看看