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");
}
}
/**
* 读取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");
}
}