解决导出CSV文件乱码的问题

这几天处理bug,在解决这个导出csv格式文件乱码的问题,
记录一下;
1,处理前代码

public static void exportList(String[] headers, String[] columns, List dtos, String sheetName,
			HttpServletResponse response) throws Exception {
		List header = Arrays.asList(headers);
		List fids = Arrays.asList(columns);
		List list = genDtoExportData(dtos, fids);

		File file = File.createTempFile("export", ".csv");

		StringBuffer sb = new StringBuffer("");
		for (String h : headers) {
			sb.append(transfer(h)).append(",");
		}
		sb.append("\r\n");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (Object obj : list) {
			for (Object obj2 : (List) obj) {
				if (obj2 instanceof Date) {
					sb.append(sdf.format((Date) obj2)).append(",");
				} else {
					sb.append(transfer(obj2.toString())).append(",");
				}
			}
			sb.append("\r\n");
		}
		OutputStream out = null;
		OutputStreamWriter writer = null;
		try {
			out = new FileOutputStream(file);
			writer = new OutputStreamWriter(out);
			writer.append(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(writer != null){
				writer.close();
			}
			if(out != null){
				out.close();
			}
		}

		response.setContentType("text/csv; charset=\"utf-8\"");
		response.setHeader("Content-disposition", "attachment; filename="+transferFilename(sheetName)+".csv");
		byte[] buffer = new byte[1024];
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		try {
			fis = new FileInputStream(file);
			bis = new BufferedInputStream(fis);
			OutputStream os = response.getOutputStream();
			int i = bis.read(buffer);
			while (i != -1) {
				os.write(buffer, 0, i);
				i = bis.read(buffer);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bis != null)
				bis.close();
			if (fis != null)
				fis.close();
			file.delete();
		}
	}
	/** 处理双引号和逗号的特殊转译 */
	public static String transfer(String str) {
		String tempDescription = str;
		// 如果有逗号
		if (str.contains(",")) {
			// 如果还有双引号,先将双引号转义,避免两边加了双引号后转义错误
			if (str.contains("\"")) {
				tempDescription = str.replace("\"", "\"\"");
			}
			// 在将逗号转义
			tempDescription = "\"" + tempDescription + "\"";
		}
		return tempDescription;
	}

2,处理后代码

public static void exportList(String[] headers, String[] columns, List dtos, String sheetName, HttpServletResponse response) throws Exception {
		List header = Arrays.asList(headers);
		List fids = Arrays.asList(columns);
		List list = genDtoExportData(dtos, fids);
		File file = File.createTempFile("export", ".csv");

		StringBuffer sb = new StringBuffer("");
		for (String h : headers) {
			sb.append(transfer(h)).append(",");
		}
		sb.append("\r\n");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (Object obj : list) {
			for (Object obj2 : (List) obj) {
				if (obj2 instanceof Date) {
					sb.append(sdf.format((Date) obj2)).append(",");
				} else {
					sb.append(transfer(obj2.toString())).append(",");
				}
			}
			sb.append("\r\n");
		}
		OutputStream out = null;
		OutputStreamWriter writer = null;
		try {
			out = new FileOutputStream(file);
			writer = new OutputStreamWriter(out, "UTF-8");
			writer.write(new String(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF }));  
			writer.append(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(writer != null){
				writer.close();
			}
			if(out != null){
				out.close();
			}
		}

		response.setContentType("text/csv; charset=\"utf-8\"");
		response.setHeader("Content-disposition", "attachment; filename="+transferFilename(sheetName)+".csv");
		byte[] buffer = new byte[1024];
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		try {
			fis = new FileInputStream(file);
			bis = new BufferedInputStream(fis);
			OutputStream os = response.getOutputStream();
			int i = bis.read(buffer);
			while (i != -1) {
				os.write(buffer, 0, i);
				i = bis.read(buffer);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bis != null)
				bis.close();
			if (fis != null)
				fis.close();
			file.delete();
		}
	}
	/** 处理双引号和逗号的特殊转译 */
	public static String transfer(String str) {
		String tempDescription = str;
		// 如果有逗号
		if (str.contains(",")) {
			// 如果还有双引号,先将双引号转义,避免两边加了双引号后转义错误
			if (str.contains("\"")) {
				tempDescription = str.replace("\"", "\"\"");
			}
			// 在将逗号转义
			tempDescription = "\"" + tempDescription + "\"";
		}
		return tempDescription;
	}

看不出来改的哪里,没关系,下面这个是对比图
在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值