已解决-当我从Java文件夹中下载Excel时,出现此“ .xls文件的格式和扩展名不匹配”

问题描述 投票:0回答:1

正如标题所述,当我从文件夹下载Excel工作表时,我收到此消息

这是我的代码

RequestMapping(value = "/descargar-datos-entrada/{idSimulacion}", method = RequestMethod.GET)
    public void descargarDatosEntrada(@PathVariable("idSimulacion") String idSimulacion, HttpServletResponse response)
            throws IOException {
        Properties properties = new Properties();
        properties = Util.getProperties("mongo");
        FileInputStream inputStream = null;
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; filename=Hoja-Resultados-Descarga.xls");

        try {

            Simulacion simulacion = simulacionService.finById(idSimulacion);
            inputStream = new FileInputStream(
                    properties.getProperty("ruta.copia.resultados.excel") + idSimulacion + ".xls");

            int c;
            while ((c = inputStream.read()) != -1) {
                response.getWriter().write(c);
            }

        } catch (SimulacionException | IOException e) {
            Util.autoLogError(e);
        } finally {
            if (inputStream != null)
                inputStream.close();
            response.getWriter().close();
        }

    }

结果.xls文件将如下所示:

enter image description here

我希望获得一些帮助或建议,在此先感谢您,这是我的第一个问题,请原谅,如果我提出的问题不正确。

java excel apache-poi xls
1个回答
1
投票

getOutputStream执行二进制输出,而不是文本输出getWriter

        Path path = Paths.get(properties.getProperty("ruta.copia.resultados.excel")
                    + idSimulacion + ".xls");
        Files.copy(path, response.getOutputStream());
© www.soinside.com 2019 - 2024. All rights reserved.