POI匯出匯入Excel的方法,拿去即可用

2021-10-08 19:53:56 字數 2887 閱讀 3836

/**

* * @param request

* @param response

* @param titles 表頭行

* @param datas 資料

* @param filename 檔名

//寫入資料

int a=1;

for(

int j=

0;jsize()

;j++

)else

if(i==1)

}}//匯出資料

trycatch

(exception e)

finally

if(wb!=null)

}}

匯出的時候,如果資料的型別不是string字串,那麼我們最好將其轉換成string型別

if

(valueobject instanceof

string

)else

if(valueobject instanceof

integer

)else

if(valueobject instanceof

bigdecimal

)else

public

void

inputexcel

(multipartfile file)

throws exception

//獲取檔名

string originalfilename = file.

getoriginalfilename()

;//獲取字尾

string filetype = originalfilename.

substring

(originalfilename.

lastindexof

(".")+

1, originalfilename.

length()

);//獲取輸入流

inputstream inputstream = file.

getinputstream()

; workbook wb = null;

//excel資料

list

datalist=

newarraylist

<

>()

;try

else

if(xlsx.

equals

(filetype)

)else

// 解析sheet

for(

int i =

0; i < wb.

getnumberofsheets()

; i++)if

(row.

getfirstcellnum()

<0)

exceldata data=

newexceldata()

;//獲取總列數

int cellcount = row.

getphysicalnumberofcells()

;// 解析sheet 的列

for(

int k =

0; k < cellcount; k++

) datalist.

add(data);}

}}catch

(exception e)

finally

if(wb!=null)

} datalist.

stream()

.foreach

(x-> system.out.

println

(x));}

private exceldata getcellvalue

(exceldata data,

int num, cell cell)

else

}break

;case cell.cell_type_boolean:

//布林型

cellvalue = string.

valueof

(cell.

getbooleancellvalue()

);break

;case cell.cell_type_blank:

//空白

cellvalue = cell.

getstringcellvalue()

;break

;case cell.cell_type_error:

//錯誤

cellvalue =

"錯誤"

;break

;case cell.cell_type_formula:

//公式

cellvalue =

"錯誤"

;break

;default

: cellvalue =

"錯誤";}

cellvalue = cellvalue.

trim()

;if(num==0)

else

if(num==1)

return data;

}

poi匯入匯出excel

org.apache.poigroupid poiartifactid 3.14 beta1version dependency public private string headtextname 列頭 標題 名 private string propertyname 對應欄位名 private ...

POI匯入匯出Excel

public class excelutils sheet sheet null row row null cell cell null list new arraylist 遍歷excel中所有的sheet for int i 0 i wb.getnumberofsheets i 遍歷當前shee...

POI 匯入 匯出Excel

使用form表單 enctype multipart form data 上傳excel檔案,後台接收multipartfile檔案格式。讀取excel private static final string excel xls xls private static final string exc...