控制器例子:
一. 解析excel內容插入到資料庫
vm:注意:解析按鈕必須是標籤,如果換成button,用公司的表單提交則會出錯:提示不是multipartfile請求.
js:$(『#form_file』).bupform(
url:』xx.do』,
datatype:』json』,
onsubmit:function(),//return false則會阻止表單提交
success:function(data){
//成功後的**函式
controller:
處理請求的方法引數用(multipartfile uploadfile)型別接收
解析excel內容步驟:
通過傳過來的uploadfile拿到流:
a) imputstream is = uploadfile.getinputstraam();
建立poisysteam:
fs = new poifsfilesystem(is);
建立workbook
a) wb = new hssfworkbook(fs);
1. sheet = wb.getsheetat(0);//拿到excel的第乙個sheet
2. 得到總行數:int rownum = sheet.getlastrownum();
3. 得到excel的某一行
a) hssfrow row = sheet.getrow(0);//得到第0行 也就是excel的標題
得到一行裡的某個單元格:
a) hssfcell cell = row.getcell(0);
b) 通過hssfcell 單元格型別拿到單元格的資料
獲取單元格的type: type = cell.getcelltype();
hssfcell.cell_type_formul
數字型別:cell.getnumericcellvalue();
hssfcell.cell_type_string:型別cell.getrichstringcellvalue();
hssfcell.cell_type_formula:型別
if(hssfdateutil.iscelldateformatted(cell)){
date date = cell.getdatecellvalue();
身份證號避免科學計數法:
decimalformat df = new decimalformat("0");
cellvalue = df.format(cell.getnumericcellvalue());
如果cell==null 的話,就給乙個』』空字串給他
7. 每一行的資料,可以自己定義乙個類來儲存,類中的字段於行裡面的每一列對應
也可以用乙個map-à 《行號,行裡面的內容》
方式一:
方式二:
vm:controller:
處理請求的方法:
a) 設定檔案的mime型別
response.setcontenttype(getservletcontext().getmimetype(filename));
b) 拿到絕對路徑
string fullfilename = 「/res/excel/」+filename;
c) response.setheader("content-disposition", "attachment; filename="+ new string(filename.getbytes("gbk"), "iso8859-1"));
d) inputstream in = new fileinputstream(fullfilename);
e) bufferedinputstream bis = new bufferedinputstream(in);
f) bufferedoutputstreamout=new bufferedoutputstream(response.getoutputstream());
int b;
while(b=(bis.read()!=-1){
out.write(b);
in.close();
out.flush();
out.close();
PHP匯入匯出Excel方法小結
最近因專案需要,需要開發乙個模組,把系統中的一些資料匯出成excel 修改後再導回系統。就趁機對這個研究了一番,下面進行一些 總結 基本上匯出的檔案分為兩種 1 類excel格式,這個其實不是傳統意義上的excel檔案,只是因為excel的相容能力強,能夠正確開啟而已。修改這種檔案後再儲存,通常會提...
前端匯入Excel檔案並解析例項
js var wb 讀取完成的資料 var rabs true 是否將檔案讀取為二進位制字串 function importf obj var f obj.files 0 var reader new filereader reader.onload function e else 是獲取sheet...
poi解析excel匯入MySQL資料庫
ssh框架下利用poi解析excel檔案後匯入資料庫到相應的字段,列出遇到的問題,貼出解決辦法,網上的 雖然是遍地開發,但最開始的時候確實給了我不少的啟示,良莠不齊,要花大量的的時間來甄選,比較苦惱,另外得根據自己的實際情況作出符合的調整,才能實現,才能為我所用,有一些 雖然可以實現最後的目的,但是...