900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 解决导出CSV文件乱码的问题

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

时间:2023-09-26 23:07:36

相关推荐

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

看不出来改的哪里,没关系,下面这个是对比图

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。