zoukankan      html  css  js  c++  java
  • CSV导入

     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>
  • 相关阅读:
    Intellij IDEA13 创建多模块Maven项目
    oracle锁
    oracle rac负载均衡
    awk命令
    政务外网、政务专网、政务内网和互联网
    图片切换实现选中-未选中效果
    生成带logo 的二维码
    控制input为number时样式
    移动端适配的解决方法?
    input-checkbox选中及非选中样式设置
  • 原文地址:https://www.cnblogs.com/superJF/p/6110286.html
Copyright © 2011-2022 走看看