この問題を解決するためのJavaのエクスポートExcelが文字化けして読めないオープンエクスポートされたファイルを復元します

インポート・パッケージには、それはまだそれの簡単なポイントですので、私は、彼らは常に読めないエラーになりますストリームをエクスポートする理由がわからない、忍耐、POIに対処する必要はありません自分自身を持っているでしょう。

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.9</version>
</dependency>

ツールコード:

public class FileUtil {

    public static void download(String filename, HttpServletResponse res) {
        String filePath = "./template/" + filename;
        try (OutputStream os = res.getOutputStream(); InputStream bis = new BufferedInputStream(new ClassPathResource(filePath).getInputStream())){
            // 设置信息给客户端不解析
            String type = new MimetypesFileTypeMap().getContentType(filename);
            // 设置content-type,即告诉客户端所发送的数据属于什么类型
            res.setContentType(type);
            // 设置编码
            String name = URLEncoder.encode(filename, "UTF-8");

            // 设置扩展头,当Content-Type 的类型为要下载的类型时 , 这个信息头会告诉浏览器这个文件的名字和类型。
            res.setHeader("Content-Disposition", "attachment;filename=" + name);
            XSSFWorkbook workbook = new XSSFWorkbook(bis);
            workbook.write(os);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}

フレーズを中心に:XSSFWorkbookワークブック=新しいXSSFWorkbook(ビス)。

ファイルの場所:

公開された56元の記事 ウォン称賛75 ビュー280 000 +

おすすめ

転載: blog.csdn.net/seanxwq/article/details/104774543