今天从网上找了一个读写csv格式的开源程序,还挺好用的。
下面是一个读取例子:
源文件格式:
ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued
1,Chai,1,1,10 boxes x 20 bags,18,39,0,10,FALSE
2,Chang,1,1,24 - 12 oz bottles,19,17,40,25,FALSE
下面读取程序
CsvReader reader
=
new
CsvReader(
"
products.csv
"
);
reader.readHeaders();
while
(reader.readRecord())
{
String productID
=
reader.get(
"
ProductID
"
);
String productName
=
reader.get(
"
ProductName
"
);
String supplierID
=
reader.get(
"
SupplierID
"
);
String categoryID
=
reader.get(
"
CategoryID
"
);
String quantityPerUnit
=
reader.get(
"
QuantityPerUnit
"
);
String unitPrice
=
reader.get(
"
UnitPrice
"
);
String unitsInStock
=
reader.get(
"
UnitsInStock
"
);
String unitsOnOrder
=
reader.get(
"
UnitsOnOrder
"
);
String reorderLevel
=
reader.get(
"
ReorderLevel
"
);
String discontinued
=
reader.get(
"
Discontinued
"
);
//
perform program logic here
}
reader.close();
写CSV例子:
CsvWriter writer = new CsvWriter(new FileWriter(new File("c:\\1.csv")),',');
writer.write("aa");
writer.write("bb");
writer.write("cc");
writer.endRecord();
writer.write("1");
writer.write("2");
writer.write("3");
writer.close();
posted on 2006-11-07 12:05
jspark 阅读(6551)
评论(0) 编辑 收藏