CSV文件解析工具

时间:2021-04-05 08:24:44
package com.common.util;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* excel与csv解析
*
* @author Administrator
*
*/
public class ExcelAndCsvRead {
private InputStreamReader fr = null;
private BufferedReader br = null; public ExcelAndCsvRead(String f) throws IOException {
fr = new InputStreamReader(new FileInputStream(f));
}
/**
* 解析csv文件 到一个list中 每个单元个为一个String类型记录,每一行为一个list。 再将所有的行放到一个总list中
*/
public List<List<String>> readCSVFile() throws IOException {
br = new BufferedReader(fr);
String rec = null;// 一行
String str;// 一个单元格
List<List<String>> listFile = new ArrayList<List<String>>();
try {
// 读取一行
while ((rec = br.readLine()) != null) {
Pattern pCells = Pattern
.compile("(\"[^\"]*(\"{2})*[^\"]*\")*[^,]*,*");
Matcher mCells = pCells.matcher(rec);
List<String> cells = new ArrayList<String>();// 每行记录一个list
// 读取每个单元格
while (mCells.find()) {
str = mCells.group();
str = str.replaceAll(
"(?sm)\"?([^\"]*(\"{2})*[^\"]*)\"?.*,", "$1");
str = str.replaceAll("(?sm)(\"(\"))", "$2");
cells.add(str);
}
listFile.add(cells);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fr != null) {
fr.close();
}
if (br != null) {
br.close();
}
}
return listFile;
}

    //测试
public static void main(String[] args) throws Throwable {
ExcelAndCsvRead test = new ExcelAndCsvRead("C://Users//Administrator//AppData//Local//Temp//moban.csv"); //传入需要解析的文件路径
List<List<String>> csvList = test.readCSVFile();
System.out.println(csvList.get(0)); //解析文件的title
System.out.println(csvList.get(1)); //文件的第一行数据
System.out.println(csvList.get(2));//文件的第二行数据
}
}