使用easy poi註解實現二級表頭
一、使用easy poi註解實現二級表頭效果
使用註解寫的話easy poi 貌似最多隻能實現二級表頭, 更多級需求用easy poi的模板, 或者用阿里的easy excel; 下面是註解實現下圖效果程式碼。官網: http://easypoi.mydoc.io/#category_41961
二、實現
1、依賴
<!-- spring boot直接用這個就行 -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<!-- 非spring boot用這個 -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.3.0</version>
</dependency>
2、實體bean
@Data
public class RealIncome {
/**
* 運營專案名稱
*/
@Excel(name = "運營專案名稱", orderNum = "0", width = 15, height = 13.5d, mergeVertical = true)
private String projectName;
/**
* 我方主體名稱
*/
@Excel(name = "我方主體名稱", orderNum = "1", width = 15, isWrap = false)
private String myEntityName;
/**
* 收入_含稅 1-12月
*/
@Excel(name = "1月", orderNum = "7", groupName = "收入_含稅")
private BigDecimal incomeContainTax1;
@Excel(name = "2月", orderNum = "8", groupName = "收入_含稅")
@Excel(name = "12月", orderNum = "18", groupName = "收入_含稅")
private BigDecimal incomeContainTax12;
@Excel(name = "總計", orderNum = "19", groupName = "收入_含稅")
private BigDecimal incomeContainTaxSum;
...
}
上面height屬性設定行高不生效, 官網寫使用@ExcelTarget的height, 但是裡面只有一個value屬性, 沒有找到height
更多引數見官網: http://easypoi.mydoc.io/#category_41961
3、生成匯出
/**
* excel 匯出
*
* @param list 資料
* @param title 標題
* @param sheetName sheet名稱
* @param pojoClass pojo型別
* @param fileName 檔名稱
* @param response
*/
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
HttpServletResponse response) throws IOException {
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName, ExcelType.XSSF));
}
/**
* 預設的 excel 匯出
*
* @param list 資料
* @param pojoClass pojo型別
* @param fileName 檔名稱
* @param response
* @param exportParams 匯出引數
*/
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response,
ExportParams exportParams) throws IOException {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
downLoadExcel(fileName, response, workbook);
}
/**
* 下載
*
* @param fileName 檔名稱
* @param response
* @param workbook excel資料
*/
private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook)
throws IOException {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename="
+ URLEncoder.encode(fileName + "." + ExcelTypeEnum.XLSX.getValue(), "UTF-8"));
workbook.write(response.getOutputStream());
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
4、問題
使用上述註解實現, 無法動態傳入表頭!!!
可以使用下面的ExcelExportEntity實現, 但是對資料封裝結構要求比較嚴格, 沒法直接用List實現!!!
5、使用ExcelExportEntity實現動態表頭傳值
資料封裝結構比較複雜, 不好實現, 不推薦, 直接使用模板應該可以。
public static void main(String[] args) {
List<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> map = new HashMap<>();
map.put("projectName", "雲");
map.put("myEntityName", "en");
List<Map<String, Object>> incomeCTList = new ArrayList<>();
Map<String, Object> subMap = new HashMap<>();
subMap.put("incomeContainTax1", "12");
subMap.put("incomeContainTax2", "13");
subMap.put("incomeContainTaxSum", "25");
incomeCTList.add(subMap);
map.put("incomeContainTax", incomeCTList);
list.add(map);
Map<String, Object> map2 = new HashMap<>();
map2.put("projectName", "雲2");
map2.put("myEntityName", "en2");
List<Map<String, Object>> incomeCTList2 = new ArrayList<>();
Map<String, Object> subMap2 = new HashMap<>();
subMap2.put("incomeContainTax1", "120");
subMap2.put("incomeContainTax2", "130");
subMap2.put("incomeContainTaxSum", "250");
incomeCTList2.add(subMap2);
map2.put("incomeContainTax", incomeCTList2);
List<Map<String, Object>> incomeNCTList2 = new ArrayList<>();
Map<String, Object> subMap22 = new HashMap<>();
subMap22.put("incomeNotContainTax1", "120");
subMap22.put("incomeNotContainTax2", "130");
subMap22.put("incomeNotContainTaxSum", new BigDecimal("23.567"));
incomeNCTList2.add(subMap22);
map2.put("incomeNotContainTax", incomeNCTList2);
list.add(map2);
try {
test(list);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void test(List list) throws IOException {
List<ExcelExportEntity> colList = new ArrayList<>();
ExcelExportEntity colEntity = new ExcelExportEntity("運營專案名稱", "projectName");
colEntity.setNeedMerge(true);
colList.add(colEntity);
colEntity = new ExcelExportEntity("我方主體名稱", "myEntityName");
colEntity.setNeedMerge(true);
colList.add(colEntity);
String year = "2020";
ExcelExportEntity incomeContainTaxGroup = new ExcelExportEntity("收入_含稅", "incomeContainTax");
List<ExcelExportEntity> incomeContainTaxList = new ArrayList<>();
incomeContainTaxList.add(new ExcelExportEntity("1月", "incomeContainTax1"));
incomeContainTaxList.add(new ExcelExportEntity("2月", "incomeContainTax2"));
incomeContainTaxList.add(new ExcelExportEntity("3月", "incomeContainTax3"));
incomeContainTaxList.add(new ExcelExportEntity(year, "incomeContainTaxSum"));
incomeContainTaxGroup.setList(incomeContainTaxList);
colList.add(incomeContainTaxGroup);
ExcelExportEntity incomeNotContainTaxGroup = new ExcelExportEntity("收入_不含稅", "incomeNotContainTax");
List<ExcelExportEntity> incomeNotContainTaxList = new ArrayList<>();
incomeNotContainTaxList.add(new ExcelExportEntity("1月", "incomeNotContainTax1"));
incomeNotContainTaxList.add(new ExcelExportEntity("2月", "incomeNotContainTax2"));
incomeNotContainTaxList.add(new ExcelExportEntity("3月", "incomeNotContainTax3"));
incomeNotContainTaxList.add(new ExcelExportEntity(year, "incomeNotContainTaxSum"));
incomeNotContainTaxGroup.setList(incomeNotContainTaxList);
colList.add(incomeNotContainTaxGroup);
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("利潤彙總表", "sheetName啊"), colList,
list);
FileOutputStream fos = new FileOutputStream("E:/利潤彙總表.xls");
workbook.write(fos);
fos.close();
}
相關文章
- 使用高斯Redis實現二級索引Redis索引
- Android使用RecyclerView實現二級列表AndroidView
- Apache POI使用詳解Apache
- 通過模板實現POI
- 報表展現時如何實現固定表頭效果
- spring-AOP(二)實現原理之AspectJ註解方式Spring
- 【Spring註解驅動開發】使用@Lazy註解實現懶載入Spring
- POI的使用及匯出excel報表Excel
- 使用自定義註解透過BeanPostProcessor實現策略模式Bean模式
- js 實現二級聯動JS
- 二. 重識Java之夯實註解Java
- 如何實現報表的點選表頭排序需求排序
- php中用ajax實現二級省市級聯PHP
- Deno從零到架構級系列(二)——註解路由架構路由
- Java實現資料庫和資料表的二級聯動Java資料庫
- 帶你瞭解Go怎樣實現二級快取Go快取
- 實驗一--Easy IoT實現mqtt實驗MQQT
- Android註解使用之通過annotationProcessor註解生成程式碼實現自己的ButterKnife框架Android框架
- 使用float,flex和tailwind實現同一個表單註冊效果FlexAI
- Spring系列之事務的控制 註解實現+xml實現+事務的隔離等級SpringXML
- 【C#】DevExpress實現複合表頭C#devExpress
- 原生JS實現二級聯動JS
- 可行的二級評論實現
- 保姆教程系列二、Nacos實現註冊中心
- 自定義註解+反射 實現給註解新增功能的效果反射
- Java高階特性-註解:註解實現Excel匯出功能JavaExcel
- 註解實現:判空賦值賦值
- so easy 前端實現多語言前端
- Maui Blazor 使用攝像頭實現UIBlazor
- 使用工具類 使用poi匯入匯出excel報表Excel
- HTML 使用表單標籤實現註冊頁面的例項程式碼HTML
- Spring Boot之使用Scheduled註解實現定時任務Springboot
- SpringBoot 介面引數解密的實現方法(使用註解)Spring Boot解密
- Django實踐(二)——使用模型類定義資料表,實現表單頁面跳轉Django模型
- Spring實現無需註解實現自動注入Spring
- 用CSS實現的固定表頭的HTML表格CSSHTML
- web 中怎麼實現斜線表頭效果?Web
- Silverlight DataGrid 多重表頭實現