Commit 35e0869d authored by 张永's avatar 张永
parents 1188f535 071beacc
......@@ -67,7 +67,7 @@
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.8</version>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
......
......@@ -106,7 +106,9 @@ public class DateUtil {
* @return
*/
public static Date parse(Object obj) {
if (obj instanceof Date) {
if(obj == null) {
return null;
} else if (obj instanceof Date) {
return new Date(((Date) obj).getTime());
} else {
String str = String.valueOf(obj).trim();
......
......@@ -3,25 +3,25 @@ package com.egolm.common;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
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.HSSFRichTextString;
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.CellType;
import org.apache.poi.ss.usermodel.Row;
/**
......@@ -32,7 +32,35 @@ import org.apache.poi.ss.usermodel.Row;
*/
public class ExcelUtil {
/**
* 生成Excel
* @param os 输出流
* @param args 获取数据的键
* @param titles Excel表头
* @param widths Excel列宽度
* @param list 数据集
*/
public static void excel(OutputStream os, LinkedHashMap<String, String> titleMap, List<Map<String, Object>> list) {
List<String> argsList = new ArrayList<String>();
List<String> titleList = new ArrayList<String>();
for(String key : titleMap.keySet()) {
argsList.add(key);
titleList.add(titleMap.get(key));
}
String[] args = argsList.toArray(new String[argsList.size()]);
String[] titles = titleList.toArray(new String[titleList.size()]);
Object[][] objs = new Object[list.size()+1][args.length];
objs[0] = titles;
for(int i = 0; i < list.size(); i++) {
Map<String, Object> map = list.get(i);
Object[] obj = new Object[args.length];
for(int k = 0; k < obj.length; k++) {
obj[k] = map.get(args[k]);
}
objs[i+1] = obj;
}
ExcelUtil.excel(os, objs, null);
}
/**
......@@ -80,8 +108,7 @@ public class ExcelUtil {
* @param widths 列宽度{24, 12, 15, 15}
*/
public static void excel(OutputStream os, Object[][] objs, Integer[] widths) {
try {
HSSFWorkbook wb = new HSSFWorkbook();
try(HSSFWorkbook wb = new HSSFWorkbook()) {
HSSFCellStyle bodyStyle = wb.createCellStyle();
bodyStyle.setWrapText(true);
HSSFSheet sheet = wb.createSheet("sheet1");
......@@ -196,8 +223,7 @@ public class ExcelUtil {
* @throws IOException
*/
public static String[][] excel(InputStream is) {
try {
HSSFWorkbook wb = new HSSFWorkbook(is);
try (HSSFWorkbook wb = new HSSFWorkbook(is)) {
HSSFSheet sheet = wb.getSheetAt(0);
String[][] datas = new String[sheet.getLastRowNum()+1][];
for(int i = 0; i < datas.length; i++) {
......@@ -222,39 +248,32 @@ public class ExcelUtil {
* @return
*/
private static String stringVal(Cell cell) {
try {
int cellType = cell.getCellType();
if(cellType == HSSFCell.CELL_TYPE_NUMERIC) {
if (HSSFDateUtil.isCellDateFormatted(cell)) {
return DateUtil.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
if(cell != null) {
CellType cellType = cell.getCellType();
if(cellType == CellType.NUMERIC) {
if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
return DateUtil.format(org.apache.poi.ss.usermodel.DateUtil.getJavaDate(cell.getNumericCellValue()));
} else {
return String.valueOf(FMT_INT.format(cell.getNumericCellValue()));
}
} else if(cellType == HSSFCell.CELL_TYPE_STRING) {
return String.valueOf(cell.getRichStringCellValue());
} else if(cellType == HSSFCell.CELL_TYPE_FORMULA) {
} else if(cellType == CellType.STRING) {
return cell.getStringCellValue();
} else if(cellType == CellType.FORMULA) {
String value = String.valueOf(cell.getNumericCellValue());
if (value.equals("NaN")) {
value = String.valueOf(cell.getRichStringCellValue());
}
return value;
} else if(cellType == HSSFCell.CELL_TYPE_BOOLEAN) {
} else if(cellType == CellType.BOOLEAN) {
return String.valueOf(cell.getBooleanCellValue());
} else if(cellType == HSSFCell.CELL_TYPE_BLANK) {
return "";
} else if(cellType == HSSFCell.CELL_TYPE_ERROR) {
} else if(cellType == CellType.BLANK || cellType == CellType.ERROR) {
return "";
} else {
return String.valueOf(cell.getRichStringCellValue());
return cell.getStringCellValue();
}
} catch (Exception e) {
return null;
} else {
return "";
}
}
public static void main(String[] args) throws FileNotFoundException, IOException {
String[][] ds = new String[][]{{"1", "2", "3"}, {"4", "5", "6"}};
ExcelUtil.csv(new FileOutputStream("D:/test.csv"), ds);
}
}
......@@ -1161,6 +1161,8 @@ public class StringUtil {
return null;
} else if(object instanceof Date) {
return DateUtil.format((Date)object);
} else if(object instanceof Throwable) {
return toStackString((Throwable)object).toString();
}
return object.toString();
}
......@@ -1169,4 +1171,22 @@ public class StringUtil {
return str1 == null ? str2 == null : str1.equals(str2);
}
public static StringBuffer toStackString(Throwable ex) {
StringBuffer stack = new StringBuffer();
if(ex != null) {
stack.append(ex.getClass().getName()).append(": ").append(ex.getMessage()).append(System.lineSeparator());
StackTraceElement[] elms = ex.getStackTrace();
for(StackTraceElement elm : elms) {
stack.append("\t").append(elm.getClassName()).append(".").append(elm.getMethodName()).append("(").append(elm.getFileName()).append(" ").append(elm.getLineNumber()).append(")\n");
}
if(ex.getCause() != null) {
stack.append("\n\n").append("Cause By: \n").append(toStackString(ex.getCause()));
}
for(Throwable e : ex.getSuppressed()) {
stack.append("\n\n").append("Suppressed By: \n").append(toStackString(e));
}
}
return stack;
}
}
......@@ -274,9 +274,28 @@ public class JdbcTemplate extends org.springframework.jdbc.core.JdbcTemplate {
}
public int save(String tableName, Map<String, Object> objMap) {
Sql desc = dialect.argsInsert(tableName, objMap);
int save_count = this.executeUpdate(desc.getSql(), desc.getArgs());
return save_count;
Sql iSql = dialect.argsInsert(tableName, objMap);
KeyHolder keyHolder = new GeneratedKeyHolder();
int count = super.update(new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(iSql.getSql(), Statement.RETURN_GENERATED_KEYS);
Object[] args = iSql.getArgs();
for(int i = 0; i <args.length; i++) {
Object argObj = args[i];
if(argObj instanceof java.util.Date) {
java.util.Date argDate = (java.util.Date)argObj;
ps.setObject(i+1, new java.sql.Timestamp(argDate.getTime()));
} else if(argObj.getClass().isEnum()) {
ps.setObject(i+1, args[i].toString());
} else {
ps.setObject(i+1, args[i]);
}
}
return ps;
}
}, keyHolder);
objMap.putAll(keyHolder.getKeys());
return count;
}
public int delete(Object... objs) {
......
......@@ -333,9 +333,16 @@ public interface Dialect {
default Sql argsQuery(String tableName, Map<String, Object> objMap) {
List<String> wheres = new ArrayList<String>();
List<Object> args = new ArrayList<Object>();
for(String key : objMap.keySet()) {
wheres.add(getColumnLeft() + key + getColumnRight() + " = ?");
args.add(objMap.get(key));
if(objMap != null) {
for(String key : objMap.keySet()) {
Object val = objMap.get(key);
if(val == null) {
wheres.add(getColumnLeft() + key + getColumnRight() + " is null");
} else {
wheres.add(getColumnLeft() + key + getColumnRight() + " = ?");
args.add(val);
}
}
}
String sql = "select * from " + tableName + StringUtil.join(" and ", " where ", "", wheres);
return new Sql(sql, args.toArray());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment