美文网首页
测试导出

测试导出

作者: 忠犬_1c67 | 来源:发表于2019-11-26 15:57 被阅读0次

    ···
    /**
    * 测试输入不正确的文件导出
    *
    * @param filecontent
    */

    public static void approvalFile(MultipartFile filecontent) {
        OutputStream os = null;
        InputStream inputStream = null;
        String fileName = null;
        try {
            inputStream = filecontent.getInputStream();
            fileName = filecontent.getOriginalFilename();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            String path = "E:\\testExport\\";
            // 2、保存到临时文件
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流保存到本地文件
            File tempFile = new File(path);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }
            os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
            // 开始读取
            while ((len = inputStream.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 完毕,关闭所有链接
            try {
                os.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * excel文件导出
     *
     * @param response
     * @param list
     */
    public static void exportExcel(HttpServletResponse response, List<CnbFundSchemeDto> list) {
        //创建HSSFWorkbook对象(excel的文档对象)
        HSSFWorkbook wb = new HSSFWorkbook();
        //建立新的sheet对象(excel的表单)
        HSSFSheet sheet = wb.createSheet("公积金方案表");
        //在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
        HSSFRow row1 = sheet.createRow(0);
        //创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
        HSSFCell cell = row1.createCell(0);
        //设置单元格内容
        cell.setCellValue("公积金方案表");
        //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));
        //在sheet里创建第二行
        HSSFRow row2 = sheet.createRow(1);
        /*HSSFCell cell1 = row2.createCell(0);
        HSSFCell cell2 = row2.createCell(1);
        HSSFCell cell3 = row2.createCell(2);
        HSSFCell cell4 = row2.createCell(3);
        HSSFCell cell5 = row2.createCell(4);
        HSSFCell cell6 = row2.createCell(5);
        //设置列宽
        sheet.setColumnWidth( cell1.getColumnIndex(),256 * 25);
        sheet.setColumnWidth( cell2.getColumnIndex(),256 * 20);
        sheet.setColumnWidth( cell3.getColumnIndex(),256 * 20);
        sheet.setColumnWidth( cell4.getColumnIndex(),256 * 20);
        sheet.setColumnWidth( cell5.getColumnIndex(),256 * 20);
        sheet.setColumnWidth( cell6.getColumnIndex(),256 * 20);*/
        //设置缺省列宽
        sheet.setDefaultColumnWidth(20);
        //设置缺省列高
        sheet.setDefaultRowHeightInPoints(30);
        //创建单元格并设置单元格内容
        row2.createCell(0).setCellValue("序号");
    
        HSSFCellStyle style = wb.createCellStyle();
        // 实例化样式对象
        row2.createCell(1).setCellValue("方案名称");
        // 垂直居中
        //style.setAlignment(HorizontalAlignment.CENTER);
        //将样式应用到行
        cell.setCellStyle(style);
        row2.createCell(2).setCellValue("城市名");
        row2.createCell(3).setCellValue("组织名");
        row2.createCell(4).setCellValue("年份");
        row2.createCell(5).setCellValue("统计");
        for (int i = 0; i < list.size(); i++) {
            HSSFRow rows = sheet.createRow(i + 2);
            rows.createCell(0).setCellValue(list.get(i).getOrderBy() != null ? list.get(i).getOrderBy().toString() : null);
            rows.createCell(1).setCellValue(list.get(i).getFundSchemeNameSys());
            rows.createCell(2).setCellValue(list.get(i).getCityName());
            rows.createCell(3).setCellValue(list.get(i).getLocalFullUnitName());
            rows.createCell(4).setCellValue(list.get(i).getSubordinateYear());
            rows.createCell(5).setCellValue(list.get(i).getPayTotal().toString());
        }
        try {
            String fileName = new String(("公积金方案表.xls").getBytes(), "UTF-8");
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
    
            OutputStream os = response.getOutputStream();
            wb.write(os);
            FileOutputStream output = new FileOutputStream("E:\\testExport\\公积金方案表.xls");
            wb.write(output);
            os.flush();
            os.close();
        } catch (Exception var8) {
            throw new HrmsException("导出excel发生异常", var8);
        }
    
    }
    

    ···

    相关文章

      网友评论

          本文标题:测试导出

          本文链接:https://www.haomeiwen.com/subject/yzxywctx.html