publicclassCsvUtil {
privateString fileName =null;
privateBufferedReader br =null;
privateList list =newArrayList();
publicCsvUtil() {
}
publicCsvUtil(String fileName)throwsException {
this.fileName = fileName;
br = newBufferedReader(newFileReader(fileName));
String stemp;
while((stemp = br.readLine()) !=null) {
list.add(stemp);
}
}
publicList getList() {
returnlist;
}
/**
* 获取行数
* @return
*/
publicintgetRowNum() {
returnlist.size();
}
/**
* 获取列数
* @return
*/
publicintgetColNum() {
if(!list.toString().equals("[]")) {
if(list.get(0).toString().contains(",")) {// csv为逗号分隔文件
returnlist.get(0).toString().split(",").length;
} elseif(list.get(0).toString().trim().length() !=0) {
return1;
} else{
return0;
}
} else{
return0;
}
}
/**
* 获取制定行
* @param index
* @return
*/
publicString getRow(intindex) {
if(this.list.size() !=0) {
return(String) list.get(index);
} else{
returnnull;
}
}
/**
* 获取指定列
* @param index
* @return
*/
publicString getCol(intindex) {
if(this.getColNum() ==0) {
returnnull;
}
StringBuffer sb = newStringBuffer();
String tmp = null;
intcolnum =this.getColNum();
if(colnum >1) {
for(Iterator it = list.iterator(); it.hasNext();) {
tmp = it.next().toString();
sb = sb.append(tmp.split(",")[index] +",");
}
} else{
for(Iterator it = list.iterator(); it.hasNext();) {
tmp = it.next().toString();
sb = sb.append(tmp + ",");
}
}
String str = newString(sb.toString());
str = str.substring(0, str.length() -1);
returnstr;
}
/**
* 获取某个单元格
* @param row
* @param col
* @return
*/
publicString getString(introw,intcol) {
String temp = null;
intcolnum =this.getColNum();
if(colnum >1) {
temp = list.get(row).toString().split(",")[col];
} elseif(colnum ==1){
temp = list.get(row).toString();
} else{
temp = null;
}
returntemp;
}
publicvoidCsvClose()throwsException{
this.br.close();
}
/**
*去表头
**/
publicString removehead(String str){
String[] str_1=str.split(",");
String sb=newString();
for(inti=1;i
sb=sb+str_1[i]+",";
}
returnsb;
}
}
public class CsvUtil {
private String fileName = null;
private BufferedReader br = null;
private List list = new ArrayList();
public CsvUtil() {
}
public CsvUtil(String fileName) throws Exception {
this.fileName = fileName;
br = new BufferedReader(new FileReader(fileName));
String stemp;
while ((stemp = br.readLine()) != null) {
list.add(stemp);
}
}
public List getList() {
return list;
}
/**
* 获取行数
* @return
*/
public int getRowNum() {
return list.size();
}
/**
* 获取列数
* @return
*/
public int getColNum() {
if (!list.toString().equals("[]")) {
if (list.get(0).toString().contains(",")) {// csv为逗号分隔文件
return list.get(0).toString().split(",").length;
} else if (list.get(0).toString().trim().length() != 0) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
}
/**
* 获取制定行
* @param index
* @return
*/
public String getRow(int index) {
if (this.list.size() != 0) {
return (String) list.get(index);
} else {
return null;
}
}
/**
* 获取指定列
* @param index
* @return
*/
public String getCol(int index) {
if (this.getColNum() == 0) {
return null;
}
StringBuffer sb = new StringBuffer();
String tmp = null;
int colnum = this.getColNum();
if (colnum > 1) {
for (Iterator it = list.iterator(); it.hasNext();) {
tmp = it.next().toString();
sb = sb.append(tmp.split(",")[index] + ",");
}
} else {
for (Iterator it = list.iterator(); it.hasNext();) {
tmp = it.next().toString();
sb = sb.append(tmp + ",");
}
}
String str = new String(sb.toString());
str = str.substring(0, str.length() - 1);
return str;
}
/**
* 获取某个单元格
* @param row
* @param col
* @return
*/
public String getString(int row, int col) {
String temp = null;
int colnum = this.getColNum();
if (colnum > 1) {
temp = list.get(row).toString().split(",")[col];
} else if(colnum == 1){
temp = list.get(row).toString();
} else {
temp = null;
}
return temp;
}
public void CsvClose()throws Exception{
this.br.close();
}
/**
*去表头
**/
public String removehead(String str){
String[] str_1=str.split(",");
String sb=new String();
for(int i=1;i
sb=sb+str_1[i]+",";
}
return sb;
}
}
publicJSONArray readcsv(String path){
JSONArray array=newJSONArray();
CsvUtil util;
try{
util = newCsvUtil(path);
introw=util.getRowNum();
intcol=util.getColNum();
for(inti=0;i
JSONObject jsonobject=newJSONObject();
String value=util.getCol(i);
jsonobject.put(util.getString(0, i),util.removehead(value));
array.add(jsonobject);
}
} catch(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
returnarray;
}
public JSONArray readcsv(String path){
JSONArray array=new JSONArray();
CsvUtil util;
try {
util = new CsvUtil(path);
int row=util.getRowNum();
int col=util.getColNum();
for(int i=0;i
JSONObject jsonobject=new JSONObject();
String value=util.getCol(i);
jsonobject.put(util.getString(0, i),util.removehead(value));
array.add(jsonobject);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return array;
} 将其表头设为key 其他内容设为value