packageutils;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileOutputStream;importjava.io.FileReader;importjava.io.IOException;importjava.io.OutputStreamWriter;importjava.util.ArrayList;importjava.util.LinkedHashMap;importjava.util.List;importjava.util.Map;importorg.apache.commons.csv.CSVFormat;importorg.apache.commons.csv.CSVPrinter;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;/*** csv导入导出工具类
*@authorkpzc
* @date 2018年12月29日 下午3:45:00
* @desc*/
public classCSVUtils {//private static final Logger logger = LoggerFactory.getLogger(CSVUtils.class);
/*** io流导出
*@authorkpzc
* @date 2018年12月29日 下午3:48:34
*@paramfile csv文件(路径+文件名),csv文件不存在会自动创建
*@paramdataList 数据,字符串用逗号分隔
*@return返回导出是否成功 true成功 false失败*/
public static boolean exportCsv(File file, ListdataList){boolean isSucess=false;
FileOutputStream out=null;
OutputStreamWriter osw=null;
BufferedWriter bw=null;try{
out= newFileOutputStream(file);
osw= new OutputStreamWriter(out, "GBK");//解决FileOutputStream中文乱码问题 解决MS office乱码问题
bw =newBufferedWriter(osw);if(dataList!=null && !dataList.isEmpty()){for(String data : dataList){
bw.append(data).append("\r");
}
}
isSucess=true;
}catch(Exception e) {
isSucess=false;
}finally{if(bw!=null){try{
bw.close();
bw=null;
}catch(IOException e) {
e.printStackTrace();
}
}if(osw!=null){try{
osw.close();
osw=null;
}catch(IOException e) {
e.printStackTrace();
}
}if(out!=null){try{
out.close();
out=null;
}catch(IOException e) {
e.printStackTrace();
}
}
}returnisSucess;
}/*** 导入
*@authorkpzc
* @date 2018年12月29日 下午3:48:11
*@paramfile csv文件(路径+文件)
*@return返回List列表*/
public static ListimportCsv(File file){
List dataList=new ArrayList();
BufferedReader br=null;try{
br= new BufferedReader(newFileReader(file));
String line= "";while ((line = br.readLine()) != null) {
dataList.add(line);
}
}catch(Exception e) {
e.printStackTrace();
}finally{if(br!=null){try{
br.close();
br=null;
}catch(IOException e) {
e.printStackTrace();
}
}
}returndataList;
}/*** apache commons-csv导出
* 注意jdk要在1.7及以上使用
* map的数据个数要与header的个数相等 并且一一对应,可参照main方法
*@authorkpzc
* @date 2019年1月4日 上午10:12:20
*@paramfilePath 文件存储路径
*@paramlist 数据列表
*@paramheader 表头*/
public static void write(String filePath,List>list,String... header) {try{
FileOutputStream fos= newFileOutputStream(filePath);
OutputStreamWriter osw= new OutputStreamWriter(fos, "GBK");
CSVFormat csvFormat=CSVFormat.DEFAULT.withHeader(header);
CSVPrinter csvPrinter= newCSVPrinter(osw, csvFormat);//跟上面两行代码是一样的//CSVPrinter csvPrinter = CSVFormat.DEFAULT.withHeader(header).print(osw);
for (Mapmap : list) {
csvPrinter.printRecord(map.values());
}
csvPrinter.flush();
csvPrinter.close();
}catch(IOException e) {
e.printStackTrace();
}
}public static voidmain(String[] args) {//io流导出
File file=new File("E:/aa.csv");
List dataList=new ArrayList();
dataList.add("1,2,3,'/N',4");
dataList.add("1,2,3,'/N',4");
exportCsv(file, dataList);//apache commons-csv导出
String filePath = "E://aaa.csv";
String header1= "姓名";
String header2= "性别";
String header3= "编号";
String header4= "描述";
List> recordList = new ArrayList>();for (int i = 0; i < 5; i++) {
LinkedHashMap map = new LinkedHashMap();
map.put("name", "zhangsan");
map.put("sex", "男");
map.put("code", "001");
map.put("aa", "aaa");
recordList.add(map);
}
write(filePath, recordList,header1,header2,header3,header4);
}
}