OpenCSV 實現CSV的匯入匯出

2021-10-18 23:19:23 字數 2596 閱讀 4677

本文將介紹以下內容

//***********************************====position******************************====//

//通過position形式

//實體內的註解,基於position

@csvbindbyposition

(position =0)

@excel

(iswrap =

false

,name =

"樣例"

,ordernum =

"1",width =30)

private string demo;

public

static

list

readdatafromlocalpathwithposition

(class

clazz, string filepath)

catch

(filenotfoundexception e)}if

(collectionutils.

isnotempty

(parseresult)

)return collections.

emptylist()

;}//***********************************====header******************************====//

@csvcustombindbyname

(column =

"num"

,converter = convertcsvstringtolong.

class

)@excel

(iswrap =

false

,name =

"人數"

,ordernum =

"4",width =20)

private long num;

//convertcsvstringtolong是自定義的型別轉換,僅需要繼承abstractbeanfield即可,自行實現型別的轉換

public

class

convertcsvstringtolong

extends

abstractbeanfield

}//以下讀取時指定編碼utf-8,建議讀取和寫入的時候統一指定編碼,避免出現中文亂碼

public

static

list

readdatafromlocalpathwithheader

(class

clazz, string filepath)

catch

(filenotfoundexception e)

finally

if(fin != null)

}catch

(exception e)}}

if(collectionutils.

isnotempty

(parseresult)

)return collections.

emptylist()

;}

public

static

void

writedatatocsvfile

(list

datalist,class

clazz, string finalpath)))

;// 設定顯示的順序

new()

;settype

(clazz)

;// 寫表頭

csvwriter csvwriter =

newcsvwriter

(writer)

; statefulbeantocsv

beantocsv =

newstatefulbeantocsvbuilder

(writer)..

withquotechar

(csvwriter.no_quote_character)

.withseparator

(default_biz_seperator)

.withescapechar

('\\').

build()

; beantocsv.

write

(datalist)

; csvwriter.

close()

; writer.

close()

;}catch

(ioexception

| csvdatatypemismatchexception | csvrequiredfieldemptyexception e)

log.

info

("[csvutils][readmodeldatafromlocalpathwithheader]write to csv file successfully ,local path is "

+finalpath)

;}

csv檔案處理——opencsv

mongodb匯入csv檔案

使用命令 mongoimport 引數 h 主機ip 埠 d 要匯入的db c 要匯入的collections f 指定字段 type 型別 file 匯入的檔案路徑 演示如下 root localhost bin mongoimport h 192.168.1.151 27017 d logs c...

Mysql 匯入csv檔案

mysql load data infile命令可以把csv平面檔案中的資料匯入到資料庫中。linux下 load data infile home test dump ip location.csv into table ip location character set utf8 fields ...

Solr匯入CSV檔案

首先得準備好乙份csv檔案,這份檔案可以是手動生成,也可以是從資料庫中匯出來的。參見postgresql匯入匯出資料 將postgres中的表匯出為csv檔案。上傳csv檔案示例,在csv檔案中,header不能加雙引號 postgres匯出時缺省會加上 比如下面的檔案匯入會出錯,id name 8...