JAVA以UTF-8导出CSV文件,用excel打开产生乱码的解决方法

参考博客:https://blog.csdn.net/youzhouliu/article/details/52038889
  本次在项目中遇到下载excel文件,用excel打开,是乱码,用notepad或者editplus打开是正常的,代码如下:
此为csv文件下载工具类示例:
public static final String DBS_INSTRUCTION_FILE_SEPERATOR = “,”;
public class DownloadCSVUtil {
   public Response downloadCSV(String fileName, List titleList, List columnList) {
    DownloadProvider provider = new DownloadProvider();
    provider.download(buildDownLoadContent(titleList, columnList), ConstantsHelper.CHARSET_UTF8);
     return Response.ok(provider, MediaType.APPLICATION_OCTET_STREAM_TYPE).header(
        “Content-Disposition”, String.format(“attachment; filename=%s.csv”, fileName)).build();
     }
  private String buildDownLoadContent(List titleList, List columnList) {
    StringBuilder sb = new StringBuilder();
    // build title
    for (String title : titleList) {
       sb.append(title);
       sb.append(ConstantsHelper.DBS_INSTRUCTION_FILE_SEPERATOR);
     }
    sb.deleteCharAt(sb.lastIndexOf(ConstantsHelper.DBS_INSTRUCTION_FILE_SEPERATOR));
    sb.append("\r\n");
     //build content
    int n = titleList.size();
    int j = columnList.size() / n;
    for (int i = 0; i < j; i++) {
      for (int m = 0; m < n; m++) {
        sb.append(StringUtils.isBlank(columnList.get(m + i * n)) ? “” : columnList.get(m + i * n));
        sb.append(ConstantsHelper.DBS_INSTRUCTION_FILE_SEPERATOR);
       }
      sb.deleteCharAt(sb.lastIndexOf(ConstantsHelper.DBS_INSTRUCTION_FILE_SEPERATOR));
      sb.append("\r\n");
     }
    return sb.toString();
  }
}


public class DownloadProvider implements StreamingOutput {
   private InputStream is;
   private byte[] buffer = null;
   private File file = null;
   public DownloadProvider() {
      }
   public DownloadProvider(InputStream inputStream) {
     this.is = inputStream;
     }
  public void write(OutputStream output) throws IOException {
    InputStream fileInputStream = getInputStream();
    if (fileInputStream == null) return;
    try {
       // add BOM 此处解决csv文件,excel打开乱码
       output.write(new byte[] { (byte) 0xEF, (byte) 0xBB,(byte) 0xBF});
       int length;
       byte[] buffer = new byte[1000];
       while ((length = fileInputStream.read(buffer)) != -1) {
       output.write(buffer, 0, length);
       }
       output.flush();
       output.close();
    } finally {
       fileInputStream.close();
    }
  }
  private InputStream getInputStream() throws IOException {
    if (is == null) {
      if (this.buffer != null) {
        is = new ByteArrayInputStream(this.buffer);
      } else if (this.file != null) {
        is = new FileInputStream(this.file);
      }
    }
    return is;
  }
  public void download(String htmlExcel) {
    try {
        this.buffer = htmlExcel.getBytes(“UTF-8”);
      } catch (UnsupportedEncodingException e) {
        Logger.warn(this, String.format(“The htmlExcel is [%s]”, htmlExcel), e);
     }
   }
  public void download(String htmlExcel,String charset) {
     try {
         this.buffer = htmlExcel.getBytes(charset);
     } catch (UnsupportedEncodingException e) {
        Logger.warn(this, String.format(“The htmlExcel is [%s]”, htmlExcel), e);
      }
   }
}

此为csv文件解析示例:
public class CSVService {
  public List loadDataFromCVS(InputStream is) throws IOException {
     return new CSVReader(new InputStreamReader(is)).readAll();
    }
  public List loadDataFromCVS(File file) throws IOException {
    FileInputStream fis = new FileInputStream(file);
     try {
         return loadDataFromCVS(fis);
     } finally {
         IOUtils.closeQuietly(fis);
     }
    }
  public List loadDataFromCVS(String fileName) throws IOException {
     return loadDataFromCVS(new File(fileName));
   }
  public List loadDataFromCVS(String fileName, String charsetName) throws IOException {
     return loadDataFromCVS(new File(fileName), charsetName);
   }
  public List loadDataFromCVS(File file, String charsetName) throws IOException {
     FileInputStream fis = new FileInputStream(file);
     try {
        return loadDataFromCVS(fis, charsetName);
     } finally {
        IOUtils.closeQuietly(fis);
     }
    }
  public List loadDataFromCVS(InputStream is, String charsetName) throws IOException {
      return new CSVReader(new InputStreamReader(is, charsetName)).readAll();
    }

   /**
   * 指定字段分割符
   * */
   public List loadDataFromCSVWithSeparator(InputStream is, String charsetName, int skipLines,
     char separator) throws IOException {
     return new CSVReader(new InputStreamReader(is, charsetName),separator, CSVParser.DEFAULT_QUOTE_CHARACTER,
        CSVParser.DEFAULT_ESCAPE_CHARACTER, skipLines).readAll();
    }
 }

你可能感兴趣的:(java,csv,csv下载)