国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - Java使用easypoi快速導入導出的實現

Java使用easypoi快速導入導出的實現

2021-07-22 16:15子_軒 Java教程

這篇文章主要介紹了實現Java使用easypoi快速導入導出的實現,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

簡介

easypoi功能如同名字easy,主打的功能就是容易,讓一個沒見接觸過poi的人員就可以方便的寫出Excel導入,導出,通過簡單的注解和模板語言(熟悉的表達式語法),完成以前復雜的寫法。

集成

pom 中引入依賴即可

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!--easypoi-->
<dependency>
  <groupId>cn.afterturn</groupId>
  <artifactId>easypoi-base</artifactId>
  <version>3.0.3</version>
</dependency>
<dependency>
  <groupId>cn.afterturn</groupId>
  <artifactId>easypoi-web</artifactId>
  <version>3.0.3</version>
</dependency>
<dependency>
  <groupId>cn.afterturn</groupId>
  <artifactId>easypoi-annotation</artifactId>
  <version>3.0.3</version>
</dependency>

整合工具類 EasyPoiUtil

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package cn.common.util;
 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.common.exception.ZXException;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
 
/**
 * @author huangy
 * @date 2019/6/28 14:57
 */
public class EasyPoiUtil {
    /**
    * 導出Excel,包括文件名以及表名。是否創建表頭
    *
    * @param list 導出的實體類
    * @param title 表頭名稱
    * @param sheetName sheet表名
    * @param pojoClass 映射的實體類
    * @param isCreateHeader 是否創建表頭
    * @param fileName 文件名
    * @param response
    * @return
    */
  public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response){
    ExportParams exportParams = new ExportParams(title, sheetName);
    exportParams.setCreateHeadRows(isCreateHeader);
    defaultExport(list, pojoClass, fileName, response, exportParams);
 
  }
 
  /**
   * 導出Excel 默認格式 默認有創建表頭
   */
  public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName, HttpServletResponse response){
    defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
  }
 
  /**
   * map多sheet形式導出
   * @param list
   * @param fileName
   * @param response
   */
  public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response){
    defaultExport(list, fileName, response);
  }
 
  /**
   * 常規默認導出方式
   * @param list
   * @param pojoClass
   * @param fileName
   * @param response
   * @param exportParams
   */
  private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {
    Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
    ExcelExportUtil.closeExportBigExcel();
    if (workbook != null);
    downLoadExcel(fileName, response, workbook);
  }
 
  /**
   * 多sheet默認導出方式
   * @param list
   * @param fileName
   * @param response
   */
  private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
    Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
    ExcelExportUtil.closeExportBigExcel();
    if (workbook != null);
    downLoadExcel(fileName, response, workbook);
  }
 
  /**
   * 下載excel
   * @param fileName
   * @param response
   * @param workbook
   */
  private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
    try {
      response.setCharacterEncoding("UTF-8");
      response.setHeader("content-Type", "application/vnd.ms-excel");
      response.setHeader("Content-Disposition",
          "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
      workbook.write(response.getOutputStream());
    } catch (IOException e) {
      throw new ZXException(e.getMessage());
    }
  }
 
  /**
   * 導入 文件路徑形式
   * @param filePath
   * @param titleRows
   * @param headerRows
   * @param pojoClass
   * @param <T>
   * @return
   */
  public static <T> List<T> importExcel(String filePath,Integer titleRows,Integer headerRows, Class<T> pojoClass){
    if (StringUtils.isBlank(filePath)){
      return null;
    }
    ImportParams params = new ImportParams();
    params.setTitleRows(titleRows);
    params.setHeadRows(headerRows);
    List<T> list = null;
    try {
      list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
    }catch (NoSuchElementException e){
      throw new ZXException("模板不能為空");
    } catch (Exception e) {
      e.printStackTrace();
      throw new ZXException(e.getMessage());
    }
    return list;
  }
 
  /**
   * 導入 MultipartFile 形式
   * @param file
   * @param titleRows
   * @param headerRows
   * @param pojoClass
   * @param <T>
   * @return
   */
  public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){
    if (file == null){
      return null;
    }
    ImportParams params = new ImportParams();
    params.setTitleRows(titleRows);
    params.setHeadRows(headerRows);
    List<T> list = null;
    try {
      list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
    }catch (NoSuchElementException e){
      throw new ZXException("excel文件不能為空");
    } catch (Exception e) {
      throw new ZXException(e.getMessage());
    }
    return list;
  }
 
}

使用示例

實體類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public class BlackListExport {
  @Excel(name = "客戶姓名", width = 15, orderNum = "2")
  private String name;
  @Excel(name = "備注", width = 10, orderNum = "1")
  private String remark;
  @Excel(name = "手機號", width = 15, orderNum = "0")
  private String phone;
 
  public String getName() {
    return name;
  }
 
  public void setName(String name) {
    this.name = name;
  }
 
  public String getRemark() {
    return remark;
  }
 
  public void setRemark(String remark) {
    this.remark = remark;
  }
 
  public String getPhone() {
    return phone;
  }
 
  public void setPhone(String phone) {
    this.phone = phone;
  }
 
  public BlackListExport() {
  }
 
  public BlackListExport(String name, String remark, String phone) {
    this.name = name;
    this.remark = remark;
    this.phone = phone;
  }
}

接口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
@ApiOperation(value = "easyPoiUtil 導出測試")
@GetMapping(value = "/poi/export1")
public void export1(HttpServletResponse response){
  List<BlackListExport> list=new ArrayList<>();
  for(int i=0;i<10000;i++){
    list.add(new BlackListExport(i+"",i+"",i+""));
  }
  EasyPoiUtil.exportExcel(list,"zx","huangy",BlackListExport.class,"zx.xls",response);
}
 
/**
 * 如果填充不同sheet得data數據列表使用相同得list對象進行填充的話,
 * 會出現第一次填充得sheet有數據,后續其他使用相同list對象進行data填充得sheet沒有數據展示。
 * @param response
 */
@ApiOperation(value = "多sheet 導出測試")
@GetMapping(value = "/poi/export2")
public void export2(HttpServletResponse response){
  // 查詢數據,此處省略
  List list = new ArrayList<>();
  list.add(new BlackListExport("姓名1","備注1","手機1")) ;
  list.add(new BlackListExport("姓名2","備注2","手機2")) ;
  list.add(new BlackListExport("姓名3","備注3","手機3")) ;
  List list2 = new ArrayList<>();
  list2.add(new BlackListExport("姓名-1","備注-1","手機-1")) ;
  list2.add(new BlackListExport("姓名-2","備注-2","手機-2")) ;
  list2.add(new BlackListExport("姓名-3","備注-3","手機-3")) ;
  List<Map<String,Object>> sheetsList = new ArrayList<>();
  for(int i=1;i<=4;i++){
  // 設置導出配置
  // 創建參數對象(用來設定excel得sheet得內容等信息)
  ExportParams params = new ExportParams() ;
  // 設置sheet得名稱
  params.setSheetName("表格"+i);
 
  //創建sheet使用的map
  Map<String,Object> dataMap = new HashMap<>();
  // title的參數為ExportParams類型,目前僅僅在ExportParams中設置了sheetName
  dataMap.put("title",params) ;
  // 模版導出對應得實體類型
  dataMap.put("entity",BlackListExport.class) ;
  // sheet中要填充得數據
  if(i%2==0){
    dataMap.put("data",list) ;
  }else {
    dataMap.put("data",list2) ;
  }
 
  sheetsList.add(dataMap);
  }
  EasyPoiUtil.exportExcel(sheetsList,"hy.xls",response);
}

到此這篇關于Java使用easypoi快速導入導出的實現的文章就介紹到這了,更多相關Java easypoi導入導出內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/qq_37209293/article/details/94025049

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 欧美黄色精品 | 美女视频黄色 | 国产精品久久精品 | 国产一区二区三区欧美 | 正在播放国产精品 | 希岛爱理在线 | 亚洲精品在线视频 | 一区二区三区入口 | 日本久久国产 | 久久久久国产精品www | 一本大道伊人久久综合 | 在线视频自拍 | 日韩精品视频一区二区三区 | 免费嗨片网| 亚洲精品二区三区 | 91精品国产日韩91久久久久久 | 黄色影片免费观看 | 国产精品极品美女在线观看免费 | 日韩精品极品视频在线观看免费 | 欧美三区二区一区 | 精品久 | 中文字幕亚洲一区二区三区 | 亚洲欧美自拍偷拍 | 欧美一级特黄aaaaaa大片在线观看 | 福利视频网 | 国内免费自拍视频 | 中文在线√天堂 | 色狠狠一区 | 免费在线成人网 | 亚洲国产精品尤物yw在线观看 | aaa综合国产 | 国产黄色在线播放 | 日韩在线播放一区二区 | 天天色天天色 | 伊人干 | www.久久| 人人九九| av一区二区三区四区 | 一级黄色大片免费观看 | 欧美成人免费在线 | 国产免费一区二区三区 |