|
@@ -0,0 +1,1075 @@
|
|
|
+package com.jsjty.jdc.common;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.lang.reflect.Modifier;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Calendar;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Map.Entry;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFCell;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFCellStyle;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFDateUtil;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFRow;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
|
|
+import org.apache.poi.ss.usermodel.Cell;
|
|
|
+import org.apache.poi.ss.usermodel.CellStyle;
|
|
|
+import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.xtframe.core.exception.BizException;
|
|
|
+import com.xtframe.util.DateTime;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 公共方法
|
|
|
+ *
|
|
|
+ * @author zhoutl
|
|
|
+ *
|
|
|
+ * @param <ID>
|
|
|
+ */
|
|
|
+public class CommonUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取年份数据
|
|
|
+ *
|
|
|
+ * @param num
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<Map<String, String>> getYearList(int num) {
|
|
|
+ List<Map<String, String>> yearList = new ArrayList<Map<String, String>>();
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ int year = c.get(Calendar.YEAR);
|
|
|
+ for (int i = year; i > (year - num); i--) {
|
|
|
+ Map<String, String> map = new HashMap<String, String>();
|
|
|
+ map.put("code", String.valueOf(i));
|
|
|
+ map.put("value", String.valueOf(i));
|
|
|
+ yearList.add(map);
|
|
|
+ }
|
|
|
+ return yearList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置CELL单元格样式
|
|
|
+ *
|
|
|
+ * @param sheet
|
|
|
+ * @param rownum
|
|
|
+ * @param cellnum
|
|
|
+ * @param value
|
|
|
+ */
|
|
|
+ private static void setCellStyle(HSSFSheet sheet, int rownum, int cellnum, CellStyle style) {
|
|
|
+ HSSFCell cell = getCell(sheet, rownum, cellnum);
|
|
|
+ cell.setCellStyle(style);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置CELL单元格内容并附带样式
|
|
|
+ *
|
|
|
+ * @param sheet
|
|
|
+ * @param rownum
|
|
|
+ * @param cellnum
|
|
|
+ * @param value
|
|
|
+ */
|
|
|
+ public static void setCellValue(HSSFSheet sheet, int rownum, int cellnum, String value) {
|
|
|
+ HSSFCell cell = getCell(sheet, rownum, cellnum);
|
|
|
+ cell.setCellValue(value);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取CELL单元格
|
|
|
+ *
|
|
|
+ * @param sheet
|
|
|
+ * @param rownum
|
|
|
+ * @param cellnum
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static HSSFCell getCell(HSSFSheet sheet, int rownum, int cellnum) {
|
|
|
+ if (null == sheet.getRow(rownum)) {
|
|
|
+ sheet.createRow(rownum);
|
|
|
+ }
|
|
|
+ if (null == sheet.getRow(rownum).getCell(cellnum)) {
|
|
|
+ sheet.getRow(rownum).createCell(cellnum);
|
|
|
+ }
|
|
|
+ return sheet.getRow(rownum).getCell(cellnum);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 动态填充值
|
|
|
+ *
|
|
|
+ * @param sheet
|
|
|
+ * @param cellKeyMap
|
|
|
+ * @param o
|
|
|
+ */
|
|
|
+ public static void fillCellValue(HSSFSheet sheet, Map<String, Map<String, Integer>> cellKeyMap, Object o) {
|
|
|
+ Iterator<String> it = cellKeyMap.keySet().iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ String key = (String) it.next();
|
|
|
+ Map<String, Integer> cellPosMap = cellKeyMap.get(key);
|
|
|
+ String value = (String) getFieldValueByName(key, o);
|
|
|
+ setCellValue(sheet, cellPosMap.get("rowIndex"), cellPosMap.get("colIndex"), value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 动态填充值
|
|
|
+ *
|
|
|
+ * @param sheet
|
|
|
+ * @param cellKeyMap
|
|
|
+ * @param o
|
|
|
+ * @param 是否竖排往下递增
|
|
|
+ */
|
|
|
+ public static void fillListCellValue(HSSFSheet sheet, Map<String, Map<String, Integer>> cellKeyMap,
|
|
|
+ List<Object> listObj, boolean isV) {
|
|
|
+ Iterator<String> it = null;
|
|
|
+ if (null != listObj && listObj.size() > 0) {
|
|
|
+ for (int i = 0; i < listObj.size(); i++) {
|
|
|
+ Object o = listObj.get(i);
|
|
|
+ it = cellKeyMap.keySet().iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ String key = (String) it.next();
|
|
|
+ Map<String, Integer> cellPosMap = cellKeyMap.get(key);
|
|
|
+ // 获取行号
|
|
|
+ Integer rowIndex = Integer.parseInt(cellPosMap.get("rowIndex").toString());
|
|
|
+ // 获取列号
|
|
|
+ Integer colIndex = Integer.parseInt(cellPosMap.get("colIndex").toString());
|
|
|
+ if (isV) {
|
|
|
+ rowIndex = rowIndex + i;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ colIndex = colIndex + i;
|
|
|
+ }
|
|
|
+ String value = (String) getFieldValueByName(key, o);
|
|
|
+ setCellValue(sheet, rowIndex, colIndex, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ it = cellKeyMap.keySet().iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ String key = (String) it.next();
|
|
|
+ Map<String, Integer> cellPosMap = cellKeyMap.get(key);
|
|
|
+ // 获取行号
|
|
|
+ Integer rowIndex = Integer.parseInt(cellPosMap.get("rowIndex").toString());
|
|
|
+ // 获取列号
|
|
|
+ Integer colIndex = Integer.parseInt(cellPosMap.get("colIndex").toString());
|
|
|
+ setCellValue(sheet, rowIndex, colIndex, "");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所有单元格动态键值
|
|
|
+ *
|
|
|
+ * @param sheet
|
|
|
+ */
|
|
|
+ public static Map<String, Map<String, Integer>> getCellKeyMap(HSSFSheet sheet) {
|
|
|
+ return getCellKeyMapByMark(sheet, Constants.EXCEL_KEY_SIGN);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所有单元格动态键值
|
|
|
+ *
|
|
|
+ * @param sheet
|
|
|
+ */
|
|
|
+ public static Map<String, Map<String, Integer>> getCellKeyMapByMark(HSSFSheet sheet, String mark) {
|
|
|
+ Map<String, Map<String, Integer>> cellKeyMap = new HashMap<String, Map<String, Integer>>();
|
|
|
+ int rowCount = sheet.getLastRowNum();
|
|
|
+ HSSFRow row = null;
|
|
|
+ HSSFCell cell = null;
|
|
|
+ // 循环所有行
|
|
|
+ for (int rowIndex = 0; rowIndex <= rowCount; rowIndex++) {
|
|
|
+ row = sheet.getRow(rowIndex);
|
|
|
+ int colCount = row.getPhysicalNumberOfCells();
|
|
|
+ // 循环所有列
|
|
|
+ for (int colIndex = 0; colIndex <= colCount; colIndex++) {
|
|
|
+ cell = row.getCell(colIndex);
|
|
|
+ if (null != cell) {
|
|
|
+ if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String cellVal = cell.getStringCellValue();
|
|
|
+ if (cellVal.startsWith(mark) && cellVal.endsWith(mark)) {
|
|
|
+ String key = cellVal.replace(mark, "");
|
|
|
+ Map<String, Integer> cellPosMap = new HashMap<String, Integer>();
|
|
|
+ cellPosMap.put("rowIndex", rowIndex);
|
|
|
+ cellPosMap.put("colIndex", colIndex);
|
|
|
+ cellKeyMap.put(key, cellPosMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return cellKeyMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所有单元格动态键值
|
|
|
+ *
|
|
|
+ * @param sheet
|
|
|
+ */
|
|
|
+ public static Map<String, Map<String, Integer>> getCellKeyMapBIndex(HSSFSheet sheet, int index) {
|
|
|
+ Map<String, Map<String, Integer>> cellKeyMap = new HashMap<String, Map<String, Integer>>();
|
|
|
+ HSSFRow row = null;
|
|
|
+ HSSFCell cell = null;
|
|
|
+ // 循环所有行
|
|
|
+ row = sheet.getRow(index);
|
|
|
+ int colCount = row.getPhysicalNumberOfCells();
|
|
|
+ // 循环所有列
|
|
|
+ for (int colIndex = 0; colIndex <= colCount; colIndex++) {
|
|
|
+ cell = row.getCell(colIndex);
|
|
|
+ if (null != cell) {
|
|
|
+ if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String cellVal = cell.getStringCellValue();
|
|
|
+ if (StringUtils.isNotEmpty(cellVal)) {
|
|
|
+ String key = cellVal;
|
|
|
+ Map<String, Integer> cellPosMap = new HashMap<String, Integer>();
|
|
|
+ cellPosMap.put("rowIndex", index);
|
|
|
+ cellPosMap.put("colIndex", colIndex);
|
|
|
+ cellKeyMap.put(key, cellPosMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return cellKeyMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取所有单元格动态键值
|
|
|
+ *
|
|
|
+ * @param sheet
|
|
|
+ */
|
|
|
+ public static Map<String, Map<String, Integer>> getCellKeyMapByRow(HSSFRow row) {
|
|
|
+ Map<String, Map<String, Integer>> cellKeyMap = new HashMap<String, Map<String, Integer>>();
|
|
|
+ int colCount = row.getPhysicalNumberOfCells();
|
|
|
+ HSSFCell cell = null;
|
|
|
+ // 循环所有列
|
|
|
+ for (int colIndex = 0; colIndex <= colCount; colIndex++) {
|
|
|
+ cell = row.getCell(colIndex);
|
|
|
+ if (null != cell) {
|
|
|
+ String cellVal = cell.getStringCellValue();
|
|
|
+ if (StringUtils.isNotEmpty(cellVal)) {
|
|
|
+ String key = cellVal;
|
|
|
+ Map<String, Integer> cellPosMap = new HashMap<String, Integer>();
|
|
|
+ cellPosMap.put("rowIndex", cell.getRowIndex());
|
|
|
+ cellPosMap.put("colIndex", colIndex);
|
|
|
+ cellKeyMap.put(key, cellPosMap);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return cellKeyMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据属性名获取属性值
|
|
|
+ * */
|
|
|
+ public static Object getFieldValueByName(String fieldName, Object o) {
|
|
|
+ try {
|
|
|
+ String firstLetter = fieldName.substring(0, 1).toUpperCase();
|
|
|
+ String getter = "get" + firstLetter + fieldName.substring(1);
|
|
|
+ Method method = o.getClass().getMethod(getter, new Class[] {});
|
|
|
+ Object value = method.invoke(o, new Object[] {});
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("rawtypes")
|
|
|
+ public static void exportExcel(HttpServletRequest request, HttpServletResponse response, String title,
|
|
|
+ String fileName, String downName, int index, List list, Class clazz) throws Exception {
|
|
|
+ downName = URLEncoder.encode(downName, "UTF-8");
|
|
|
+ response.setContentType("application/msexcel; charset=utf-8");
|
|
|
+ response.setHeader("Content-disposition", "attachment; filename=" + downName);
|
|
|
+ InputStream is = null;
|
|
|
+ HSSFWorkbook workbook2007 = null;
|
|
|
+ HSSFSheet sheet = null;
|
|
|
+ OutputStream bos = null;
|
|
|
+ try {
|
|
|
+ String realpath = File.separator + "template" + File.separator + fileName;
|
|
|
+ String path = request.getClass().getClassLoader().getResource("").getPath();
|
|
|
+ File fi = new File(path + realpath);
|
|
|
+ POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fi));
|
|
|
+ workbook2007 = new HSSFWorkbook(fs);
|
|
|
+ sheet = workbook2007.getSheetAt(0);
|
|
|
+ if (StringUtils.isNotBlank(title)) {
|
|
|
+ sheet.getRow(0).getCell(0).setCellValue(title);
|
|
|
+ }
|
|
|
+ setListInfo(sheet, index, list);
|
|
|
+ bos = response.getOutputStream();
|
|
|
+ workbook2007.write(bos);
|
|
|
+ response.flushBuffer();
|
|
|
+ bos.flush();
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ try {
|
|
|
+ if (is != null) {
|
|
|
+ is.close();
|
|
|
+ }
|
|
|
+ if (bos != null) {
|
|
|
+ bos.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings({ "rawtypes", "unchecked" })
|
|
|
+ private static void setListInfo(HSSFSheet sheet, int index, List list) {
|
|
|
+ int END_ROW = index + list.size();
|
|
|
+ if (null == list || list.size() == 0) {
|
|
|
+ HSSFRow modRow = sheet.getRow(index + 1);
|
|
|
+ sheet.removeRow(modRow);
|
|
|
+ }
|
|
|
+ if (null != list && list.size() > 0) {
|
|
|
+ List mapList = reflectForgetData(list.get(0).getClass(), list);
|
|
|
+ HSSFRow modRow = sheet.getRow(index + 1);
|
|
|
+ for (int i = 1; i < mapList.size(); i++) {
|
|
|
+ sheet.shiftRows(index + i + 1, END_ROW, 1, true, false);
|
|
|
+ HSSFRow newXSSFRow = sheet.createRow(index + i + 1);
|
|
|
+ newXSSFRow.setHeight(modRow.getHeight());
|
|
|
+ for (int k = 0; k < modRow.getLastCellNum(); k++) {
|
|
|
+ HSSFCell modcell = modRow.getCell(k);
|
|
|
+ HSSFCell newcell = newXSSFRow.createCell(k);
|
|
|
+ if (null == modcell) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ newcell.setCellStyle(modcell.getCellStyle());
|
|
|
+ newcell.setCellType(modcell.getCellType());
|
|
|
+ if (null != modcell.getStringCellValue() && !"".equals(modcell.getStringCellValue().trim())) {
|
|
|
+ newcell.setCellValue(modcell.getStringCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ int cellIndex = index + 1;
|
|
|
+ Map<String, Object> map = null;
|
|
|
+ for (int i = 0; i < mapList.size(); i++) {
|
|
|
+ map = (Map<String, Object>) mapList.get(i);
|
|
|
+ HSSFRow row = sheet.getRow(cellIndex);
|
|
|
+ if (row == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ for (int j = 0; j < row.getLastCellNum(); j++) {
|
|
|
+ HSSFCell cell = row.getCell(j);
|
|
|
+ if (cell == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (map.containsKey(cell.getStringCellValue())) {
|
|
|
+ if (map.get(cell.getStringCellValue()) != null) {
|
|
|
+ cell.setCellValue(String.valueOf(map.get(cell.getStringCellValue())));
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ cell.setCellValue("");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ cellIndex++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings({ "rawtypes" })
|
|
|
+ private static List<Map<String, Object>> reflectForgetData(Class classType, List list) {
|
|
|
+ List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
|
|
|
+ try {
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ Object obj = list.get(i);
|
|
|
+ Map<String, Object> map = new HashMap<String, Object>();
|
|
|
+ Field[] fields = getFields(classType);
|
|
|
+ for (Field f : fields) {
|
|
|
+ if (Modifier.isStatic(f.getModifiers()) || Modifier.isFinal(f.getModifiers())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ f.setAccessible(true);
|
|
|
+ map.put(f.getName(), f.get(obj));
|
|
|
+ }
|
|
|
+ map.put("cout", i + 1);
|
|
|
+ resultList.add(map);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return resultList;
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings({ "rawtypes" })
|
|
|
+ private static Field[] getFields(Class classType) {
|
|
|
+ if (classType != null && !Object.class.equals(classType)) {
|
|
|
+ Field[] fields = classType.getDeclaredFields();
|
|
|
+ Field[] fieldss = getFields(classType.getSuperclass());
|
|
|
+ if (fieldss.length <= 0) { return fields; }
|
|
|
+ next: for (int i = 0; i < fieldss.length; i++) {
|
|
|
+ for (int j = 0; j < fields.length; j++) {
|
|
|
+ if (fieldss[i].getName().equals(fields[j].getName())) {
|
|
|
+ continue next;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fields = Arrays.copyOf(fields, fields.length + 1);
|
|
|
+ fields[fields.length - 1] = fieldss[i];
|
|
|
+ }
|
|
|
+ return fields;
|
|
|
+ }
|
|
|
+ return new Field[] {};
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("rawtypes")
|
|
|
+ public static void export2Excel(HttpServletResponse response, String title, String fileName, String downName,
|
|
|
+ int index, List list) throws Exception {
|
|
|
+ downName = URLEncoder.encode(downName, "UTF-8");
|
|
|
+ response.setContentType("application/msexcel; charset=utf-8");
|
|
|
+ response.setHeader("Content-disposition", "attachment; filename=" + downName);
|
|
|
+ InputStream is = null;
|
|
|
+ HSSFWorkbook workbook2007 = null;
|
|
|
+ HSSFSheet sheet = null;
|
|
|
+ OutputStream bos = null;
|
|
|
+ try {
|
|
|
+ String realpath = File.separator + "template" + File.separator + fileName;
|
|
|
+ String path = CommonUtil.class.getClassLoader().getResource("").getPath();
|
|
|
+ File fi = new File(path + realpath);
|
|
|
+ POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fi));
|
|
|
+ workbook2007 = new HSSFWorkbook(fs);
|
|
|
+ sheet = workbook2007.getSheetAt(0);
|
|
|
+ if (StringUtils.isNotBlank(title)) {
|
|
|
+ sheet.getRow(0).getCell(0).setCellValue(title);
|
|
|
+ }
|
|
|
+ setListInfo2(sheet, index, list);
|
|
|
+ bos = response.getOutputStream();
|
|
|
+ workbook2007.write(bos);
|
|
|
+ response.flushBuffer();
|
|
|
+ bos.flush();
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ try {
|
|
|
+ if (is != null) {
|
|
|
+ is.close();
|
|
|
+ }
|
|
|
+ if (bos != null) {
|
|
|
+ bos.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings({ "rawtypes" })
|
|
|
+ private static void setListInfo2(HSSFSheet sheet, int index, List list) {
|
|
|
+ // 得到列表行和样式
|
|
|
+ HSSFRow modRow = sheet.getRow(index + 1);
|
|
|
+ sheet.removeRow(modRow);
|
|
|
+
|
|
|
+ Map<String, Map<String, Integer>> cellKeyMap = null;
|
|
|
+ // 填充数据
|
|
|
+ if (null != list && list.size() > 0) {
|
|
|
+ cellKeyMap = getCellKeyMapByRow(modRow);
|
|
|
+ Iterator<String> it = null;
|
|
|
+ // 循环列表,为每一行赋值
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ Object o = list.get(i);
|
|
|
+ it = cellKeyMap.keySet().iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ String key = (String) it.next();
|
|
|
+ Map<String, Integer> cellPosMap = cellKeyMap.get(key);
|
|
|
+ // 获取行号
|
|
|
+ Integer rowIndex = Integer.parseInt(cellPosMap.get("rowIndex").toString());
|
|
|
+ // 获取列号
|
|
|
+ Integer colIndex = Integer.parseInt(cellPosMap.get("colIndex").toString());
|
|
|
+ rowIndex = rowIndex + i;
|
|
|
+ // 设置单元内容
|
|
|
+ if ("cout".equals(key)) {
|
|
|
+ setCellValue(sheet, rowIndex, colIndex, String.valueOf(i + 1));
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ String value = (String) getFieldValueByName(key, o);
|
|
|
+ setCellValue(sheet, rowIndex, colIndex, value);
|
|
|
+ }
|
|
|
+ // 设置单元格样式
|
|
|
+ CellStyle style = modRow.getCell(colIndex).getCellStyle();
|
|
|
+ setCellStyle(sheet, rowIndex, colIndex, style);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 仅适用MhzltszyjdCtl
|
|
|
+ * @param sheet
|
|
|
+ * @param star
|
|
|
+ * @param end
|
|
|
+ * @param list
|
|
|
+ */
|
|
|
+ @SuppressWarnings({ "rawtypes" })
|
|
|
+ public static void entity2Cell(HSSFSheet sheet, int star, int end, List list) {
|
|
|
+ if (null == list || list.size() == 0) {
|
|
|
+ for(int i=0;i<end-star;i++){
|
|
|
+ HSSFRow modRow = sheet.getRow(star + i + 1);
|
|
|
+ sheet.removeRow(modRow);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(null != list && list.size() > 0) {
|
|
|
+ List mapList = reflectForgetData(list.get(0).getClass(), list);
|
|
|
+ int a = star;
|
|
|
+ int b = star + 4;
|
|
|
+ for(int m=0;m<mapList.size()-1;m++){
|
|
|
+ copyRow(sheet, a, b);
|
|
|
+ a = b;
|
|
|
+ b = b + 4;
|
|
|
+ }
|
|
|
+ setCellValue(sheet,star,end,mapList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void copyRow(HSSFSheet sheet, int star, int end){
|
|
|
+ for(int i=0;i<end-star;i++){
|
|
|
+ HSSFRow oldRow = sheet.getRow(star+1+i);
|
|
|
+ HSSFRow newRow = sheet.createRow(end+1+i);
|
|
|
+ newRow.setHeight(oldRow.getHeight());
|
|
|
+ for (int k = 0; k < oldRow.getLastCellNum(); k++) {
|
|
|
+ HSSFCell oldCell = oldRow.getCell(k);
|
|
|
+ HSSFCell newcell = newRow.createCell(k);
|
|
|
+ if (null == oldCell) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ newcell.setCellStyle(oldCell.getCellStyle());
|
|
|
+ newcell.setCellType(oldCell.getCellType());
|
|
|
+ if (null != oldCell.getStringCellValue() && !"".equals(oldCell.getStringCellValue().trim())) {
|
|
|
+ newcell.setCellValue(oldCell.getStringCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(end+1, end+end-star, 0, 0));
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(end+1, end+end-star, 1, 1));
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(end+1, end+end-star, 2, 2));
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(end+1, end+end-star, 3, 3));
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(end+1, end+end-star, 8, 8));
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(end+1, end+end-star, 9, 9));
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(end+1, end+end-star, 10, 10));
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(end+1, end+end-star, 11, 11));
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(end+1, end+end-star, 12, 12));
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings({ "rawtypes", "unchecked" })
|
|
|
+ public static void setCellValue(HSSFSheet sheet, int star, int end, List list){
|
|
|
+ int cellIndex = star+1;
|
|
|
+ Map<String, Object> map = null;
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ map = (Map<String, Object>) list.get(i);
|
|
|
+ for(int m=0;m<end-star;m++){
|
|
|
+ HSSFRow row = sheet.getRow(cellIndex+m);
|
|
|
+ if(row == null) {
|
|
|
+ continue;
|
|
|
+ }else{
|
|
|
+ for (int j = 0; j < row.getLastCellNum(); j++) {
|
|
|
+ HSSFCell cell = row.getCell(j);
|
|
|
+ if (cell == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (map.containsKey(cell.getStringCellValue())) {
|
|
|
+ if (map.get(cell.getStringCellValue()) != null) {
|
|
|
+ cell.setCellValue(String.valueOf(map.get(cell.getStringCellValue())));
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ cell.setCellValue("");
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ cell.setCellValue("");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ cellIndex+=end-star;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void removeMergedRegion(HSSFSheet sheet, CellRangeAddress ca) {
|
|
|
+ int n = sheet.getNumMergedRegions();
|
|
|
+ for(int i=0;i<n;i++){
|
|
|
+ CellRangeAddress ca1 = sheet.getMergedRegion(i);
|
|
|
+ if(ca1!=null&&ca1.equals(ca)){
|
|
|
+ sheet.removeMergedRegion(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static CellRangeAddress getMergedRegion(HSSFSheet sheet, String cellValue){
|
|
|
+ int n = sheet.getNumMergedRegions();
|
|
|
+ for(int i=0;i<n;i++){
|
|
|
+ CellRangeAddress ca = sheet.getMergedRegion(i);
|
|
|
+ int firstRow = ca.getFirstRow();
|
|
|
+ int firstCel = ca.getFirstColumn();
|
|
|
+ HSSFCell cell = sheet.getRow(firstRow).getCell(firstCel);
|
|
|
+ cell.setCellType(Cell.CELL_TYPE_STRING);
|
|
|
+ String value = cell.getStringCellValue();
|
|
|
+ if(StringUtils.isNotBlank(value)&&value.equals(cellValue)){
|
|
|
+ return ca;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("rawtypes")
|
|
|
+ private static void copyRowOnCellAddress(HSSFSheet sheet, int star, int cellNum, List list){
|
|
|
+ for(int i=0;i<list.size()-1;i++){
|
|
|
+ HSSFRow oldRow = sheet.getRow(star);
|
|
|
+ sheet.shiftRows(star+i+1, sheet.getLastRowNum(), 1, true, false);
|
|
|
+ HSSFRow newRow = sheet.createRow(star+i+1);
|
|
|
+ newRow.setHeight(oldRow.getHeight());
|
|
|
+ for(int j=0;j<oldRow.getLastCellNum();j++){
|
|
|
+ HSSFCell oldCell = oldRow.getCell(j);
|
|
|
+ if(oldCell==null){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ HSSFCell newCell = newRow.createCell(j);
|
|
|
+ newCell.setCellStyle(oldCell.getCellStyle());
|
|
|
+ newCell.setCellType(oldCell.getCellType());
|
|
|
+ if(StringUtils.isNotBlank(oldCell.getStringCellValue())&&j>=cellNum){
|
|
|
+ newCell.setCellValue(oldCell.getStringCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings({ "rawtypes" })
|
|
|
+ public static void setCellValue(HSSFSheet sheet, CellRangeAddress ca, List list ,String keyPound){
|
|
|
+ int star = ca.getFirstRow()+1;
|
|
|
+ String cv = sheet.getRow(star).getCell(1).getStringCellValue();
|
|
|
+ while(StringUtils.isNotBlank(cv)&&!cv.startsWith(keyPound)){
|
|
|
+ star+=1;
|
|
|
+ cv = sheet.getRow(star).getCell(1).getStringCellValue();
|
|
|
+ }
|
|
|
+ int end = star + list.size();
|
|
|
+ copyRowOnCellAddress(sheet, star, 1, list);
|
|
|
+ HSSFCell c = sheet.getRow(ca.getFirstRow()).getCell(ca.getFirstColumn());
|
|
|
+ HSSFCellStyle style = c.getCellStyle();
|
|
|
+ removeMergedRegion(sheet, ca);
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(star-1, ca.getLastRow()+list.size()-1, ca.getFirstColumn(), ca.getLastColumn()));
|
|
|
+ c.setCellStyle(style);
|
|
|
+ List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
|
|
|
+ if(list!=null&&list.size()>0){
|
|
|
+ mapList = reflectForgetData(list.get(0).getClass(), list);
|
|
|
+ }
|
|
|
+ Map<String, Object> map = null;
|
|
|
+ int sum = 0;
|
|
|
+ for(int i=0;i<mapList.size();i++){
|
|
|
+ map = mapList.get(i);
|
|
|
+ HSSFRow row = sheet.getRow(star+i);
|
|
|
+ if(row==null){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ for(int j=1;j<row.getLastCellNum();j++){
|
|
|
+ HSSFCell cell = row.getCell(j);
|
|
|
+ String value = cell.getStringCellValue().trim();
|
|
|
+ if(StringUtils.isBlank(value)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if(!value.startsWith(keyPound)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ value = value.substring(1,value.length());
|
|
|
+ if(map.containsKey(value)){
|
|
|
+ String v = String.valueOf(map.get(value)).trim();
|
|
|
+ if(value.endsWith("sl")){
|
|
|
+ int num = 0;
|
|
|
+ try {
|
|
|
+ num = Integer.parseInt(v);
|
|
|
+ } catch (Exception e) {
|
|
|
+ num = 0;
|
|
|
+ v="0";
|
|
|
+ }
|
|
|
+ sum += num;
|
|
|
+ }
|
|
|
+ if(StringUtils.isNotBlank(v)){
|
|
|
+ cell.setCellType(HSSFCell.CELL_TYPE_STRING);
|
|
|
+ cell.setCellValue(v);
|
|
|
+ }else{
|
|
|
+ cell.setCellType(HSSFCell.CELL_TYPE_STRING);
|
|
|
+ cell.setCellValue("");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sheet.getRow(end).getCell(2).setCellType(HSSFCell.CELL_TYPE_STRING);
|
|
|
+ sheet.getRow(end).getCell(2).setCellValue(sum);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param sheet
|
|
|
+ * @param star 开始复制行的下标
|
|
|
+ * @param end 结束复制行的下标
|
|
|
+ */
|
|
|
+ public static void copyMoreRow(HSSFSheet sheet, int star, int end){
|
|
|
+ for(int i=0;i<end-star+1;i++){
|
|
|
+ HSSFRow oldRow = sheet.getRow(star+i);
|
|
|
+ sheet.shiftRows(end+1+i, sheet.getLastRowNum(), 1, true, false);
|
|
|
+ HSSFRow newRow = sheet.createRow(end+1+i);
|
|
|
+ newRow.setHeight(oldRow.getHeight());
|
|
|
+ for(int j=1;j<oldRow.getLastCellNum();j++){
|
|
|
+ HSSFCell oldCell = oldRow.getCell(j);
|
|
|
+ HSSFCell newCell = newRow.createCell(j);
|
|
|
+ newCell.setCellStyle(oldCell.getCellStyle());
|
|
|
+ newCell.setCellType(oldCell.getCellType());
|
|
|
+ if(StringUtils.isNotBlank(oldCell.getStringCellValue())){
|
|
|
+ newCell.setCellValue(oldCell.getStringCellValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 仅适用航空公司统计
|
|
|
+ * @param sheet
|
|
|
+ * @param ca
|
|
|
+ * @param map
|
|
|
+ */
|
|
|
+ public static void setCellValue(HSSFSheet sheet, CellRangeAddress ca, Map<String, List<Map<String, String>>> map){
|
|
|
+ if(map==null||map.size()==0){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ int star = ca.getFirstRow()+1;
|
|
|
+ int end = ca.getLastRow()-1;
|
|
|
+ int a = star;
|
|
|
+ int b = end;
|
|
|
+ int sum = 0;
|
|
|
+ int sumRow = ca.getFirstRow()+map.size();
|
|
|
+ for(int i=0;i<map.size()-1;i++){
|
|
|
+ copyMoreRow(sheet, a, b);
|
|
|
+ a = b+1;
|
|
|
+ b = b + 2;
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(a, b, 1, 1));
|
|
|
+ }
|
|
|
+ Iterator<Entry<String, List<Map<String, String>>>> it = map.entrySet().iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ int num = 0;
|
|
|
+ Entry<String, List<Map<String, String>>> e = it.next();
|
|
|
+ String jx = e.getKey();
|
|
|
+ List<Map<String, String>> zjx = e.getValue();
|
|
|
+ sumRow += zjx.size();
|
|
|
+ if(zjx!=null&&zjx.size()>0){
|
|
|
+ sheet.getRow(star).getCell(1).setCellValue(jx);
|
|
|
+ if(zjx.size()>1){
|
|
|
+ copyRowOnCellAddress(sheet, star, 2, zjx);
|
|
|
+ for(int i=0;i<zjx.size();i++){
|
|
|
+ Map<String, String> map2 = zjx.get(i);
|
|
|
+ Iterator<Entry<String, String>> it2 = map2.entrySet().iterator();
|
|
|
+ while(it2.hasNext()){
|
|
|
+ Entry<String, String> e2 = it2.next();
|
|
|
+ sheet.getRow(star+i).getCell(2).setCellValue(e2.getKey());
|
|
|
+ int n = 0;
|
|
|
+ try {
|
|
|
+ n = Integer.parseInt(e2.getValue());
|
|
|
+ } catch (Exception e3) {
|
|
|
+ n = 0;
|
|
|
+ }
|
|
|
+ sheet.getRow(star+i).getCell(3).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
|
|
|
+ sheet.getRow(star+i).getCell(3).setCellValue(n);
|
|
|
+ num = num + n;
|
|
|
+ }
|
|
|
+ removeMergedRegion(sheet, getMergedRegion(sheet, jx));
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(star, star+zjx.size(), 1, 1));
|
|
|
+ }
|
|
|
+ sheet.getRow(star+zjx.size()).getCell(3).setCellValue(num);
|
|
|
+ }else{
|
|
|
+ Map<String, String> map2 = zjx.get(0);
|
|
|
+ Iterator<Entry<String, String>> it2 = map2.entrySet().iterator();
|
|
|
+ while(it2.hasNext()){
|
|
|
+ Entry<String, String> e2 = it2.next();
|
|
|
+ sheet.getRow(star).getCell(2).setCellValue(e2.getKey());
|
|
|
+ int n = 0;
|
|
|
+ try {
|
|
|
+ n = Integer.parseInt(e2.getValue());
|
|
|
+ } catch (Exception e3) {
|
|
|
+ n = 0;
|
|
|
+ }
|
|
|
+ sheet.getRow(star).getCell(3).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
|
|
|
+ sheet.getRow(star).getCell(3).setCellValue(n);
|
|
|
+ sheet.getRow(star+1).getCell(3).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
|
|
|
+ sheet.getRow(star+1).getCell(3).setCellValue(n);
|
|
|
+ num = num + n;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ sheet.getRow(star).getCell(1).setCellValue("");
|
|
|
+ sheet.getRow(star).getCell(2).setCellValue("");
|
|
|
+ sheet.getRow(star).getCell(3).setCellValue("");
|
|
|
+ sheet.getRow(star+1).getCell(3).setCellValue("");
|
|
|
+ }
|
|
|
+ sum += num;
|
|
|
+ star+=zjx.size()+1;
|
|
|
+ }
|
|
|
+ int index = ca.getFirstRow();
|
|
|
+ removeMergedRegion(sheet, ca);
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(index, sumRow+1, 0, 0));
|
|
|
+ sheet.getRow(sumRow+1).getCell(2).setCellValue(sum);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void export3Excel(HttpServletResponse response, String title, String fileName, String downName,
|
|
|
+ int index, List<Map<String, Object>> mapList) throws Exception {
|
|
|
+ downName = URLEncoder.encode(downName, "UTF-8");
|
|
|
+ response.setContentType("application/msexcel; charset=utf-8");
|
|
|
+ response.setHeader("Content-disposition", "attachment; filename=" + downName);
|
|
|
+ InputStream is = null;
|
|
|
+ HSSFWorkbook workbook2007 = null;
|
|
|
+ HSSFSheet sheet = null;
|
|
|
+ OutputStream bos = null;
|
|
|
+ try {
|
|
|
+ String realpath = File.separator + "template" + File.separator + fileName;
|
|
|
+ String path = CommonUtil.class.getClassLoader().getResource("").getPath();
|
|
|
+ File fi = new File(path + realpath);
|
|
|
+ POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fi));
|
|
|
+ workbook2007 = new HSSFWorkbook(fs);
|
|
|
+ sheet = workbook2007.getSheetAt(0);
|
|
|
+ if (StringUtils.isNotBlank(title)) {
|
|
|
+ sheet.getRow(0).getCell(0).setCellValue(title);
|
|
|
+ }
|
|
|
+ setListInfo3(sheet, index, mapList);
|
|
|
+ bos = response.getOutputStream();
|
|
|
+ workbook2007.write(bos);
|
|
|
+ response.flushBuffer();
|
|
|
+ bos.flush();
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ try {
|
|
|
+ if (is != null) {
|
|
|
+ is.close();
|
|
|
+ }
|
|
|
+ if (bos != null) {
|
|
|
+ bos.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void setListInfo3(HSSFSheet sheet, int index, List<Map<String, Object>> list) {
|
|
|
+ // 得到列表行和样式
|
|
|
+ HSSFRow modRow = sheet.getRow(index + 1);
|
|
|
+ //sheet.removeRow(modRow);
|
|
|
+ Map<String, Map<String, Integer>> cellKeyMap = null;
|
|
|
+ cellKeyMap = getCellKeyMapByRow(modRow);
|
|
|
+ int colNum = modRow.getLastCellNum();
|
|
|
+ for (int modIndex = 0; modIndex < colNum; modIndex++) {
|
|
|
+ setCellValue(sheet, index + 1, modIndex, "");
|
|
|
+ }
|
|
|
+ // 填充数据
|
|
|
+ if (null != list && list.size() > 0) {
|
|
|
+ Iterator<String> it = null;
|
|
|
+ // 循环列表,为每一行赋值
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ Map<String, Object> mapObj = list.get(i);
|
|
|
+ it = cellKeyMap.keySet().iterator();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ String key = (String) it.next();
|
|
|
+ Map<String, Integer> cellPosMap = cellKeyMap.get(key);
|
|
|
+ // 获取行号
|
|
|
+ Integer rowIndex = Integer.parseInt(cellPosMap.get("rowIndex").toString());
|
|
|
+ // 获取列号
|
|
|
+ Integer colIndex = Integer.parseInt(cellPosMap.get("colIndex").toString());
|
|
|
+ rowIndex = rowIndex + i;
|
|
|
+ // 设置单元内容
|
|
|
+ if ("cout".equals(key)) {
|
|
|
+ setCellValue(sheet, rowIndex, colIndex, String.valueOf(i + 1));
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ if (mapObj.containsKey(key))
|
|
|
+ ;
|
|
|
+ {
|
|
|
+ if (null != mapObj.get(key)) {
|
|
|
+ String value = mapObj.get(key).toString();
|
|
|
+ setCellValue(sheet, rowIndex, colIndex, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 设置单元格样式
|
|
|
+ CellStyle style = modRow.getCell(colIndex).getCellStyle();
|
|
|
+ setCellStyle(sheet, rowIndex, colIndex, style);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String addIntObj(Object... objects) {
|
|
|
+ Integer cntInt = 0;
|
|
|
+ for (Object obj : objects) {
|
|
|
+ if (null != obj && !StringUtils.isEmpty(obj.toString())) {
|
|
|
+ if(isNumeric(obj)) {
|
|
|
+ cntInt += Integer.parseInt(obj.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(cntInt == 0) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return String.valueOf(cntInt);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否为数字
|
|
|
+ * @param str
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean isNumeric(Object obj) {
|
|
|
+ if(null == obj || StringUtils.isEmpty(obj.toString())) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ String str =obj.toString();
|
|
|
+ for (int i = str.length(); --i >= 0;) {
|
|
|
+ if (!Character.isDigit(str.charAt(i))) { return false; }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sheet复制
|
|
|
+ *
|
|
|
+ * @param fromSheet
|
|
|
+ * @param toSheet
|
|
|
+ * @param copyValueFlag
|
|
|
+ */
|
|
|
+ @SuppressWarnings("rawtypes")
|
|
|
+ public static void copySheet(HSSFWorkbook wb, HSSFSheet fromSheet,
|
|
|
+ HSSFSheet toSheet, boolean copyValueFlag) {
|
|
|
+ // 合并区域处理
|
|
|
+ mergerRegion(fromSheet, toSheet);
|
|
|
+ for (Iterator rowIt = fromSheet.rowIterator(); rowIt.hasNext();) {
|
|
|
+ HSSFRow tmpRow = (HSSFRow) rowIt.next();
|
|
|
+ HSSFRow newRow = toSheet.createRow(tmpRow.getRowNum());
|
|
|
+ newRow.setHeight(tmpRow.getHeight());
|
|
|
+ // 行复制
|
|
|
+ copyRow(wb, tmpRow, newRow, copyValueFlag);
|
|
|
+ }
|
|
|
+ //copy列宽
|
|
|
+ int cellCol = fromSheet.getRow(0).getPhysicalNumberOfCells();
|
|
|
+ for(int i=0;i<cellCol;i++){
|
|
|
+ toSheet.setColumnWidth(i, fromSheet.getColumnWidth(i));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 行复制功能
|
|
|
+ *
|
|
|
+ * @param fromRow
|
|
|
+ * @param toRow
|
|
|
+ */
|
|
|
+ @SuppressWarnings("rawtypes")
|
|
|
+ public static void copyRow(HSSFWorkbook wb, HSSFRow fromRow, HSSFRow toRow,
|
|
|
+ boolean copyValueFlag) {
|
|
|
+ for (Iterator cellIt = fromRow.cellIterator(); cellIt.hasNext();) {
|
|
|
+ HSSFCell tmpCell = (HSSFCell) cellIt.next();
|
|
|
+ HSSFCell newCell = toRow.createCell(tmpCell.getColumnIndex());
|
|
|
+ copyCell(wb, tmpCell, newCell, copyValueFlag);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 复制原有sheet的合并单元格到新创建的sheet
|
|
|
+ *
|
|
|
+ * @param sheetCreat
|
|
|
+ * 新创建sheet
|
|
|
+ * @param sheet
|
|
|
+ * 原有的sheet
|
|
|
+ */
|
|
|
+ public static void mergerRegion(HSSFSheet fromSheet, HSSFSheet toSheet) {
|
|
|
+ int sheetMergerCount = fromSheet.getNumMergedRegions();
|
|
|
+ for (int i = 0; i < sheetMergerCount; i++) {
|
|
|
+ CellRangeAddress ca = fromSheet.getMergedRegion(i);
|
|
|
+ toSheet.addMergedRegion(ca);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 复制单元格
|
|
|
+ *
|
|
|
+ * @param srcCell
|
|
|
+ * @param distCell
|
|
|
+ * @param copyValueFlag
|
|
|
+ * true则连同cell的内容一起复制
|
|
|
+ */
|
|
|
+ public static void copyCell(HSSFWorkbook wb, HSSFCell srcCell,
|
|
|
+ HSSFCell distCell, boolean copyValueFlag) {
|
|
|
+ //HSSFCellStyle newstyle = wb.createCellStyle();
|
|
|
+ //copyCellStyle(srcCell.getCellStyle(), newstyle);
|
|
|
+
|
|
|
+ // 样式
|
|
|
+ distCell.setCellStyle(srcCell.getCellStyle());
|
|
|
+ // 评论
|
|
|
+ if (srcCell.getCellComment() != null) {
|
|
|
+ distCell.setCellComment(srcCell.getCellComment());
|
|
|
+ }
|
|
|
+ // 不同数据类型处理
|
|
|
+ int srcCellType = srcCell.getCellType();
|
|
|
+ distCell.setCellType(srcCellType);
|
|
|
+ if (copyValueFlag) {
|
|
|
+ if (srcCellType == HSSFCell.CELL_TYPE_NUMERIC) {
|
|
|
+ if (HSSFDateUtil.isCellDateFormatted(srcCell)) {
|
|
|
+ distCell.setCellValue(srcCell.getDateCellValue());
|
|
|
+ } else {
|
|
|
+ distCell.setCellValue(srcCell.getNumericCellValue());
|
|
|
+ }
|
|
|
+ } else if (srcCellType == HSSFCell.CELL_TYPE_STRING) {
|
|
|
+ distCell.setCellValue(srcCell.getRichStringCellValue());
|
|
|
+ } else if (srcCellType == HSSFCell.CELL_TYPE_BLANK) {
|
|
|
+ // nothing21
|
|
|
+ } else if (srcCellType == HSSFCell.CELL_TYPE_BOOLEAN) {
|
|
|
+ distCell.setCellValue(srcCell.getBooleanCellValue());
|
|
|
+ } else if (srcCellType == HSSFCell.CELL_TYPE_ERROR) {
|
|
|
+ distCell.setCellErrorValue(srcCell.getErrorCellValue());
|
|
|
+ } else if (srcCellType == HSSFCell.CELL_TYPE_FORMULA) {
|
|
|
+ distCell.setCellFormula(srcCell.getCellFormula());
|
|
|
+ } else { // nothing29
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 对象转json
|
|
|
+ *
|
|
|
+ * @param list
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String convertToJsonData(Object list) {
|
|
|
+ try {
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ mapper.setDateFormat(new SimpleDateFormat(DateTime.DATE_FORMAT_DATEONLY));
|
|
|
+ String liststr = mapper.writeValueAsString(list);
|
|
|
+ return liststr;
|
|
|
+ }
|
|
|
+ catch (JsonProcessingException jsone) {
|
|
|
+ throw new BizException(jsone);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|