|
|
|
@ -55,7 +55,28 @@ public class BaseUtil {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 文件导出 |
|
|
|
|
* pdf文件导出 |
|
|
|
|
* @param templateFile 模板文件路径 |
|
|
|
|
* @param params 数据参数 |
|
|
|
|
* @param wordPath 文件生成路径 |
|
|
|
|
* @param pdfPath pdf生成路径 |
|
|
|
|
* @param response 响应类 |
|
|
|
|
*/ |
|
|
|
|
public static void exportDocument(String templateFile, Map<String, Object> params, String wordPath, String pdfPath, HttpServletResponse response) { |
|
|
|
|
try { |
|
|
|
|
List<XWPFDocument> res = new ArrayList<>(); |
|
|
|
|
XWPFDocument document = BaseUtil.fillDocument(templateFile, params); |
|
|
|
|
res.add(document); |
|
|
|
|
BaseUtil.exportWord(res, wordPath); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("文件导出异常: {}", e.getMessage()); |
|
|
|
|
} |
|
|
|
|
AsposeUtil.wordToPdf(wordPath, pdfPath); |
|
|
|
|
BaseUtil.readPdf(response,pdfPath); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* word文件导出 |
|
|
|
|
* @param documents doc文件 |
|
|
|
|
* @param savePath 存储路径 |
|
|
|
|
*/ |
|
|
|
@ -67,22 +88,22 @@ public class BaseUtil {
|
|
|
|
|
document.write(out); |
|
|
|
|
} |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
log.error("文件导出异常: {}", e.getMessage()); |
|
|
|
|
log.error("word文件导出异常: {}", e.getMessage()); |
|
|
|
|
} finally { |
|
|
|
|
if (out != null) { |
|
|
|
|
try { |
|
|
|
|
out.close(); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
log.error("文件导出-输出流关闭异常: {}", e.getMessage()); |
|
|
|
|
log.error("word文件导出-输出流关闭异常: {}", e.getMessage()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 数据写入模板文件 |
|
|
|
|
* @param templatePath 模板路径 |
|
|
|
|
* @param params 数据 |
|
|
|
|
* 数据写入 |
|
|
|
|
* @param templatePath 模板文件路径 |
|
|
|
|
* @param params 数据参数 |
|
|
|
|
*/ |
|
|
|
|
public static XWPFDocument fillDocument(String templatePath, Map<String, Object> params) { |
|
|
|
|
XWPFDocument xwpfDocument = null; |
|
|
|
@ -113,7 +134,7 @@ public class BaseUtil {
|
|
|
|
|
"inline; filename= " + URLEncoder.encode(file.getName(), "UTF-8")); |
|
|
|
|
outputStream.flush(); |
|
|
|
|
} catch (IOException e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
log.error("本地pdf文件读取异常: {}", e.getMessage()); |
|
|
|
|
} finally { |
|
|
|
|
try { |
|
|
|
|
if (fileInputStream != null) { |
|
|
|
@ -128,16 +149,21 @@ public class BaseUtil {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 对象转map |
|
|
|
|
* @param obj 对象类 |
|
|
|
|
* @return map参数 |
|
|
|
|
*/ |
|
|
|
|
public static Map<String, Object> obj2Map(Object obj) { |
|
|
|
|
if(ObjectUtil.isNotEmpty(obj) && null != obj.getClass()) { |
|
|
|
|
if (ObjectUtil.isNotEmpty(obj) && null != obj.getClass()) { |
|
|
|
|
Map<String, Object> result = new HashMap<>(); |
|
|
|
|
List<Field> fieldList = new ArrayList<>(); |
|
|
|
|
Class<?> clazz = obj.getClass(); |
|
|
|
|
while(clazz != null) { |
|
|
|
|
while (clazz != null) { |
|
|
|
|
fieldList.addAll(Arrays.asList(clazz.getDeclaredFields())); |
|
|
|
|
clazz = clazz.getSuperclass(); |
|
|
|
|
} |
|
|
|
|
fieldList.forEach(field -> { |
|
|
|
|
fieldList.forEach (field -> { |
|
|
|
|
field.setAccessible(true); |
|
|
|
|
Object value; |
|
|
|
|
try { |
|
|
|
@ -145,11 +171,11 @@ public class BaseUtil {
|
|
|
|
|
} catch (IllegalAccessException e) { |
|
|
|
|
throw new ServiceException("获取属性性出错"); |
|
|
|
|
} |
|
|
|
|
if(value instanceof List) { |
|
|
|
|
if (value instanceof List) { |
|
|
|
|
List<Map<String, Object>> list = ((List<?>) value).stream().map(BaseUtil::obj2Map).collect(Collectors.toList()); |
|
|
|
|
result.put(field.getName(),list); |
|
|
|
|
} |
|
|
|
|
else if(value instanceof LocalDateTime) { |
|
|
|
|
else if (value instanceof LocalDateTime) { |
|
|
|
|
DateTimeToMap dateTimeToMap = field.getAnnotation(DateTimeToMap.class); |
|
|
|
|
LocalDateTime time = (LocalDateTime) value; |
|
|
|
|
if(ObjectUtil.isEmpty(dateTimeToMap)) { |
|
|
|
@ -159,10 +185,10 @@ public class BaseUtil {
|
|
|
|
|
result.putAll(LocalDateConvertMap(time,dateTimeToMap.prefix(),dateTimeToMap.split())); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else if(value instanceof Date) { |
|
|
|
|
else if (value instanceof Date) { |
|
|
|
|
DateTimeToMap dateTimeToMap = field.getAnnotation(DateTimeToMap.class); |
|
|
|
|
Date time = (Date) value; |
|
|
|
|
if(ObjectUtil.isEmpty(dateTimeToMap)) { |
|
|
|
|
if (ObjectUtil.isEmpty(dateTimeToMap)) { |
|
|
|
|
result.put(field.getName(),DateUtil.format(time,DateUtil.PATTERN_DATETIME)); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
@ -191,7 +217,7 @@ public class BaseUtil {
|
|
|
|
|
* @return map对象 |
|
|
|
|
*/ |
|
|
|
|
public static Map<String, Object> LocalDateConvertMap(LocalDateTime dateTime,String prefix,String split) { |
|
|
|
|
HashMap<String, Object> result = new HashMap<String, Object>() {{ |
|
|
|
|
return new HashMap<String, Object>() {{ |
|
|
|
|
put(prefix + split + "year", dateTime.getYear()); |
|
|
|
|
put(prefix + split + "month", dateTime.getMonthValue()); |
|
|
|
|
put(prefix + split + "day", dateTime.getDayOfMonth()); |
|
|
|
@ -199,19 +225,5 @@ public class BaseUtil {
|
|
|
|
|
put(prefix + split + "minuter", dateTime.getMinute()); |
|
|
|
|
put(prefix + split + "second", dateTime.getSecond()); |
|
|
|
|
}}; |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void exportDocument(String templateFile, Map<String, Object> params, String wordPath, String pdfPath, HttpServletResponse response) { |
|
|
|
|
try { |
|
|
|
|
List<XWPFDocument> res = new ArrayList<>(); |
|
|
|
|
XWPFDocument document = BaseUtil.fillDocument(templateFile, params); |
|
|
|
|
res.add(document); |
|
|
|
|
BaseUtil.exportWord(res, wordPath); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} |
|
|
|
|
AsposeUtil.wordToPdf(wordPath, pdfPath); |
|
|
|
|
BaseUtil.readPdf(response,pdfPath); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|