解决导出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;
	}

看不出来改的哪里,没关系,下面这个是对比图
解决导出CSV文件乱码的问题_第1张图片

你可能感兴趣的:(日常开发记录)