java操作csv

package com.mark.csv
/**
* 读取CSV文件
*/
public void readeCsv(){
try {

ArrayList<String[]> csvList = new ArrayList<String[]>(); //用来保存数据
String csvFilePath = "c:/test.csv";
CsvReader reader = new CsvReader(csvFilePath,',',Charset.forName("SJIS")); //一般用这编码读就可以了

reader.readHeaders(); // 跳过表头 如果需要表头的话,不要写这句。

while(reader.readRecord()){ //逐行读入除表头的数据
csvList.add(reader.getValues());
}
reader.close();

for(int row=0;row<csvList.size();row++){

String cell = csvList.get(row)[0]; //取得第row行第0列的数据
System.out.println(cell);

}


}catch(Exception ex){
System.out.println(ex);
}
}

/**
* 写入CSV文件
*/
public void writeCsv(){
try {

String csvFilePath = "c:/test.csv";
CsvWriter wr =new CsvWriter(csvFilePath,',',Charset.forName("SJIS"));
String[] contents = {"aaaaa","bbbbb","cccccc","ddddddddd"};
wr.writeRecord(contents);
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
}

###################################3
java操作csv文件(2008-08-27 10:32:35)转载标签: java读取操作excelcsv格式it 分类: JAVA
这里写一写用java怎么读取excel中csv格式的文件。下面是一个例子。


//JAVA 操作 excel 中的 .csv文件格式
public class CsvUtil {

private String filename = null;
private BufferedReader bufferedreader = null;
private List list =new ArrayList();
public CsvUtil() { }

public CsvUtil(String filename) throws IOException{
this.filename = filename;
bufferedreader = new BufferedReader(new FileReader(filename));
String stemp;
while((stemp = bufferedreader.readLine()) != null){
list.add(stemp);
}
}

public List getList() throws IOException {
return list;
}

//得到csv文件的行数
public int getRowNum(){
return list.size();
}
//得到csv文件的列数
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;
}
}

//取得指定行的值

public String getRow(int index) {
if (this.list.size() != 0)
return (String) list.get(index);
else
return null;
}
//取得指定列的值
public String getCol(int index){
if (this.getColNum() == 0){
return null;
}
StringBuffer scol = new StringBuffer();
String temp = null;
int colnum = this.getColNum();
if (colnum > 1){
for (Iterator it = list.iterator(); it.hasNext();) {
temp = it.next().toString();
scol = scol.append(temp.split(",")[index] + ",");
}
}else{
for (Iterator it = list.iterator(); it.hasNext();) {
temp = it.next().toString();
scol = scol.append(temp + ",");
}
}
String str=new String(scol.toString());
str = str.substring(0, str.length() - 1);
return str;
}
//取得指定行,指定列的值
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 IOException {
this.bufferedreader.close();
}

public void run(String filename) throws IOException {
CsvUtil cu = new CsvUtil(filename);
for(int i=0;i<cu.getRowNum();i++){

String name = cu.getString(i,0);//得到第i行.第一列的数据.
String email = cu.getString(i,1);//得到第i行.第二列的数据.
String tel = cu.getString(i,2);
String number = cu.getString(i,3);

System.out.println("===name:"+name);
System.out.println("===email:"+email);
System.out.println("===tel:"+tel);
System.out.println("===number:"+number);
System.out.println(" ");
}
cu.CsvClose();
}

public static void main(String[] args) throws IOException {
CsvUtil test = new CsvUtil();
test.run("D:/alpha/abc.csv");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值