javascv 和 opencsv

opencsv

http://opencsv.sourceforge.net/

示例代码:


import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;

public class ProcessCSV {
private static final String ADDRESS_FILE="D://examples/addresses.csv";

public static void main(String[] args) throws IOException {
CSVWriter writer = new CSVWriter(new FileWriter(ADDRESS_FILE));
List<String[]> allElements = new ArrayList<String[]>();
allElements.add(new String[]{"张三","地址1","邮箱1"});
allElements.add(new String[]{"李四","地址2","邮箱2"});

writer.writeAll(allElements);
writer.close();
System.out.println("\nGenerated CSV File:" + ADDRESS_FILE);

CSVReader reader = new CSVReader(new FileReader(ADDRESS_FILE));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
System.out.println("Name: ["+nextLine[0]+"]\nAddress: ["+nextLine[1]+"]\nEmail: ["+nextLine[2]+"]");
}
}
}


=======================================================


javacsv.jar下载地址:

http://sourceforge.net/project/showfiles.php?group_id=33066


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();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值