java生成csv文件,excel打开文件乱码问题

之前项目中用到了生成csv文件编码UTF-8在windos下excel打开乱码
二话不说直接撸上代码

解决方法如下:
1:生成csv文件封装类

public class CreateCsvUtil {  @SuppressWarnings("rawtypes")
    public static File createCSVFile(List exportData, LinkedHashMap map, String outPutPath,String fileName) {
        File csvFile = null;
        BufferedWriter csvFileOutputStream = null;
        try {
            File file = new File(outPutPath);
            if (!file.exists()&&!file.isDirectory()) {
                file.mkdirs();
            }
            //定义文件名格式并创建
            csvFile = File.createTempFile(fileName, ".csv", new File(outPutPath));
            System.out.println("csvFile:" + csvFile);
            FileOutputStream fileOutputStream = new FileOutputStream(csvFile);
            //加入bom 否则生成的csv文件 用excel乱码
            fileOutputStream.write(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF});
            // UTF-8使正确读取分隔符"," 
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream
                    , "utf-8");
            csvFileOutputStream = new BufferedWriter(outputStreamWriter, 1024);
            System.out.println("csvFileOutputStream:" + csvFileOutputStream);
            // 写入文件头部
            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(); ) {
                Object row = (Object) iterator.next();
                for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator.hasNext(); ) {
                    java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
                            .next();
                    //如果是空值则进行处理用 "-" 号填充 否则会抛空指针
                   String va="-";
                    String property = BeanUtils.getProperty(row, (String) propertyEntry.getKey());
                    if(property!=null&&!property.equals("")){
                        csvFileOutputStream.write((String)property);
                    }else{
                        csvFileOutputStream.write((String)va);
                    }
                    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();
            }
        }
        return csvFile;
    }
}

2:csv数据填充:

	   /**
	     * java生成csv文件
	     */
	public void createCsvFile(HttpServlteRequest request,HttpServlteResponse response){
	    	List exportData = new ArrayList();
	        Map row = new LinkedHashMap();
	        row.put("1",1);
	        row.put("2",2);
	        row.put("3",3);
	        row.put("4",4);
	        exportData.add(row);
	        //设置列名
	        LinkedHashMap map = new LinkedHashMap();
	        map.put("1","姓名");
	        map.put("2","年龄");
	        map.put("3","性别");
	        map.put("4","证件");
	        //文件名=生产的文件名称
	        String fileName = "文件名称";
	        String path="服务器路径";
	        File file = CreateCsvUtil.createCSVFile(exportData, map, path, fileName);
	 }	

成功解决生成csv文件excel打开乱码问题

如有问题欢迎留言!

注:转载请注明出处

你可能感兴趣的:(java生成csv文件,excel打开文件乱码问题)