1 import java.io.FileReader;
 2 import java.io.IOException;
 3 import java.util.List;
 4 
 5 import au.com.bytecode.opencsv.CSVReader;
 6 import au.com.bytecode.opencsv.bean.ColumnPositionMappingStrategy;
 7 import au.com.bytecode.opencsv.bean.CsvToBean;
 8 import com.jeffy.bean.Indexmanage;
 9 /**
10  * CSV公用类
11  * @author Jeffy
12  *
13  */
14 public class CsvManage {
15 
16     /**
17      * 读取csv
18      * @param path CSV文件路径
19      * @param columns bean属性对应的表头顺序
20      *             注:String数组的内容是bean的属性名,顺序是CSV中的列的顺序
21      *             例:new String[] {"","indexmanageCoding","indexmanageLevel","index_Father_Coding","indexName"}
22      * @param className 要返回的结果类型
23      * @return 结果list
24      * @throws IOException
25      */
26     public static List getDataFromCsv(String path, String[] columns, Class<? extends Object> className) throws IOException{
27         CSVReader reader = new CSVReader(new FileReader(path));
28 
29         ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
30         strat.setType(className);
31         strat.setColumnMapping(columns);
32 
33         CsvToBean csv = new CsvToBean();
34         List list = csv.parse(strat, reader);
35         list.remove(0);
36         return list;
37     }
38     public static void main(String[] args) throws IOException {
39         String[] columns = new String[] {"indexmanageCoding","indexmanageLevel","index_Father_Coding","indexName"}; // the fields to bind do in your JavaBean
40         getDataFromCsv("d:\\test3.csv", columns, Indexmanage.class);
41     }
42 }
CSV通用类

maven依赖

<dependency>
    <groupId>net.sf.opencsv</groupId>
    <artifactId>opencsv</artifactId>
    <version>2.3</version>
</dependency>