Android 导出CSV文件乱码问题处理

最近有一个需求,需要在Android端导出CSV文件,自测是用的WPS,没啥问题。可到了测试那边,用Excel直接打开就是乱码,需要在Excel数据里面用【从文件/CSV】打开。这样就显示非常的不方便。

解决办法:

java 复制代码
public static void exportToCsv(List<String[]> data, String filePath) throws IOException {
        File csvFolder = new File(FileIO.CSV_PATH);
        boolean isPresent = csvFolder.exists();
        if (!isPresent) {
            csvFolder.mkdir();
        }
        //FileWriter writer = new FileWriter(filePath);
        FileOutputStream fos = new FileOutputStream(filePath);
        // 写入BOM
        fos.write(0xEF); // 239
        fos.write(0xBB); // 191
        fos.write(0xBF); // 189
        OutputStreamWriter writer = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
        for (String[] row : data) {
            String csvRow = String.join(",", row);

            writer.write(csvRow + "\n");
        }
        writer.flush();
        writer.close();
    }

解决方案:使用UTF-8 BOM。

相关推荐
帅锅锅00721 分钟前
SeLinux 全面详解
android·linux
只想搞钱的肥仔38 分钟前
Android thermal (5)_cooling device(下)
android
某空m1 小时前
【Android】BottomNavigationView实现底部导航栏
android·java
撩得Android一次心动2 小时前
Android 四大组件桥梁 —— Intent (意图) 详解
android
用户2018792831673 小时前
MVP架构模式:餐厅点餐的有趣故事
android
用户2018792831673 小时前
MVVM 架构模式:咖啡馆的智能点餐系统
android
用户2018792831673 小时前
浅析Android MVC架构
android
AsiaLYF5 小时前
kotlin中MutableStateFlow和MutableSharedFlow的区别是什么?
android·开发语言·kotlin
2501_916008895 小时前
iOS 发布全流程详解,从开发到上架的流程与跨平台使用 开心上架 发布实战
android·macos·ios·小程序·uni-app·cocoa·iphone
4Forsee5 小时前
【Android】浅析 Android 的 IPC 跨进程通信机制
android·java