⑴ EasyPoi的導入和導出功能
EasyPoi是一款開源的軟體,用於開發EXCEL表格的導入和導出功能,簡單易上手,代碼量也很少,非常適合初學者去使用。我們使用Java開發Excel導入導出功能,以前常用的是Poi,但是需要編寫的代碼量太多,使用EasyPoi僅需要在代碼中添加註解,便可以完成大部分普通的Excel編輯工作。
工具/材料
IntelliJ IDEA
我們是使用Maven管理項目,首先我們需要添加開發EasyPoi所依賴的jar包,如下所示。
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.1.0</version>
</dependency>
EasyPoi 可以自適應Excel的xls和xlsx兩種格式,我們今天主要講解使用註解進行導入和導出的功能,我們只要修改註解就可以修改Excel的欄位和格式。我們需要編寫實體和Excel表格的對應關系,在實體上添加註解。@Excel 註解作用到filed(列)上面,是對列的描述。@Excel註解的name屬性即為列名,format用於設置時間的格式。我們創建一個PersonEntity,實現序列化介面,並添加如下的註解,另外該類需要getter和setter方法。
我們寫一個測試類去測試導出功能,創建一個測試類,並在main方法中編寫測試代碼,我們使用easypoi工具類ExcelExportUtil 的exportExcel方法,其中new ExportParams()是標題和sheet的基本設置,當然這些設置有些不是必須的,可以根據需要自行修改。具體的導出代碼如下圖所示。
接下來我們右鍵執行該方法,此時會生成一個excel文件,如下圖所示,我們可以看到設置的標題和數據已存在該excel表格中。
接下來我們編寫導入的方法,將上面生成的excel表格導入,使用ExcelImportUtil工具類的importExcel方法,ImportParams用於設置導入參數。
接下來我們測試導入功能,在執行方法後提示創建對象異常,我們需要檢查異常的原因,此時發現我們上面創建的實體類沒有無參構造方法,因為我們已經創建了一個有參的構造,就不會幫我們自動創建無參構造了,因此我們需要手動創建,如下圖所示。
另外,我們在編寫PersonEntity實體類時,步驟2的id欄位上面沒有@Excel註解,所以該欄位不起任何作用,若不需要的話可以刪除。
特別提示
EasyPoi註解作用的實體類一定要有無參構造,若實體類中存在有參構造,一定要手動創建一個無參構造。
⑵ normalexcelconstants導出怎麼設置底部
之前使用poi導出Excel表格,需要配置很多東西,也比較麻煩,這里使用poi的封裝easypoi,可以快速配置,實現Excel或者word文件的導出。這里我們結合SpringMVC開發easypoi。
導入jar包
這里是springMVC和easypoi所需的jar包
主要是easypoi-base和easypoi-web,其它都是關聯所需的jar包,我們需要commons-lang3.jar包,開始使用commons-lang2.6版本會出現錯誤。
spring-servlet.xml配置
[html] view plain
<!-- Bean解析器,級別高於默認解析器,尋找bean對象進行二次處理 -->
<bean id="beanNameViewResolver"
class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="0">
</bean>
[html] view plain
<!-- Excel 處理 根據用戶輸入進行對象處理 -->
<bean id="jeecgExcelView" class="org.jeecgframework.poi.excel.view.JeecgSingleExcelView" />
<bean id="jeecgTemplateExcelView" class="org.jeecgframework.poi.excel.view.JeecgTemplateExcelView" />
<bean id="jeecgTemplateWordView" class="org.jeecgframework.poi.excel.view.JeecgTemplateWordView" />
<bean id="jeecgMapExcelView" class="org.jeecgframework.poi.excel.view.JeecgMapExcelView" />
controller
[java] view plain
package com.mvc.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.jeecgframework.poi.excel.entity.vo.MapExcelConstants;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping(value="/excel")
public class ExcelController {
/**
*
* 方法名:
* 開發者:
* 開發時間:2016-12-29
*/
@RequestMapping(value = "/export",method = {RequestMethod.POST,RequestMethod.GET})
public String export(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap){
//標題
List<ExcelExportEntity> entityList = new ArrayList<ExcelExportEntity>();
//內容
List<Map<String,Object>> dataResult = new ArrayList<Map<String,Object>>();
entityList.add(new ExcelExportEntity("表頭1", "table1", 15));
entityList.add(new ExcelExportEntity("表頭2", "table2", 25));
entityList.add(new ExcelExportEntity("表頭3", "table3", 35));
for (int i = 0; i < 10; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("table1", "蘋果"+i);
map.put("table2", "香蕉"+i);
map.put("table3", "鴨梨"+i);
dataResult.add(map);
}
modelMap.put(MapExcelConstants.ENTITY_LIST, entityList);
modelMap.put(MapExcelConstants.MAP_LIST, dataResult);
String fileName = "easypoi測試列表";
modelMap.put(MapExcelConstants.FILE_NAME, fileName);
modelMap.put(NormalExcelConstants.PARAMS, new ExportParams("easypoi列表", "測試列表"));
return MapExcelConstants.JEECG_MAP_EXCEL_VIEW;
}
}
配置好表頭,內容體,文件名就可以使用了
⑶ 【急】easypoi模板導出合並問題:合並後的第一行數據始終無法顯示不完整,請各位大神幫忙看下,非常感謝
你的代碼中使用了List的嵌套,但是excel模板中卻只使用了一層循環。所以你要麼把代碼中的TogetherDiagnosisVO和DiagnosisDetailVO的欄位合並,要麼模板按照循環嵌套的方式寫。
⑷ cmw500和iqxel-80wifi測試一致嗎
EasyPoi的主要特點
1.設計精巧,使用簡單
2.介面豐富,擴展簡單
3.默認值多,write less do more
4.AbstractView 支持,web導出可以簡單明了
EasyPoi的幾個入口工具類
1.ExcelExportUtil Excel導出(普通導出,模板導出)
2.ExcelImportUtil Excel導入
3.WordExportUtil Word導出(只支持docx ,doc版本poi存在圖片的bug,暫不支持)
版本修改
2.0.6-SNAPSHOT
增加map的導出
增加index 列
EasyPoi導出實例
1.註解,導入導出都是基於註解的,實體上做上註解,標示導出對象,同時可以做一些操作
@ExcelTarget("courseEntity")
public class CourseEntity implements java.io.Serializable {
/** 主鍵 */
private String id;
/** 課程名稱 */
@Excel(name = "課程名稱", orderNum = "1", needMerge = true)
private String name;
/** 老師主鍵 */
@ExcelEntity(id = "yuwen")
@ExcelVerify()
private TeacherEntity teacher;
/** 老師主鍵 */
@ExcelEntity(id = "shuxue")
private TeacherEntity shuxueteacher;
@ExcelCollection(name = "選課學生", orderNum = "4")
private List<StudentEntity> students;
2.基礎導出 傳入導出參數,導出對象,以及對象列表即可完成導出
HSSFWorkbook workbook = ExcelExportUtil.exportExcel(new ExportParams(
"2412312", "測試", "測試"), CourseEntity.class, list);
3.基礎導出,帶有索引 在到處參數設置一個值,就可以在導出列增加索引
ExportParams params = new ExportParams("2412312", "測試", "測試");
params.setAddIndex(true);
HSSFWorkbook workbook = ExcelExportUtil.exportExcel(params,
TeacherEntity.class, telist);
4.導出Map 創建類似註解的集合,即可完成Map的導出,略有麻煩
List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
entity.add(new ExcelExportEntity("姓名", "name"));
entity.add(new ExcelExportEntity("性別", "sex"));
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map;
for (int i = 0; i < 10; i++) {
map = new HashMap<String, String>();
map.put("name", "1" + i);
map.put("sex", "2" + i);
list.add(map);
}
HSSFWorkbook workbook = ExcelExportUtil.exportExcel(new ExportParams(
"測試", "測試"), entity, list);
5.模板導出 根據模板配置,完成對應導出
TemplateExportParams params = new TemplateExportParams();
params.setHeadingRows(2);
params.setHeadingStartRow(2);
Map<String,Object> map = new HashMap<String, Object>();
map.put("year", "2013");
map.put("sunCourses", list.size());
Map<String,Object> obj = new HashMap<String, Object>();
map.put("obj", obj);
obj.put("name", list.size());
params.setTemplateUrl("org/jeecgframework/poi/excel/doc/exportTemp.xls");
Workbook book = ExcelExportUtil.exportExcel(params, CourseEntity.class, list,
map);
6.導入 設置導入參數,傳入文件或者流,即可獲得相應的list
ImportParams params = new ImportParams();
params.setTitleRows(2);
params.setHeadRows(2);
//params.setSheetNum(9);
params.setNeedSave(true);
long start = new Date().getTime();
List<CourseEntity> list = ExcelImportUtil.importExcel(new File(
"d:/tt.xls"), CourseEntity.class, params);
7.和spring mvc的無縫融合 簡單幾句話,Excel導出搞定
@RequestMapping(params = "exportXls")
public String exportXls(CourseEntity course,HttpServletRequest request,HttpServletResponse response
, DataGrid dataGrid,ModelMap map) {
CriteriaQuery cq = new CriteriaQuery(CourseEntity.class, dataGrid);
org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, course, request.getParameterMap());
List<CourseEntity> courses = this.courseService.getListByCriteriaQuery(cq,false);
map.put(NormalExcelConstants.FILE_NAME,"用戶信息");
map.put(NormalExcelConstants.CLASS,CourseEntity.class);
map.put(NormalExcelConstants.PARAMS,new ExportParams("課程列表", "導出人:Jeecg",
"導出信息"));
map.put(NormalExcelConstants.DATA_LIST,courses);
return NormalExcelConstants.JEECG_EXCEL_VIEW;
}
⑸ easypoi 簡單模板導出
1.依賴
如果項目有原始poi 依賴 可以使用以下兼容版本
<properties>
<poi.version>3.15</poi.version>
<easypoi.version>3.3.0</easypoi.version>
</properties>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>${easypoi.version}</version>
</dependency>
如果項目沒有引入poi 依賴 可以直接用下面依賴
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>
2.導出方法
public static void excel() {
Map total =new HashMap<>();
List> mapList =new ArrayList<>();
for (int i =1; i <=5; i++) {
Map map =new HashMap<>();
map.put("id", i +"");
/* map.put("bin", "001 1000千克");
map.put("name", "商品" + i);
map.put("code", "goods" + i);
map.put("proDate", "2019-05-30");
map.put("recvDate", "2019-07-07");*/
mapList.add(map);
}
total.put("list", mapList);
total.put("code","code9527");
TemplateExportParams params =new TemplateExportParams("export/" +"test4.xlsx",true);
try {
Workbook workbook = ExcelExportUtil.exportExcel(params, total);
// CellRangeAddress cra = new CellRangeAddress(1, 3, 0, 0);
// workbook.getSheetAt(0).addMergedRegion(cra);
FileOutputStream fileOutputStream =new FileOutputStream("F:\\project\\工時系統資料" + File.separator +"1.xls");
workbook.write(fileOutputStream);
}catch (Exception e) {
System.out.println(e.getMessage());
}
}
3.模板
位置在resources 創建export文件夾裡面放要導出的模板
簡單模板(單行遍歷)
復雜模板多行遍歷(有單元格合並情況)
效果圖
復雜模板2
效果圖
注意點 模板指令可以參考官方文檔
空格分割
三目運算 {{test ? obj:obj2}}
n: 表示 這個cell是數值類型 {{n:}}
le: 代表長度{{le:()}} 在if/else 運用{{le:() > 8 ? obj1 : obj2}}
fd: 格式化時間 {{fd:(obj;yyyy-MM-dd)}}
fn: 格式化數字 {{fn:(obj;###.00)}}
fe: 遍歷數據,創建row
!fe: 遍歷數據不創建row
$fe: 下移插入,把當前行,下面的行全部下移.size()行,然後插入
#fe: 橫向遍歷
v_fe: 橫向遍歷值
!if: 刪除當前列 {{!if:(test)}}
單引號表示常量值 '' 比如'1' 那麼輸出的就是 1
&NULL& 空格
]] 換行符 多行遍歷導出
sum: 統計數據
如果在表格表頭下面空一行在填寫循環的指令 導出的數據也會空一行在填充數據
如果需要做統計的信息 如果循環次數可以確定 可以在下面合適行添加統計的信息如果循環次數不確定可以在表格表頭下面做統計信息展示
獲取導出數據中code 值 可以在模板合適單元格位置直接 {{code}} 即可
⑹ normalexcelconstants導出怎麼過濾掉頭部
之前使用poi導出Excel表格,需要配置很多東西,也比較麻煩,這里使用poi的封裝easypoi,可以快速配置,實現Excel或者word文件的導出。這里我們結合SpringMVC開發easypoi。導入jar包這里是springMVC和easypoi所需的jar包主要是easypoi-base和easypoi-web,其它都是關聯所需的jar包,我們需要commons-lang3.jar包,開始使用commons-lang2.6版本會出現錯誤。spring-servlet.xml配置[html]viewplain[html]viewplaincontroller[java]viewplainpackagecom.mvc.controller;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.jeecgframework.poi.excel.entity.ExportParams;importorg.jeecgframework.poi.excel.entity.params.ExcelExportEntity;importorg.jeecgframework.poi.excel.entity.vo.MapExcelConstants;importorg.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;importorg.springframework.stereotype.Controller;importorg.springframework.ui.ModelMap;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;@Controller@RequestMapping(value="/excel")publicclassExcelController{/****方法名:*開發者:*開發時間:2016-12-29*/@RequestMapping(value="/export",method={RequestMethod.POST,RequestMethod.GET})publicStringexport(HttpServletRequestrequest,HttpServletResponseresponse,ModelMapmodelMap){//標題ListentityList=newArrayList();//內容List>dataResult=newArrayList>();entityList.add(newExcelExportEntity("表頭1","table1",15));entityList.add(newExcelExportEntity("表頭2","table2",25));entityList.add(newExcelExportEntity("表頭3","table3",35));for(inti=0;imap=newHashMap();map.put("table1","蘋果"+i);map.put("table2","香蕉"+i);map.put("table3","鴨梨"+i);dataResult.add(map);}modelMap.put(MapExcelConstants.ENTITY_LIST,entityList);modelMap.put(MapExcelConstants.MAP_LIST,dataResult);StringfileName="easypoi測試列表";modelMap.put(MapExcelConstants.FILE_NAME,fileName);modelMap.put(NormalExcelConstants.PARAMS,newExportParams("easypoi列表","測試列表"));returnMapExcelConstants.JEECG_MAP_EXCEL_VIEW;}}配置好表頭,內容體,文件名就可以使用了