From b6c4f507a7e635df0bd36ab57d39d66d5935fb22 Mon Sep 17 00:00:00 2001
From: wang-hao-jie <1550036656@qq.com>
Date: 星期五, 19 十一月 2021 09:17:25 +0800
Subject: [PATCH] 违章记录
---
xboot-core/pom.xml | 15 +
xboot-core/src/main/java/cn/exrick/xboot/core/common/utils/ExcelUtil.java | 526 ++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 541 insertions(+), 0 deletions(-)
diff --git a/xboot-core/pom.xml b/xboot-core/pom.xml
index af734e5..8cdee89 100644
--- a/xboot-core/pom.xml
+++ b/xboot-core/pom.xml
@@ -113,6 +113,21 @@
<artifactId>tencentcloud-sdk-java</artifactId>
</dependency>
+ <dependency>
+ <groupId>commons-beanutils</groupId>
+ <artifactId>commons-beanutils</artifactId>
+ <version>1.8.3</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-codec</groupId>
+ <artifactId>commons-codec</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>net.sourceforge.jexcelapi</groupId>
+ <artifactId>jxl</artifactId>
+ <version>2.6.12</version>
+ </dependency>
+
<!-- 娴嬭瘯妗嗘灦TestNG + 娴嬭瘯鎶ュ憡 -->
<dependency>
<groupId>com.aventstack</groupId>
diff --git a/xboot-core/src/main/java/cn/exrick/xboot/core/common/utils/ExcelUtil.java b/xboot-core/src/main/java/cn/exrick/xboot/core/common/utils/ExcelUtil.java
new file mode 100644
index 0000000..b083818
--- /dev/null
+++ b/xboot-core/src/main/java/cn/exrick/xboot/core/common/utils/ExcelUtil.java
@@ -0,0 +1,526 @@
+package cn.exrick.xboot.core.common.utils;
+
+import cn.hutool.core.util.StrUtil;
+import jxl.Cell;
+import jxl.Sheet;
+import jxl.Workbook;
+import jxl.read.biff.BiffException;
+import jxl.write.Label;
+import jxl.write.WritableSheet;
+import jxl.write.WritableWorkbook;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.reflect.Field;
+import java.text.SimpleDateFormat;
+import java.util.*;
+import java.util.Map.Entry;
+
+/**
+ * 閫氱敤鐨別xcel瀵煎叆瀵煎嚭宸ュ叿,鐢变簬鏃堕棿浠撲績鎵�浠ヨ繖浜涗唬鐮佷腑鏈夎澶氫笉灏戒汉鎰忕殑鍦版柟銆�
+ * 鐜板湪鍙兘璇存槸瀹炵幇鐨勫姛鑳姐�傚悗鏈熷簲璇ュ閫昏緫鍜屾柟娉曚唬鐮佽繘琛岀粏鑷寸殑浼樺寲銆�
+ */
+// FIXME: 2017/9/12 杩欎釜绫荤殑浠g爜鑷涓哄啓鐨勫緢鐑�,鍏堝噾娲荤敤鍚�!!!!!!!!!!
+public final class ExcelUtil {
+
+ private ExcelUtil() {
+ }
+
+ /**
+ * function 鐢ㄤ簬瀹炵幇灏嗗悗鍙版暟鎹鍏ュ埌Excel涓�
+ *
+ * @param <T>
+ * 妯℃澘涓殑绫诲瀷
+ * @param list
+ * 瑁呭湪鏁版嵁鐨刲ist
+ * @param fieldMap
+ * Excel涓殑鏁版嵁瀹瑰櫒锛岋紒锛佸�煎緱娉ㄦ剰鐨勬槸fileMap涓殑key瑕佸拰琛ㄦ牸涓殑琛ㄥご鐨勮嫳鏂囧悕鏁版嵁涓�涓�瀵瑰簲鎵嶈
+ * 濡傛灉闇�瑕佺殑鏄紩鐢ㄥ璞$殑鏌愪釜灞炴�э紝鍒欒嫳鏂囧睘鎬т娇鐢ㄧ殑鏄被浼间簬EL琛ㄨ揪寮忕殑鏍煎紡
+ * 濡傛垜浠瓨鍌ㄧ殑鏄痵tudent锛屼絾鏄湪student涓瀛樺偍鐝骇鍚嶇О锛岀彮绾ф槸涓璞★紝杩欎釜鏃跺�欐垜浠氨鍙互杩欐牱瀹炵幇
+ * fileMap.put("Calzz.name","瀛︾敓鎵�鍦ㄧ彮绾�");
+ * @param sheetName
+ * Excel涓伐浣滆〃鐨勫悕绉�
+ * @param sheetSize
+ * 宸ヤ綔涔熷緱鏁伴噺
+ * @param out
+ * 瀛楄妭娴佽緭鍑烘祦
+ * @throws RuntimeException
+ * 1--鏁版嵁婧愭病鏈夋暟鎹�
+ */
+ public static <T> void listToExcel(List<T> list, LinkedHashMap<String, String> fieldMap, String sheetName,
+ int sheetSize, OutputStream out) throws RuntimeException {
+ // 鏁版嵁涓虹┖鎴栬�呮槸鏁版嵁涓嶅瓨鍦�
+ if (list == null || list.size() == 0) {
+ throw new RuntimeException("鏁版嵁婧愪腑娌℃湁浠讳綍鏁版嵁锛佽鏍稿疄");
+ }
+ // 涓�娆℃�у鐞嗙殑琛ㄦ牸sheet鐨勮寖鍥�
+ if (sheetSize > 65535 || sheetSize < 1) {
+ // 涓嶇鍚堣鑼�
+ sheetSize = 65535;
+ }
+ // 鍒涘缓宸ヤ綔閮紝 骞堕�氳繃out鍙戦��
+ WritableWorkbook wwb = null;
+ try {
+ wwb = Workbook.createWorkbook(out);// 杩欓噷鐨剋orkBook鍖呮槸jxl閲岄潰鐨�
+ // 璁$畻涓�鍏辨湁澶氬皯涓伐浣滆〃
+ double sheetNum = Math.ceil(list.size() / new Integer(sheetSize).doubleValue());
+ // 鍒涘缓鐩稿簲鐨勫伐浣滆〃锛屽苟鍚戝叾涓~鍏呮暟鎹�
+ for (int i = 0; i < sheetNum; i++) {
+ // 鍒ゆ柇濡傛灉鍙湁涓�寮犲伐浣滆〃鐨勬儏鍐�
+ if (sheetNum == 1) {
+ WritableSheet sheet = wwb.createSheet(sheetName, i);// 浠ヤ笅琛ㄦ潵鍛藉悕
+ fillSheet(sheet, list, fieldMap, 0, list.size() - 1);
+ } else {
+ WritableSheet sheet = wwb.createSheet(sheetName + (i + 1), i);
+ // 鑾峰彇寮�濮嬬储寮曞拰缁撴潫绱㈠紩
+ int firstIndex = i * sheetSize;
+ int lastIndex = (i + 1) * sheetSize - 1 > list.size() - 1 ? list.size() - 1
+ : (i + 1) * sheetSize - 1;
+ // 濉厖宸ヤ綔琛�
+ fillSheet(sheet, list, fieldMap, firstIndex, lastIndex);
+ }
+ }
+ wwb.write();
+ wwb.close();
+ } catch (Exception e) {
+ // TODO: handle exception
+ e.printStackTrace();
+ // 濡傛灉鏄疎xcelExpection鐩存帴鎶涘嚭
+ if (e instanceof RuntimeException) {
+ throw (RuntimeException) e;
+ } else {
+ throw new RuntimeException("Excel瀵煎叆澶辫触");
+ }
+ }
+ }
+
+ /**
+ * 閲嶈浇鐨勫嚱鏁�
+ */
+ public static <T> void listToExcel(List<T> list, LinkedHashMap<String, String> fieldMap, String sheetName,
+ OutputStream out) throws RuntimeException {
+ listToExcel(list, fieldMap, sheetName, 65535, out);
+ }
+
+ /**
+ * @MethodName : listToExcel
+ * @Description : 瀵煎嚭Excel锛堝鍑哄埌娴忚鍣紝鍙互鑷畾涔夊伐浣滆〃鐨勫ぇ灏忥級
+ * @param list
+ * 鏁版嵁婧�
+ * @param fieldMap
+ * 绫荤殑鑻辨枃灞炴�у拰Excel涓殑涓枃鍒楀悕鐨勫搴斿叧绯�
+ * @param sheetSize
+ * 姣忎釜宸ヤ綔琛ㄤ腑璁板綍鐨勬渶澶т釜鏁�
+ * @param response
+ * 浣跨敤response鍙互瀵煎嚭鍒版祻瑙堝櫒
+ * @throws RuntimeException
+ */
+ public static <T> void listToExcel(List<T> list, LinkedHashMap<String, String> fieldMap, String sheetName,
+ int sheetSize, HttpServletResponse response) throws RuntimeException {
+
+ // 璁剧疆榛樿鏂囦欢鍚嶄负褰撳墠鏃堕棿锛氬勾鏈堟棩鏃跺垎绉�
+ String fileName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()).toString();
+
+ // 璁剧疆response澶翠俊鎭�
+ response.reset();
+ response.setContentType("application/vnd.ms-excel"); // 鏀规垚杈撳嚭excel鏂囦欢
+ response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xls");
+
+ // 鍒涘缓宸ヤ綔绨垮苟鍙戦�佸埌娴忚鍣�
+ try {
+
+ OutputStream out = response.getOutputStream();
+ listToExcel(list, fieldMap, sheetName, sheetSize, out);
+
+ } catch (Exception e) {
+ e.printStackTrace();
+
+ // 濡傛灉鏄疪untimeException锛屽垯鐩存帴鎶涘嚭
+ if (e instanceof RuntimeException) {
+ throw (RuntimeException) e;
+
+ // 鍚﹀垯灏嗗叾瀹冨紓甯稿寘瑁呮垚RuntimeException鍐嶆姏鍑�
+ } else {
+ throw new RuntimeException("瀵煎嚭Excel澶辫触");
+ }
+ }
+ }
+
+ /**
+ * @MethodName : listToExcel
+ * @Description : 瀵煎嚭Excel锛堝鍑哄埌娴忚鍣紝宸ヤ綔琛ㄧ殑澶у皬鏄�2003鏀寔鐨勬渶澶у�硷級
+ * @param list
+ * 鏁版嵁婧�
+ * @param fieldMap
+ * 绫荤殑鑻辨枃灞炴�у拰Excel涓殑涓枃鍒楀悕鐨勫搴斿叧绯�
+ * @param response
+ * 浣跨敤response鍙互瀵煎嚭鍒版祻瑙堝櫒
+ * @throws RuntimeException
+ */
+ public static <T> void listToExcel(List<T> list, LinkedHashMap<String, String> fieldMap, String sheetName,
+ HttpServletResponse response) throws RuntimeException {
+
+ listToExcel(list, fieldMap, sheetName, 65535, response);
+ }
+
+ /**
+ *
+ * @param <T>
+ * 妯″瀷T
+ * @param in
+ * Excel鐨勮緭鍏ユ祦
+ * @param sheetName
+ * Excel鏂囦欢鍚�
+ * @param entityClass
+ * 瀹炰綋瀵硅薄
+ * @param fieldMap
+ * Excel涓殑涓枃鐚庡ご鍜岀被鐨勮嫳鏂囧睘鎬х殑瀵瑰簲鍏崇郴MAP
+ * @param uniqueFields
+ * 鎸囧畾涓氬姟涓婚敭缁勫悎(鏃㈠鍚堜富閿�) 锛� 杩欎簺鍒楃殑缁勫悎涓嶈兘閲嶅
+ * @return
+ * @throws RuntimeException
+ */
+ public static <T> List<T> excelToList(InputStream in, String sheetName, Class<T> entityClass,
+ LinkedHashMap<String, String> fieldMap, String[] uniqueFields) throws RuntimeException {
+ // 瀹氫箟瑕佽繑鍥炵殑list
+ List<T> resultList = new ArrayList<T>();
+ try {
+ // 鏍规嵁Excel鏁版嵁婧愭潵鍒涘缓WorkBook
+ Workbook wb = Workbook.getWorkbook(in);
+ // 鑾峰彇宸ヤ綔琛�
+ Sheet sheet = null;
+ if(StrUtil.isNotBlank(sheetName)){
+ sheet = wb.getSheet(sheetName);
+ }else{
+ sheet = wb.getSheets()[0];
+ }
+ // 鑾峰彇宸ヤ綔琛ㄧ殑鏈夋晥琛屾暟 榛樿涓�0
+ int realRows = 0;
+ for (int i = 0; i < sheet.getRows(); i++) {
+ int nullCols = 0;
+ for (int j = 0; j < sheet.getColumns(); j++) {
+ // 鑾峰彇鍗曞厓鏍煎唴瀹�
+ Cell currentCell = sheet.getCell(j, i);
+ if (currentCell == null || "".equals(currentCell.getContents().toString())) {
+ nullCols++;
+ }
+ }
+ if (nullCols == sheet.getColumns()) {
+ break;
+ } else {
+ realRows++;
+ }
+ }
+ if (realRows <= 1) {
+ // 濡傛灉Excel涓病鏈夋暟鎹垯鎻愮ず鎶ラ敊
+ throw new RuntimeException("Excel涓病鏈夋暟鎹�");
+ }
+ Cell[] firstRow = sheet.getRow(0);
+ String[] excelFieldNames = new String[firstRow.length];
+ // 鑾峰彇Excel涓殑鍒楀悕
+ for (int i = 0; i < firstRow.length; i++) {
+ excelFieldNames[i] = firstRow[i].getContents().toString().trim();
+ }
+ // 鍒ゆ柇闇�瑕佺殑瀛楁鏄惁鍚﹀凡缁忓湪Excel涓瓨鍦ㄤ簡
+ boolean isExist = true;
+ // 灏嗗瓧娈靛悕鏁扮粍杞彉鎴恖ist闆嗗悎
+ List<String> excelFieldList = Arrays.asList(excelFieldNames);
+ for (String cnName : fieldMap.values()) {
+ if (!excelFieldList.contains(cnName)) {
+ isExist = false;
+ break;
+ }
+ }
+ // 濡傛灉鏈夊垪鍚嶄笉鍖归厤 鎶ラ敊
+ if (!isExist) {
+ throw new RuntimeException("Excel涓己灏戝繀瑕佺殑瀛楁锛屾垨瀛楁鍚嶇О鏈夎锛�");
+ }
+ // 灏嗗垪鍚嶅拰鍒楀彿鏀惧叆map涓紝杩欐牱鏂逛究鎴戜滑閫氳繃鍒楀悕鎷垮埌鍒楀彿
+ LinkedHashMap<String, Integer> colMap = new LinkedHashMap<String, Integer>();
+ for (int i = 0; i < excelFieldNames.length; i++) {
+ colMap.put(excelFieldNames[i], firstRow[i].getColumn());
+ }
+ if (uniqueFields != null && uniqueFields.length > 0) {
+ // 鍒ゆ柇鏄惁鏈夐噸澶嶈
+ // 1.鑾峰彇uniqueFields鎸囧畾鐨勫垪
+ Cell[][] uniqueCells = new Cell[uniqueFields.length][];
+ for (int i = 0; i < uniqueFields.length; i++) {
+ int col = colMap.get(uniqueFields[i]);
+ uniqueCells[i] = sheet.getColumn(col);
+ }
+ // 2.浠庢寚瀹氬垪涓鎵鹃噸澶嶈
+ for (int i = 1; i < realRows; i++) {
+ int nullCols = 0;
+ for (int j = 0; j < uniqueFields.length; j++) {
+ String currentContent = uniqueCells[j][i].getContents();
+ Cell sameCell = sheet.findCell(currentContent);
+ /*
+ * Cell sameCell = sheet.findCell(currentContent,
+ * uniqueCells[j][i].getColumn(), uniqueCells[j][i]
+ * .getRow() + 1, uniqueCells[j][i] .getColumn(),
+ * uniqueCells[j][realRows - 1] .getRow(), true);
+ */
+ if (sameCell != null) {
+ nullCols++;
+ }
+ }
+ if (nullCols == uniqueFields.length) {
+ throw new RuntimeException("Excel涓湁閲嶅琛岋紝璇锋鏌�");
+ }
+ }
+ }
+ // 灏唖heet杞崲涓簂ist
+ for (int i = 1; i < realRows; i++) {
+ // 鏂板缓瑕佽浆鎹㈢殑瀵硅薄
+ T entity = entityClass.newInstance();
+ // 缁欏璞′腑鐨勫瓧娈佃祴鍊�
+ for (Entry<String, String> entry : fieldMap.entrySet()) {
+ // 鑾峰彇涓枃瀛楁鍚�
+ String cnNormalName = entry.getValue();
+ // 鑾峰彇鑻辨枃瀛楁鍚�
+ String enNormalName = entry.getKey();
+ // 鏍规嵁涓枃瀛楁鍚嶈幏鍙栧垪
+ int col = colMap.get(cnNormalName);
+ // 鑾峰彇褰撳墠鍗曞厓鏍肩殑鍐呭 鍘荤┖鏍�
+ String content = sheet.getCell(col, i).getContents().toString().trim();
+ // 缁欏璞¤祴鍊�
+ if(content!=null && !"".equals(content) && content.length()>0){
+ setFieldValueByName(enNormalName, content, entity);
+ }
+ }
+ resultList.add(entity);
+ }
+ } catch (BiffException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ throw new RuntimeException("瀵煎叆Excel澶辫触");
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ throw new RuntimeException("瀵煎叆Excel澶辫触");
+ } catch (InstantiationException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ throw new RuntimeException("瀵煎叆Excel澶辫触");
+ } catch (IllegalAccessException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ throw new RuntimeException("瀵煎叆Excel澶辫触");
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ throw new RuntimeException("瀵煎叆Excel澶辫触");
+ }
+ return resultList;
+ }
+
+ /* 杈呭姪鏂规硶 */
+ /**
+ * @MethodName : getFieldValueByName
+ * @Description : 鏍规嵁瀛楁鍚嶈幏鍙栧瓧娈靛��
+ * @param fieldName
+ * 瀛楁鍚�
+ * @param o
+ * 瀵硅薄
+ * @return 瀛楁鍊�
+ */
+ private static Object getFieldValueByName(String fieldName, Object o) throws Exception {
+
+ Object value = null;
+ Field field = getFieldByName(fieldName, o.getClass());
+
+ if (field != null) {
+ field.setAccessible(true);
+ value = field.get(o);
+ } else {
+ throw new RuntimeException(o.getClass().getSimpleName() + "绫讳笉瀛樺湪瀛楁鍚� " + fieldName);
+ }
+
+ return value;
+ }
+
+ /**
+ * @MethodName : getFieldByName
+ * @Description : 鏍规嵁瀛楁鍚嶈幏鍙栧瓧娈�
+ * @param fieldName
+ * 瀛楁鍚�
+ * @param clazz
+ * 鍖呭惈璇ュ瓧娈电殑绫�
+ * @return 瀛楁
+ */
+ private static Field getFieldByName(String fieldName, Class<?> clazz) {
+ // 鎷垮埌鏈被鐨勬墍鏈夊瓧娈�
+ Field[] selfFields = clazz.getDeclaredFields();
+
+ // 濡傛灉鏈被涓瓨鍦ㄨ瀛楁锛屽垯杩斿洖
+ for (Field field : selfFields) {
+ if (field.getName().equals(fieldName)) {
+ return field;
+ }
+ }
+
+ // 鍚﹀垯锛屾煡鐪嬬埗绫讳腑鏄惁瀛樺湪姝ゅ瓧娈碉紝濡傛灉鏈夊垯杩斿洖
+ Class<?> superClazz = clazz.getSuperclass();
+ if (superClazz != null && superClazz != Object.class) {
+ return getFieldByName(fieldName, superClazz);
+ }
+
+ // 濡傛灉鏈被鍜岀埗绫婚兘娌℃湁锛屽垯杩斿洖绌�
+ return null;
+ }
+
+ /**
+ * @MethodName : getFieldValueByNameSequence
+ * @Description : 鏍规嵁甯﹁矾寰勬垨涓嶅甫璺緞鐨勫睘鎬у悕鑾峰彇灞炴�у��
+ * 鍗虫帴鍙楃畝鍗曞睘鎬у悕锛屽userName绛夛紝鍙堟帴鍙楀甫璺緞鐨勫睘鎬у悕锛屽student.department.name绛�
+ *
+ * @param fieldNameSequence
+ * 甯﹁矾寰勭殑灞炴�у悕鎴栫畝鍗曞睘鎬у悕
+ * @param o
+ * 瀵硅薄
+ * @return 灞炴�у��
+ * @throws Exception
+ */
+ private static Object getFieldValueByNameSequence(String fieldNameSequence, Object o) throws Exception {
+
+ Object value = null;
+
+ // 灏唂ieldNameSequence杩涜鎷嗗垎
+ String[] attributes = fieldNameSequence.split("\\.");
+ if (attributes.length == 1) {
+ value = getFieldValueByName(fieldNameSequence, o);
+ } else {
+ // 鏍规嵁灞炴�у悕鑾峰彇灞炴�у璞�
+ Object fieldObj = getFieldValueByName(attributes[0], o);
+ String subFieldNameSequence = fieldNameSequence.substring(fieldNameSequence.indexOf(".") + 1);
+ value = getFieldValueByNameSequence(subFieldNameSequence, fieldObj);
+ }
+ return value;
+
+ }
+
+ /**
+ * @MethodName : setFieldValueByName
+ * @Description : 鏍规嵁瀛楁鍚嶇粰瀵硅薄鐨勫瓧娈佃祴鍊�
+ * @param fieldName
+ * 瀛楁鍚�
+ * @param fieldValue
+ * 瀛楁鍊�
+ * @param o
+ * 瀵硅薄
+ */
+ private static void setFieldValueByName(String fieldName, Object fieldValue, Object o) throws Exception {
+
+ Field field = getFieldByName(fieldName, o.getClass());
+ if (field != null) {
+ field.setAccessible(true);
+ // 鑾峰彇瀛楁绫诲瀷
+ Class<?> fieldType = field.getType();
+
+ // 鏍规嵁瀛楁绫诲瀷缁欏瓧娈佃祴鍊�
+ if (String.class == fieldType) {
+ field.set(o, String.valueOf(fieldValue));
+ } else if ((Integer.TYPE == fieldType) || (Integer.class == fieldType)) {
+ field.set(o, Integer.parseInt(fieldValue.toString()));
+ } else if ((Long.TYPE == fieldType) || (Long.class == fieldType)) {
+ field.set(o, Long.valueOf(fieldValue.toString()));
+ } else if ((Float.TYPE == fieldType) || (Float.class == fieldType)) {
+ field.set(o, Float.valueOf(fieldValue.toString()));
+ } else if ((Short.TYPE == fieldType) || (Short.class == fieldType)) {
+ field.set(o, Short.valueOf(fieldValue.toString()));
+ } else if ((Double.TYPE == fieldType) || (Double.class == fieldType)) {
+ field.set(o, Double.valueOf(fieldValue.toString()));
+ } else if (Character.TYPE == fieldType) {
+ field.set(o, Character.valueOf(fieldValue.toString().charAt(0)));
+ } else if (Date.class == fieldType) {
+ String dateStr = fieldValue.toString();
+ if(dateStr.length() > 10){
+ field.set(o, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr));
+ }else{
+ field.set(o, new SimpleDateFormat("yyyy-MM-dd").parse(dateStr));
+ }
+ } else {
+ field.set(o, fieldValue);
+ }
+ } else {
+ throw new RuntimeException(o.getClass().getSimpleName() + "绫讳笉瀛樺湪瀛楁鍚� " + fieldName);
+ }
+ }
+
+ /**
+ * @MethodName : setColumnAutoSize
+ * @Description : 璁剧疆宸ヤ綔琛ㄨ嚜鍔ㄥ垪瀹藉拰棣栬鍔犵矖
+ * @param ws
+ */
+ private static void setColumnAutoSize(WritableSheet ws, int extraWith) {
+ // 鑾峰彇鏈垪鐨勬渶瀹藉崟鍏冩牸鐨勫搴�
+ for (int i = 0; i < ws.getColumns(); i++) {
+ int colWith = 0;
+ for (int j = 0; j < ws.getRows(); j++) {
+ String content = ws.getCell(i, j).getContents().toString();
+ int cellWith = content.length();
+ if (colWith < cellWith) {
+ colWith = cellWith;
+ }
+ }
+ // 璁剧疆鍗曞厓鏍肩殑瀹藉害涓烘渶瀹藉搴�+棰濆瀹藉害
+ ws.setColumnView(i, colWith + extraWith);
+ }
+
+ }
+
+ /**
+ * @MethodName : fillSheet
+ * @Description : 鍚戝伐浣滆〃涓~鍏呮暟鎹�
+ * @param sheet
+ * 宸ヤ綔琛�
+ * @param list
+ * 鏁版嵁婧�
+ * @param fieldMap
+ * 涓嫳鏂囧瓧娈靛搴斿叧绯荤殑Map
+ * @param firstIndex
+ * 寮�濮嬬储寮�
+ * @param lastIndex
+ * 缁撴潫绱㈠紩
+ */
+ private static <T> void fillSheet(WritableSheet sheet, List<T> list, LinkedHashMap<String, String> fieldMap,
+ int firstIndex, int lastIndex) throws Exception {
+
+ // 瀹氫箟瀛樻斁鑻辨枃瀛楁鍚嶅拰涓枃瀛楁鍚嶇殑鏁扮粍
+ String[] enFields = new String[fieldMap.size()];
+ String[] cnFields = new String[fieldMap.size()];
+
+ // 濉厖鏁扮粍
+ int count = 0;
+ for (Entry<String, String> entry : fieldMap.entrySet()) {
+ enFields[count] = entry.getKey();
+ cnFields[count] = entry.getValue();
+ count++;
+ }
+ // 濉厖琛ㄥご
+ for (int i = 0; i < cnFields.length; i++) {
+ Label label = new Label(i, 0, cnFields[i]);
+ sheet.addCell(label);
+ }
+
+ // 濉厖鍐呭
+ int rowNo = 1;
+ for (int index = firstIndex; index <= lastIndex; index++) {
+ // 鑾峰彇鍗曚釜瀵硅薄
+ T item = list.get(index);
+ for (int i = 0; i < enFields.length; i++) {
+ Object objValue = getFieldValueByNameSequence(enFields[i], item);
+ String fieldValue = objValue == null ? "" : objValue.toString();
+ Label label = new Label(i, rowNo, fieldValue);
+ sheet.addCell(label);
+ }
+
+ rowNo++;
+ }
+
+ // 璁剧疆鑷姩鍒楀
+ setColumnAutoSize(sheet, 5);
+ }
+}
--
Gitblit v1.9.1