|
@@ -0,0 +1,634 @@
|
|
|
|
+package com.ym.mec.util.excel;
|
|
|
|
+
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileInputStream;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
|
+import java.security.cert.CertificateException;
|
|
|
|
+import java.security.cert.X509Certificate;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Iterator;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.Map.Entry;
|
|
|
|
+
|
|
|
|
+import org.apache.commons.beanutils.ConvertUtils;
|
|
|
|
+import org.apache.commons.beanutils.NestedNullException;
|
|
|
|
+import org.apache.commons.beanutils.PropertyUtils;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
|
+import org.apache.http.NameValuePair;
|
|
|
|
+import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
|
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
|
|
+import org.apache.http.impl.client.BasicCookieStore;
|
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
|
+import org.apache.http.message.BasicNameValuePair;
|
|
|
|
+import org.apache.http.ssl.SSLContextBuilder;
|
|
|
|
+import org.apache.http.ssl.TrustStrategy;
|
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFCell;
|
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFCellStyle;
|
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFFont;
|
|
|
|
+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.ss.usermodel.Cell;
|
|
|
|
+import org.apache.poi.ss.usermodel.CellStyle;
|
|
|
|
+import org.apache.poi.ss.usermodel.Font;
|
|
|
|
+import org.apache.poi.ss.usermodel.Row;
|
|
|
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
|
+import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+
|
|
|
|
+import com.ym.mec.util.exception.UtilException;
|
|
|
|
+
|
|
|
|
+public class POIUtil {
|
|
|
|
+
|
|
|
|
+ private final static Logger LOGGER = LoggerFactory.getLogger(POIUtil.class);
|
|
|
|
+
|
|
|
|
+ // 能导出的最大数据条数
|
|
|
|
+ private final static int MAX_DATA_SIZE = 50000;
|
|
|
|
+
|
|
|
|
+ private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 将数据集dataset导出到fileName文件中(只支持.xlsx格式)
|
|
|
|
+ * @param headColumns 导出文件的列名
|
|
|
|
+ * @param dataset 数据源
|
|
|
|
+ * @return
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ public static SXSSFWorkbook exportBigExcel(String[] headColumns, List<Map<String, Object>> dataset) throws IOException {
|
|
|
|
+ if (headColumns == null) {
|
|
|
|
+ throw new UtilException("excel列名不能为空");
|
|
|
|
+ }
|
|
|
|
+ if (dataset == null) {
|
|
|
|
+ throw new UtilException("数据集不能为空");
|
|
|
|
+ }
|
|
|
|
+ SXSSFWorkbook workbook = new SXSSFWorkbook();
|
|
|
|
+ CellStyle style = workbook.createCellStyle(); // 获取单元格样式
|
|
|
|
+ /************** 设置单元格样式 *************/
|
|
|
|
+ style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直
|
|
|
|
+ style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平
|
|
|
|
+ style.setWrapText(true);
|
|
|
|
+ Font font = workbook.createFont();
|
|
|
|
+ font.setColor(HSSFFont.COLOR_NORMAL);
|
|
|
|
+ font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
|
|
|
|
+ style.setFont(font);
|
|
|
|
+
|
|
|
|
+ Sheet sheet = workbook.createSheet(); // 创建sheet
|
|
|
|
+ // 设置表头
|
|
|
|
+ // 创建第一行
|
|
|
|
+ Row row1 = sheet.createRow(0);
|
|
|
|
+ Cell cell = null;
|
|
|
|
+ for (int i = 0; i < headColumns.length; i++) {
|
|
|
|
+ // 创建列
|
|
|
|
+ cell = row1.createCell(i);
|
|
|
|
+ // 定义单元格为字符串类型
|
|
|
|
+ cell.setCellType(HSSFCell.CELL_TYPE_STRING);
|
|
|
|
+ // 设置单元格的样式
|
|
|
|
+ cell.setCellStyle(style);
|
|
|
|
+ // 设置单元格的值
|
|
|
|
+ cell.setCellValue(headColumns[i]);
|
|
|
|
+ }
|
|
|
|
+ Row row = null;
|
|
|
|
+ // 添加数据
|
|
|
|
+ Map<String, Object> data = null;
|
|
|
|
+ for (int i = 1; i <= dataset.size(); i++) {
|
|
|
|
+ data = dataset.get(i - 1);
|
|
|
|
+ if (data != null) {
|
|
|
|
+ row = sheet.createRow(i);
|
|
|
|
+ int index = 0;
|
|
|
|
+ for (Entry<String, Object> entry : data.entrySet()) {
|
|
|
|
+ row.createCell(index).setCellValue(entry.getValue().toString());
|
|
|
|
+ index++;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return workbook;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 将数据集dataset导出到fileName文件中
|
|
|
|
+ * @param headColumns 导出文件的列名
|
|
|
|
+ * @param dataset 数据源
|
|
|
|
+ * @return
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ public static HSSFWorkbook exportExcel(String[] headColumns, List<Map<String, Object>> dataset) throws IOException {
|
|
|
|
+ if (headColumns == null) {
|
|
|
|
+ throw new UtilException("excel列名不能为空");
|
|
|
|
+ }
|
|
|
|
+ if (dataset == null) {
|
|
|
|
+ throw new UtilException("数据集不能为空");
|
|
|
|
+ }
|
|
|
|
+ if (dataset.size() > MAX_DATA_SIZE) {
|
|
|
|
+ throw new UtilException("数据集太大,不能导出.最大数据集不能超过" + MAX_DATA_SIZE);
|
|
|
|
+ }
|
|
|
|
+ HSSFWorkbook workbook = new HSSFWorkbook();
|
|
|
|
+ HSSFCellStyle style = workbook.createCellStyle(); // 获取单元格样式
|
|
|
|
+ /************** 设置单元格样式 *************/
|
|
|
|
+ style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直
|
|
|
|
+ style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平
|
|
|
|
+ style.setWrapText(true);
|
|
|
|
+ HSSFFont font = workbook.createFont();
|
|
|
|
+ font.setColor(HSSFFont.COLOR_NORMAL);
|
|
|
|
+ font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
|
|
|
|
+ style.setFont(font);
|
|
|
|
+
|
|
|
|
+ HSSFSheet sheet = workbook.createSheet(); // 创建sheet
|
|
|
|
+ // 设置表头
|
|
|
|
+ // 创建第一行
|
|
|
|
+ HSSFRow row1 = sheet.createRow(0);
|
|
|
|
+ HSSFCell cell = null;
|
|
|
|
+ for (int i = 0; i < headColumns.length; i++) {
|
|
|
|
+ // 创建列
|
|
|
|
+ cell = row1.createCell(i);
|
|
|
|
+ // 定义单元格为字符串类型
|
|
|
|
+ cell.setCellType(HSSFCell.CELL_TYPE_STRING);
|
|
|
|
+ // 设置单元格的样式
|
|
|
|
+ cell.setCellStyle(style);
|
|
|
|
+ // 设置单元格的值
|
|
|
|
+ cell.setCellValue(headColumns[i]);
|
|
|
|
+ }
|
|
|
|
+ HSSFRow row = null;
|
|
|
|
+ // 添加数据
|
|
|
|
+ Map<String, Object> data = null;
|
|
|
|
+ for (int i = 1; i <= dataset.size(); i++) {
|
|
|
|
+ data = dataset.get(i - 1);
|
|
|
|
+ if (data != null) {
|
|
|
|
+ row = sheet.createRow(i);
|
|
|
|
+ int index = 0;
|
|
|
|
+ for (Entry<String, Object> entry : data.entrySet()) {
|
|
|
|
+ row.createCell(index).setCellValue(entry.getValue().toString());
|
|
|
|
+ index++;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return workbook;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 将数据集dataset导出到fileName文件中
|
|
|
|
+ * @param headColumns 导出文件的列名
|
|
|
|
+ * @param fieldColumns
|
|
|
|
+ * @param dataset 数据源
|
|
|
|
+ * @return
|
|
|
|
+ * @throws IOException
|
|
|
|
+ * @throws NoSuchMethodException
|
|
|
|
+ * @throws InvocationTargetException
|
|
|
|
+ * @throws IllegalAccessException
|
|
|
|
+ */
|
|
|
|
+ public static <T> HSSFWorkbook exportExcel(String[] headColumns, String[] fieldColumns, List<T> dataset) throws IOException, IllegalAccessException,
|
|
|
|
+ InvocationTargetException, NoSuchMethodException {
|
|
|
|
+ if (headColumns == null) {
|
|
|
|
+ throw new UtilException("excel列名不能为空");
|
|
|
|
+ }
|
|
|
|
+ if (dataset == null) {
|
|
|
|
+ throw new UtilException("数据集不能为空");
|
|
|
|
+ }
|
|
|
|
+ if (dataset.size() > MAX_DATA_SIZE) {
|
|
|
|
+ throw new UtilException("数据集太大,不能导出.最大数据集不能超过" + MAX_DATA_SIZE);
|
|
|
|
+ }
|
|
|
|
+ HSSFWorkbook workbook = new HSSFWorkbook();
|
|
|
|
+ HSSFCellStyle style = workbook.createCellStyle(); // 获取单元格样式
|
|
|
|
+ /************** 设置单元格样式 *************/
|
|
|
|
+ style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直
|
|
|
|
+ style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平
|
|
|
|
+ style.setWrapText(true);
|
|
|
|
+ HSSFFont font = workbook.createFont();
|
|
|
|
+ font.setColor(HSSFFont.COLOR_NORMAL);
|
|
|
|
+ font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
|
|
|
|
+ style.setFont(font);
|
|
|
|
+
|
|
|
|
+ HSSFSheet sheet = workbook.createSheet(); // 创建sheet
|
|
|
|
+ // 设置表头
|
|
|
|
+ // 创建第一行
|
|
|
|
+ HSSFRow row1 = sheet.createRow(0);
|
|
|
|
+ HSSFCell cell = null;
|
|
|
|
+ for (int i = 0; i < headColumns.length; i++) {
|
|
|
|
+ // 创建列
|
|
|
|
+ cell = row1.createCell(i);
|
|
|
|
+ // 定义单元格为字符串类型
|
|
|
|
+ cell.setCellType(HSSFCell.CELL_TYPE_STRING);
|
|
|
|
+ // 设置单元格的样式
|
|
|
|
+ cell.setCellStyle(style);
|
|
|
|
+ // 设置单元格的值
|
|
|
|
+ cell.setCellValue(headColumns[i]);
|
|
|
|
+ }
|
|
|
|
+ HSSFRow row = null;
|
|
|
|
+ // 添加数据
|
|
|
|
+ T data = null;
|
|
|
|
+ Object obj = null;
|
|
|
|
+ for (int i = 1; i <= dataset.size(); i++) {
|
|
|
|
+ data = dataset.get(i - 1);
|
|
|
|
+ if (data != null) {
|
|
|
|
+ row = sheet.createRow(i);
|
|
|
|
+ for (int j = 0; j < fieldColumns.length; j++) {
|
|
|
|
+ cell = row.createCell(j);
|
|
|
|
+ try {
|
|
|
|
+ obj = PropertyUtils.getNestedProperty(data, fieldColumns[j]);
|
|
|
|
+ if (obj instanceof Date) {
|
|
|
|
+ obj = sdf.format(obj);
|
|
|
|
+ }
|
|
|
|
+ } catch (NestedNullException e) {
|
|
|
|
+ LOGGER.warn(e.getMessage());
|
|
|
|
+ obj = null;
|
|
|
|
+ }
|
|
|
|
+ if (obj != null) {
|
|
|
|
+ cell.setCellValue(obj.toString());
|
|
|
|
+ } else {
|
|
|
|
+ cell.setCellValue("");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return workbook;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 将数据集dataset导出到fileName文件中(只支持.xlsx格式)
|
|
|
|
+ * @param headColumns 导出文件的列名
|
|
|
|
+ * @param fieldColumns
|
|
|
|
+ * @param dataset 数据源
|
|
|
|
+ * @return
|
|
|
|
+ * @throws IOException
|
|
|
|
+ * @throws NoSuchMethodException
|
|
|
|
+ * @throws InvocationTargetException
|
|
|
|
+ * @throws IllegalAccessException
|
|
|
|
+ */
|
|
|
|
+ public static <T> SXSSFWorkbook exportBigExcel(String[] headColumns, String[] fieldColumns, List<T> dataset) throws IOException, IllegalAccessException,
|
|
|
|
+ InvocationTargetException, NoSuchMethodException {
|
|
|
|
+ if (headColumns == null) {
|
|
|
|
+ throw new UtilException("excel列名不能为空");
|
|
|
|
+ }
|
|
|
|
+ if (dataset == null) {
|
|
|
|
+ throw new UtilException("数据集不能为空");
|
|
|
|
+ }
|
|
|
|
+ SXSSFWorkbook workbook = new SXSSFWorkbook();
|
|
|
|
+ CellStyle style = workbook.createCellStyle(); // 获取单元格样式
|
|
|
|
+ /************** 设置单元格样式 *************/
|
|
|
|
+ style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直
|
|
|
|
+ style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平
|
|
|
|
+ style.setWrapText(true);
|
|
|
|
+ Font font = workbook.createFont();
|
|
|
|
+ font.setColor(HSSFFont.COLOR_NORMAL);
|
|
|
|
+ font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
|
|
|
|
+ style.setFont(font);
|
|
|
|
+
|
|
|
|
+ Sheet sheet = workbook.createSheet(); // 创建sheet
|
|
|
|
+ // 设置表头
|
|
|
|
+ // 创建第一行
|
|
|
|
+ Row row1 = sheet.createRow(0);
|
|
|
|
+ Cell cell = null;
|
|
|
|
+ for (int i = 0; i < headColumns.length; i++) {
|
|
|
|
+ // 创建列
|
|
|
|
+ cell = row1.createCell(i);
|
|
|
|
+ // 定义单元格为字符串类型
|
|
|
|
+ cell.setCellType(HSSFCell.CELL_TYPE_STRING);
|
|
|
|
+ // 设置单元格的样式
|
|
|
|
+ cell.setCellStyle(style);
|
|
|
|
+ // 设置单元格的值
|
|
|
|
+ cell.setCellValue(headColumns[i]);
|
|
|
|
+ }
|
|
|
|
+ Row row = null;
|
|
|
|
+ // 添加数据
|
|
|
|
+ T data = null;
|
|
|
|
+ Object obj = null;
|
|
|
|
+ for (int i = 1; i <= dataset.size(); i++) {
|
|
|
|
+ data = dataset.get(i - 1);
|
|
|
|
+ if (data != null) {
|
|
|
|
+ row = sheet.createRow(i);
|
|
|
|
+ for (int j = 0; j < fieldColumns.length; j++) {
|
|
|
|
+ cell = row.createCell(j);
|
|
|
|
+ try {
|
|
|
|
+ obj = PropertyUtils.getNestedProperty(data, fieldColumns[j]);
|
|
|
|
+ if (obj instanceof Date) {
|
|
|
|
+ obj = sdf.format(obj);
|
|
|
|
+ }
|
|
|
|
+ } catch (NestedNullException e) {
|
|
|
|
+ LOGGER.warn(e.getMessage());
|
|
|
|
+ obj = null;
|
|
|
|
+ }
|
|
|
|
+ if (obj != null) {
|
|
|
|
+ cell.setCellValue(obj.toString());
|
|
|
|
+ } else {
|
|
|
|
+ cell.setCellValue("");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return workbook;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 导入指定的excel文件
|
|
|
|
+ * @param excelFile excel文件
|
|
|
|
+ * @param startRowNum 从第几行数据开始导入
|
|
|
|
+ * @return
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ public static Map<String, List<Map<String, Object>>> importExcel(File excelFile, int startRowNum) throws IOException {
|
|
|
|
+ Map<String, List<Map<String, Object>>> result = new HashMap<String, List<Map<String, Object>>>();
|
|
|
|
+
|
|
|
|
+ if (!excelFile.exists()) {
|
|
|
|
+ LOGGER.error("文件" + excelFile.getAbsolutePath() + "不存在");
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ Workbook workbook = null;
|
|
|
|
+ FileInputStream fis = new FileInputStream(excelFile);
|
|
|
|
+ int index = excelFile.getName().lastIndexOf('.');
|
|
|
|
+ if (index <= 0) {
|
|
|
|
+ fis.close();
|
|
|
|
+ throw new UtilException("excel文件的扩展名是.xls or .xlsx!");
|
|
|
|
+ }
|
|
|
|
+ String ext = excelFile.getName().substring(index).toLowerCase();
|
|
|
|
+ try {
|
|
|
|
+ if (".xlsx".equals(ext.toLowerCase())) {
|
|
|
|
+ // 支持excel2007 xlsx格式
|
|
|
|
+ workbook = new XSSFWorkbook(fis);
|
|
|
|
+ } else if (".xls".equals(ext)) {
|
|
|
|
+ // 支持excel2003以前 xls格式
|
|
|
|
+ workbook = new HSSFWorkbook(fis);
|
|
|
|
+ } else {
|
|
|
|
+ throw new UtilException("excel文件的扩展名是.xls or .xlsx!");
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
+ LOGGER.error("excel open error.", ex);
|
|
|
|
+ return result;
|
|
|
|
+ } finally {
|
|
|
|
+ if (fis != null) {
|
|
|
|
+ fis.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ int sheetCount = workbook.getNumberOfSheets();
|
|
|
|
+ Sheet sheet = null;
|
|
|
|
+ Row row = null;
|
|
|
|
+ Cell cell = null;
|
|
|
|
+ int currentRowNum = 0, currentCellNum = 0;
|
|
|
|
+ // Object fieldValue = null;
|
|
|
|
+ Iterator<Row> rowIter = null;
|
|
|
|
+ Iterator<Cell> cellIter = null;
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < sheetCount; i++) {
|
|
|
|
+ String fieldsName[] = null;
|
|
|
|
+ // 行号清零
|
|
|
|
+ currentRowNum = 0;
|
|
|
|
+ // 顺序取sheet
|
|
|
|
+ sheet = workbook.getSheetAt(i);
|
|
|
|
+ List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
|
|
|
|
+ rowIter = sheet.iterator();
|
|
|
|
+ while (rowIter.hasNext()) {
|
|
|
|
+ Map<String, Object> obj = null;
|
|
|
|
+ // 获取当前行
|
|
|
|
+ row = rowIter.next();
|
|
|
|
+ if (row != null) {
|
|
|
|
+ currentRowNum++;
|
|
|
|
+
|
|
|
|
+ if (currentRowNum == 1) {// 第一列表示英文名称对应表字段
|
|
|
|
+ cellIter = row.iterator();
|
|
|
|
+ // 列号清零
|
|
|
|
+ currentCellNum = 0;
|
|
|
|
+ List<String> names = new ArrayList<String>();
|
|
|
|
+ while (cellIter.hasNext()) {
|
|
|
|
+ cell = cellIter.next();
|
|
|
|
+ names.add(cell.getStringCellValue());
|
|
|
|
+ }
|
|
|
|
+ fieldsName = names.toArray(new String[names.size()]);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 跳过指定的行
|
|
|
|
+ if (currentRowNum < startRowNum) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 实例化对象
|
|
|
|
+ obj = new HashMap<String, Object>();// clazz.newInstance();
|
|
|
|
+
|
|
|
|
+ cellIter = row.iterator();
|
|
|
|
+ // 列号清零
|
|
|
|
+ currentCellNum = 0;
|
|
|
|
+ while (cellIter.hasNext()) {
|
|
|
|
+ cell = cellIter.next();
|
|
|
|
+ cell.setCellType(Cell.CELL_TYPE_STRING);
|
|
|
|
+ String fieldValue = cell.getStringCellValue();
|
|
|
|
+ obj.put(fieldsName[currentCellNum], fieldValue);
|
|
|
|
+ currentCellNum++;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (obj != null)
|
|
|
|
+ datas.add(obj);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ if (!datas.isEmpty())
|
|
|
|
+ result.put(sheet.getSheetName(), datas);
|
|
|
|
+ }
|
|
|
|
+ if (workbook != null) {
|
|
|
|
+ workbook.close();
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 导入指定的excel文件
|
|
|
|
+ * @param inputStream excel文件流
|
|
|
|
+ * @param startRowNum 从第几行数据开始导入
|
|
|
|
+ * @param extName 文件扩展名,仅支持.xls 或 .xlsx
|
|
|
|
+ * @return
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ public static Map<String, List<Map<String, Object>>> importExcel(InputStream inputStream, int startRowNum, String extName) throws IOException {
|
|
|
|
+
|
|
|
|
+ Map<String, List<Map<String, Object>>> result = new HashMap<String, List<Map<String, Object>>>();
|
|
|
|
+
|
|
|
|
+ Workbook workbook = null;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ if (".xlsx".equals(extName.toLowerCase())) {
|
|
|
|
+ // 支持excel2007 xlsx格式
|
|
|
|
+ workbook = new XSSFWorkbook(inputStream);
|
|
|
|
+ } else if (".xls".equals(extName)) {
|
|
|
|
+ // 支持excel2003以前 xls格式
|
|
|
|
+ workbook = new HSSFWorkbook(inputStream);
|
|
|
|
+ } else {
|
|
|
|
+ throw new UtilException("excel文件的扩展名是.xls or .xlsx!");
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception ex) {
|
|
|
|
+ LOGGER.error("excel open error.", ex);
|
|
|
|
+ return result;
|
|
|
|
+ } finally {
|
|
|
|
+ if (inputStream != null) {
|
|
|
|
+ inputStream.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ int sheetCount = workbook.getNumberOfSheets();
|
|
|
|
+ Sheet sheet = null;
|
|
|
|
+ Row row = null;
|
|
|
|
+ Cell cell = null;
|
|
|
|
+ int currentRowNum = 0, currentCellNum = 0;
|
|
|
|
+ // Object fieldValue = null;
|
|
|
|
+ Iterator<Row> rowIter = null;
|
|
|
|
+ Iterator<Cell> cellIter = null;
|
|
|
|
+
|
|
|
|
+ for (int i = 0; i < sheetCount; i++) {
|
|
|
|
+ String fieldsName[] = null;
|
|
|
|
+ // 行号清零
|
|
|
|
+ currentRowNum = 0;
|
|
|
|
+ // 顺序取sheet
|
|
|
|
+ sheet = workbook.getSheetAt(i);
|
|
|
|
+ List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
|
|
|
|
+ rowIter = sheet.iterator();
|
|
|
|
+ while (rowIter.hasNext()) {
|
|
|
|
+ Map<String, Object> obj = null;
|
|
|
|
+ // 获取当前行
|
|
|
|
+ row = rowIter.next();
|
|
|
|
+ if (row != null) {
|
|
|
|
+ currentRowNum++;
|
|
|
|
+
|
|
|
|
+ if (currentRowNum == 1) {// 第一列表示英文名称对应表字段
|
|
|
|
+ cellIter = row.iterator();
|
|
|
|
+ // 列号清零
|
|
|
|
+ currentCellNum = 0;
|
|
|
|
+ List<String> names = new ArrayList<String>();
|
|
|
|
+ while (cellIter.hasNext()) {
|
|
|
|
+ cell = cellIter.next();
|
|
|
|
+ names.add(cell.getStringCellValue());
|
|
|
|
+ }
|
|
|
|
+ fieldsName = names.toArray(new String[names.size()]);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 跳过指定的行
|
|
|
|
+ if (currentRowNum < startRowNum) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 实例化对象
|
|
|
|
+ obj = new HashMap<String, Object>();// clazz.newInstance();
|
|
|
|
+
|
|
|
|
+ cellIter = row.iterator();
|
|
|
|
+ // 列号清零
|
|
|
|
+ currentCellNum = 0;
|
|
|
|
+ while (cellIter.hasNext()) {
|
|
|
|
+ cell = cellIter.next();
|
|
|
|
+ cell.setCellType(Cell.CELL_TYPE_STRING);
|
|
|
|
+ String fieldValue = cell.getStringCellValue();
|
|
|
|
+ obj.put(fieldsName[currentCellNum], fieldValue);
|
|
|
|
+ currentCellNum++;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (obj != null)
|
|
|
|
+ datas.add(obj);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ if (!datas.isEmpty())
|
|
|
|
+ result.put(sheet.getSheetName(), datas);
|
|
|
|
+ }
|
|
|
|
+ if (workbook != null) {
|
|
|
|
+ workbook.close();
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ File file = new File("e:/reward.xlsx");
|
|
|
|
+ String sheetName = "reward";
|
|
|
|
+ Map<String, List<Map<String, Object>>> map = null;
|
|
|
|
+ try {
|
|
|
|
+ map = POIUtil.importExcel(file, 1);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ if (map != null) {
|
|
|
|
+ // Map<String, Object> loginParam = new HashMap<String, Object>();
|
|
|
|
+
|
|
|
|
+ SSLContextBuilder builder = new SSLContextBuilder();
|
|
|
|
+ try {
|
|
|
|
+ builder.loadTrustMaterial(null, new TrustStrategy() {
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ HttpPost httpPost = null;
|
|
|
|
+ CloseableHttpClient httpClient = null;
|
|
|
|
+ try {
|
|
|
|
+ SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
|
|
|
|
+ httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultCookieStore(new BasicCookieStore()).build();
|
|
|
|
+ // 登陆
|
|
|
|
+ httpPost = new HttpPost("https://ehjinrong.com/manage/admin/login.do?userName=sadmin&password=Hello@ehjr&captcha=411049");
|
|
|
|
+
|
|
|
|
+ HttpResponse httpResponse = httpClient.execute(httpPost);
|
|
|
|
+ HttpEntity httpEntity = httpResponse.getEntity();
|
|
|
|
+ String str = EntityUtils.toString(httpEntity);
|
|
|
|
+ System.out.println("--------" + str);
|
|
|
|
+ EntityUtils.consume(httpEntity);
|
|
|
|
+
|
|
|
|
+ // 发放奖励
|
|
|
|
+ for (Map<String, Object> row : map.get(sheetName)) {
|
|
|
|
+ try {
|
|
|
|
+ Long arg0 = Long.parseLong(row.get("user_id_").toString());
|
|
|
|
+ double arg1 = Double.parseDouble(row.get("reward").toString());
|
|
|
|
+ //String arg2 = row.get("tender_id_").toString();
|
|
|
|
+
|
|
|
|
+ Map<String, Object> parameterMap = new HashMap<String, Object>();
|
|
|
|
+ parameterMap.put("userId", arg0);
|
|
|
|
+ parameterMap.put("amount", arg1);
|
|
|
|
+ parameterMap.put("memo", "结清");
|
|
|
|
+
|
|
|
|
+ List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
|
|
|
|
+ if (parameterMap != null) {
|
|
|
|
+ for (Entry<String, Object> entry : parameterMap.entrySet()) {
|
|
|
|
+ String name = entry.getKey();
|
|
|
|
+ String value = ConvertUtils.convert(entry.getValue());
|
|
|
|
+ if (StringUtils.isNotEmpty(name)) {
|
|
|
|
+ nameValuePairs.add(new BasicNameValuePair(name, value));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ httpPost = new HttpPost("https://ehjinrong.com/manage/sysAccount/fundAllocate.do");
|
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
|
|
|
|
+ httpResponse = httpClient.execute(httpPost);
|
|
|
|
+ httpEntity = httpResponse.getEntity();
|
|
|
|
+ EntityUtils.toString(httpEntity);
|
|
|
|
+ EntityUtils.consume(httpEntity);
|
|
|
|
+
|
|
|
|
+ System.out.println(" userId:" + arg0 + " reward:" + arg1);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ } finally {
|
|
|
|
+ httpPost.releaseConnection();
|
|
|
|
+ try {
|
|
|
|
+ httpClient.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|