1. 程式人生 > >jFinal中excel匯入匯出

jFinal中excel匯入匯出

jFinal中excel匯入匯出

Jfinal剛接觸不久,之前接觸的java框架基本上是jeesite,所以,當接到excel匯入及模板下載任務時,第一個想到的還是jeesiteApache POI 3.9的簡單封裝,實現Excel的匯入匯出功能(只適用於簡單的匯入匯出,不合適複雜的表格或使用模板進行匯入匯出),

具體參考:http://blog.csdn.net/layman1024/article/details/71190927

一、實現匯入匯出

先將jeesite中的六個檔案複製到本系統

 

而後將實體類中注入註解

 

@ExcelField(title="型別名稱

", align=2, sort=30)

Title是匯出欄位標題

Align是匯出欄位對齊方式(0:自動;1:靠左;2:居中;3:靠右)

Sort是匯出欄位排序(升序)

Controller中直接呼叫即可

 

 

具體程式碼可在碼雲中查詢:點選開啟連結

二、遇到的問題

1java.lang.IllegalStateException: Could not auto-size column. Make sure the column was tracked prior to auto-sizing the column.

 

檔案下載時遇到的,原因是無法自動追蹤所有的列。

解決如下:

  1.之前使用的是sheet介面,改為SXSSFSheet

  2.autoSizeColumn前使用sheet.trackAllColumnsForAutoSizing();,手動設定。

2、com.jfinal.plugin.activerecord.ActiveRecordException: The attribute name does not exist: xxx

主要是表中沒有這個欄位,所以在寫的時候,不要寫get set方法的時候,使用return get(‘xxx’)set(‘xxx’, xxx)這兩個方法,而是使用return xxx

this.xxx=xxx這種,就可以解決。


另注:下載時,前臺不能使用ajaxContent方法,後臺返回時,可使用renderNull(),或renderFile()。