欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

写csv解决Excel打开乱码问题

程序员文章站 2024-01-31 14:25:10
...

写csv解决以文本和Excel打开乱码问题


    public static File createCSVFile(List<?> exportData, LinkedHashMap map, String outPutPath,
                                     String fileName) {


        File csvFile = null;

        if (exportData.size() > 0) {

            BufferedWriter csvFileOutputStream = null;
            try {
                File file = new File(outPutPath);
                if (!file.exists()) {
                    file.mkdir();
                }
                //定义文件名格式并创建  
                csvFile = new File(outPutPath + "/" + fileName + ".csv");
                //System.out.println("csvFile:" + csvFile);
                // UTF-8使正确读取分隔符","  
                // 如果生产文件乱码,windows下用gbk,linux用UTF-8
                csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "UTF-8"), 1024);
                //System.out.println("csvFileOutputStream:" + csvFileOutputStream);
                // 写入文件头部  
                //csvFileOutputStream.write(new byte[]{(byte) 0xEF, (byte) 0xBB,(byte) 0xBF});
                 //解决csv文件在Excel打开乱码问题
                csvFileOutputStream.write(new String(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}));

                for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator.hasNext(); ) {
                    java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
                    csvFileOutputStream.write((String) propertyEntry.getValue() != null ? (String) propertyEntry.getValue() : "");
                    if (propertyIterator.hasNext()) {
                        csvFileOutputStream.write(",");
                    }
                }
                csvFileOutputStream.newLine();

                // 写入文件内容  
                for (Iterator iterator = exportData.iterator(); iterator.hasNext(); ) {
                    Map row = (Map) iterator.next();

                    for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator.hasNext(); ) {
                        java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
                        csvFileOutputStream.write((String) String.valueOf(row.getOrDefault(propertyEntry.getKey(), "")));
                        if (propertyIterator.hasNext()) {
                            csvFileOutputStream.write(",");
                        }
                    }

                    if (iterator.hasNext()) {
                        csvFileOutputStream.newLine();
                    }
                }
                csvFileOutputStream.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    csvFileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            log.info(String.format("[%s] 文件为空,不生成", fileName, ".csv"));
        }
        return csvFile;
    }

本代码主要是在写入头部文件之前写入一下代码解决

//解决csv文件在Excel打开乱码问题
csvFileOutputStream.write(new String(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}));