java excel 写入与下载实现 (解决乱码问题)

情况描述:
Excel的默认编码格式是 ANSI,但是 office 版本有很多
旧版本的excel无法打开utf-8编码的文件,只能打开ANSI编码的文件 (如office2007版本)
新版本的excel可以打开utf-8编码的文件,也能打开ANSI编码的文件 (如office2010版本)
因此,不能只是输出一个文件然后给它命名为 .csv/.xlx 这样无法做到版本兼容

解决方式:
生成一个excel然后再输出,这样就可以做到版本兼容

代码:
1,2,3
4,5,6
7,8,9
文件内容如上所示,然后写入到excel并输出

    /**
     *  将文本内容写入Excel,然后下载该Excel
     * @param pathfile 待读取的文本路径
     * @param response 响应
     * @param fileName 生成的Excel文件名称  (XXX.xls)
     */
    public void downLoadFile(String pathfile, HttpServletResponse response, String fileName) {
        try {
            //解决下载后的文件中文名乱码问题
            fileName = new String(fileName.getBytes("GBK"), "iso-8859-1");
        } catch (UnsupportedEncodingException e) {
            logger.error("", e);
            view.viewString(ParamUtils.errorParam("不支持iso-8859-1编码格式"), response);
        }

        //声明一个工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        //生成一个表格
        HSSFSheet sheet = workbook.createSheet(fileName);
        //向excel中写入内容
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            fis = new FileInputStream(pathfile);
            isr = new InputStreamReader(fis,"utf-8");
            br = new BufferedReader(isr);

            String line;
            int i = 0;
            //按行读取文件中的内容,然后写入Excel中(PIO也可以设置Excel的样式)
            while((line = br.readLine()) != null){
                String str = line;
                String[] splits = str.split(",");
                HSSFRow nrow = sheet.createRow(i);
                for (int j = 0; j < splits.length; j++) {
                    HSSFCell nCell = nrow.createCell(j);
                    nCell.setCellValue(splits[j]);
                }
                i++;
            }
        } catch (FileNotFoundException e) {
            logger.error("指定的读取文件不存在\"", e);
        } catch (UnsupportedEncodingException e) {
            logger.error("文本内容不能转成 utf-8", e);
        } catch (IOException e) {
            logger.error("写入内容出错", e);
        } finally {
            try {
                fis.close();
                isr.close();
                br.close();
            } catch (IOException e) {
                logger.error("读取流关闭异常", e);
            }
        }

        //输出excel
        response.reset();
        response.setCharacterEncoding("utf-8");
        // Content-disposition 告诉浏览器以下载的形式打开
        response.setHeader("Content-disposition", "attachment; filename=" + fileName);
        // application/ms-excel;charset=utf-8 告诉浏览器下载的文件是excel
        response.setContentType("application/ms-excel");
        OutputStream out = null;
        try {
            out = new BufferedOutputStream(response.getOutputStream());
            workbook.write(out);
        } catch (IOException e) {
            logger.error("excel导出有误", e);
        } finally {
            try {
              out.close();
            } catch (IOException e) {
                logger.error("读取内容有误", e);
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_32657967/article/details/84784869