easyexcel icon indicating copy to clipboard operation
easyexcel copied to clipboard

如何生成一个带有BOM头的csv文件

Open FristDawn opened this issue 1 year ago • 1 comments

public static Pair<ExcelWriter, WriteSheet> buildCsvWriter(File file, String sheetName, Charset charset) { ExcelWriter excelWriter = EasyExcel.write(file).charset(charset).build(); WriteSheet writeSheet = new WriteSheet(); writeSheet.setSheetName(sheetName);

    return Pair.of(excelWriter, writeSheet);
}

再生成文件写入句柄的时候,设置了utf-8编码格式,但是生成文件无法带bom头, tempFile = File.createTempFile(SESSION_DETAIL_FILE_NAME + System.currentTimeMillis(), ".csv",dir); fos = new FileOutputStream(tempFile.getPath()); fos.write(new byte[]{(byte)0xEF, (byte)0xBB, (byte)0xBF}); fos.flush(); 在获取句柄前单独写入也不行 会话明细17272333965255218563994055952976.csv: Unicode text, UTF-8 text, with CRLF line terminators

所以我该如何生成一个带有bom头的csv文件呢?

FristDawn avatar Sep 25 '24 03:09 FristDawn

   try(OutputStream os = response.getOutputStream()){

        String filename = "export.csv";
        response.setHeader("Content-Disposition", "attachment;fileName=" + filename);

        //写bom,解决微软Excel乱码。
        byte[] bom = {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF};
        os.write(bom);

        FileUtils.copyFile(file1, os);

}catch(Exception e){

}

imgoby avatar Sep 25 '24 04:09 imgoby