java导出excel表格,文件名称汉字话

//导出Excel
    @RequestMapping("/export")
    @ResponseBody
    public void createExcel(HttpServletResponse response, HttpServletRequest request)
            throws IOException {
        String districtCode = request.getParameter("districtCode");
        String codeFlage = request.getParameter("codeFlage");
        AreaNexus areaNexus = new AreaNexus();
        areaNexus.setParentCode(districtCode);
        AreaNexus areaNexusDistrict = areaNexusMapper.getCityOrgCode(areaNexus);
        String districtName = "";

        if (areaNexusDistrict != null) {
            districtName = areaNexusDistrict.getOrgName();
        }
        String dst_fname = districtName ;
        //文件名称 汉字显示
        try {
            dst_fname = new String(dst_fname.getBytes("GB2312"), "8859_1");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }

        //导出的文件的数据
        List<EhrPirBean> newlist = new ArrayList<EhrPirBean>();
        if ("2".equals(codeFlage)) {
            newlist = ehrPirInfoMapper.getCityEhrRepeat();
        } else {
            newlist = ehrPirInfoMapper.getCountyRepeatDetail(districtCode);
        }
        //获取查询结果的数据,reportlist为别的方法查询出来的数据,格式为List<Object[]>,其实这里不管reportlist是什么数据格式,这里只要对其进行封装就行了
        //数据封装,这里的map之所以敢这样add是因为这里的add顺序和hql中的select字段顺序是一样的,总共就查询那么多字段
        List<Map<String, Object>> solist = new ArrayList();
        for (EhrPirBean obj : newlist) {
            //每次循环都要重新new一个map,表示不同对象
            Map<String, Object> map = new HashMap();
            map.put("name", obj.getName());
            map.put("idnumber", obj.getIdNo());
            map.put("healthRecordCode", obj.getHealthRecordCode());
            //map.put("nation", obj.getNationCd());
            map.put("sex", obj.getGenderCd());
            map.put("tel", obj.getTel());
            map.put("orgName", obj.getOrgName());
            map.put("districtName", obj.getDistrictName());
            solist.add(map);
        }
        //创建HSSFWorkbook对象(excel的文档对象)
        HSSFWorkbook wb = new HSSFWorkbook();
        //建立新的sheet对象(excel的表单)
        HSSFSheet sheet = wb.createSheet(districtName );
        //在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
        HSSFRow row1 = sheet.createRow(0);
        //创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
        HSSFCell cell = row1.createCell(0);

        // 1.生成字体对象  
        HSSFFont font = wb.createFont();
        font.setFontHeightInPoints((short) 12);
        font.setFontName("新宋体");

        // 2.生成样式对象,这里的设置居中样式和版本有关,我用的poi用HSSFCellStyle.ALIGN_CENTER会报错,所以用下面的
        HSSFCellStyle style = wb.createCellStyle();
        //      style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//设置居中样式
        style.setFont(font); // 调用字体样式对象  
        style.setWrapText(true);
        // style.setAlignment(HorizontalAlignment.CENTER);//设置居中样式

        // 3.单元格应用样式  
        cell.setCellStyle(style);

        //设置单元格内容

        cell.setCellValue(districtName + "重复建档个人基本信息");
        //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));

        //在sheet里创建第二行
        HSSFRow row2 = sheet.createRow(1);
        //创建单元格并设置单元格内容及样式
        HSSFCell cell0 = row2.createCell(0);
        cell0.setCellStyle(style);
        cell0.setCellValue("姓名");

        HSSFCell cell1 = row2.createCell(1);
        cell1.setCellStyle(style);
        cell1.setCellValue("身份证号");

        HSSFCell cell2 = row2.createCell(2);
        cell2.setCellStyle(style);
        cell2.setCellValue("的规划");

扫描二维码关注公众号,回复: 2514504 查看本文章

        HSSFCell cell3 = row2.createCell(3);
        cell3.setCellStyle(style);
        cell3.setCellValue("性别");

        HSSFCell cell4 = row2.createCell(4);
        cell4.setCellStyle(style);
        cell4.setCellValue("电话");

        HSSFCell cell5 = row2.createCell(5);
        cell5.setCellStyle(style);
        cell5.setCellValue("是法国和");

        HSSFCell cell6 = row2.createCell(6);
        cell6.setCellStyle(style);
        cell6.setCellValue("建复活节");

        String name;
        String idnumber;
        String healthRecordCode;
        String sex;
        String tel;
        String orgName;
        String districtNameExcel;
        //宽度自适应可自行选择自适应哪一行,这里写在前面的是适应第二行,写在后面的是适应第三行
        for (int i = 0; i < solist.size(); i++) {
            //单元格宽度自适应
            sheet.autoSizeColumn((short) 0);
            sheet.autoSizeColumn((short) 1);
            sheet.autoSizeColumn((short) 2);
            sheet.autoSizeColumn((short) 3);
            sheet.autoSizeColumn((short) 4);
            sheet.autoSizeColumn((short) 5);
            //从sheet第三行开始填充数据
            HSSFRow rowx = sheet.createRow(i + 2);
            Map<String, Object> map = solist.get(i);
            //这里的name,idnumber等都是前面定义的全局变量
            name = (String) map.get("name");
            HSSFCell cell00 = rowx.createCell(0);
            //cell00.setCellStyle(style);
            cell00.setCellValue(name);

            idnumber = (String) map.get("idnumber");
            HSSFCell cell01 = rowx.createCell(1);
            //cell01.setCellStyle(style);
            cell01.setCellValue(idnumber);

            healthRecordCode = (String) map.get("healthRecordCode");
            HSSFCell cell02 = rowx.createCell(2);
            //cell02.setCellStyle(style);
            cell02.setCellValue(healthRecordCode);

            sex = (String) map.get("sex");
            HSSFCell cell03 = rowx.createCell(3);
            //cell03.setCellStyle(style);
            cell03.setCellValue(sex);

            tel = (String) map.get("tel");
            HSSFCell cell04 = rowx.createCell(4);
            // cell04.setCellStyle(style);
            cell04.setCellValue(tel);

            orgName = (String) map.get("orgName");
            HSSFCell cell05 = rowx.createCell(5);
            // cell05.setCellStyle(style);
            cell05.setCellValue(orgName);

            districtNameExcel = (String) map.get("districtName");
            HSSFCell cell06 = rowx.createCell(6);
            //cell06.setCellStyle(style);
            cell06.setCellValue(districtNameExcel);
        }
       
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        try {
            wb.write(os);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        byte[] content = os.toByteArray();
        InputStream is = new ByteArrayInputStream(content);
        // 设置response参数,可以打开下载页面
        response.reset();
        String charsetStr = "UTF-8";
        response.setContentType("application/vnd.ms-excel;charset=" + charsetStr);
        response.setHeader("Content-Disposition", "attachment;filename=" + dst_fname + ".xlsx");
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(out);
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bis != null)
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            if (bos != null)
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

猜你喜欢

转载自blog.csdn.net/wgd930701/article/details/81240838
今日推荐