提交 a5135806 authored 作者: 孙洁清's avatar 孙洁清

v1.1

上级 2b430f18
package com.zjty.inspect.service;
import com.zjty.inspect.entity.DocumentPage;
public interface DocumentService {
String createDocument(DocumentPage documentPage);
}
package com.zjty.inspect.service.impl;
import com.zjty.inspect.entity.DocumentContent;
import com.zjty.inspect.entity.DocumentPage;
import com.zjty.inspect.entity.TitleHead;
import com.zjty.inspect.service.DocumentService;
import com.zjty.inspect.utils.DocumentWordUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
@Service
public class DocumentServiceImpl implements DocumentService {
@Override
public String createDocument(DocumentPage documentPage) {
StringBuilder sb=new StringBuilder();
//前面部分内容
String standard = DocumentWordUtil.standard();
String enddard = DocumentWordUtil.enddard();
//标题内容
String maintitle = DocumentWordUtil.Maintitle();
String subheading = DocumentWordUtil.subheading();
//标题下的内容
String standContent = DocumentWordUtil.content();
//表格内容
//页眉
String header = documentPage.getHeader();
String s1 = maintitle.replaceAll("#title#", header);
sb.append(standard);
sb.append(s1);
List<TitleHead> titleHeads = documentPage.getTitleHeads();
if(titleHeads!=null&&titleHeads.size()>0){
for (TitleHead titleHead : titleHeads) {
String titleContent=subheading;
//标题名称
String name = titleHead.getName();
if(!StringUtils.isEmpty(name)) {
//标题级别
int parentId = titleHead.getParentId();
String s = titleContent.replaceAll("#title#", name)
.replaceAll("#parentId#", String.valueOf(parentId));
sb.append(s);
}
List<DocumentContent> documentContents = titleHead.getDocumentContents();
if(documentContents!=null&&documentContents.size()>0){
for (DocumentContent documentContent : documentContents) {
if(documentContent!=null){
String content = documentContent.getContent();
String baseUrl = documentContent.getBaseUrl();
String documentTable = documentContent.getDocumentTable();
if(!StringUtils.isEmpty(content)){
String c=standContent;
String s = c.replaceAll("#content#",content);
sb.append(s);
}
if(!StringUtils.isEmpty(baseUrl)){
}
if(!StringUtils.isEmpty(documentTable)){
sb.append(documentTable);
}
}
}
}
}
}
sb.append(enddard);
return sb.toString();
}
}
package com.zjty.inspect.utils;
import com.zjty.inspect.entity.TechnologyContent;
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.*;
import java.util.List;
import java.util.Map;
public class FreeMakerUtils {
public static String parseTpl(String viewName, Map<String, List<TechnologyContent>> params) {
Configuration cfg = SpringContextHolder.getBean(Configuration.class);
String html = null;
Template t = null;
try {
t = cfg.getTemplate(viewName + ".ftl");
createWord(t,params);
// html = FreeMarkerTemplateUtils.processTemplateIntoString(t, params);
System.out.println(html);
} catch (Exception e) {
e.printStackTrace();
}
return html;
}
public static String createWord(Template template, Map<String, List<TechnologyContent>> dataMap){
try {
//文件路径
String filePath= "E://doc";
//文件名称
String fileName = System.currentTimeMillis()+".doc";
// 输出文件
File outFile = new File(filePath + File.separator + fileName);
// 如果输出目标文件夹不存在,则创建
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdirs();
}
// 将模板和数据模型合并生成文件
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile), "UTF-8"));
// 生成文件
template.process(dataMap, out);
// 关闭流
out.flush();
out.close();
}catch (Exception e){
e.printStackTrace();
}
return null;
}
public static void writeFile(String file, String conent) {
// Log4jBean.logger.info("开始以追加的形式写文件到:["+file+"]");
BufferedWriter out = null;
try {
File outFile = new File(file);
// 如果输出目标文件夹不存在,则创建
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdirs();
}
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile, false)));
out.write(conent);
// Log4jBean.logger.info("写文件:["+file+"]完成");
} catch (Exception e) {
// Log4jBean.logger.error("写文件:["+file+"]异常,异常信息为:["+e.getMessage()+"]");
} finally {
// Log4jBean.logger.info("开始关闭输出流");
try {
out.close();
} catch (IOException e) {
// Log4jBean.logger.info("关闭输出流异常,异常信息为:["+e.getMessage()+"]");
}
}
}
}
package com.zjty.inspect.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContextHolder implements ApplicationContextAware {
private static ApplicationContext appContext = null;
/**
* 通过name获取 Bean.
*
* @param name
* @return
*/
public static Object getBean(String name) {
return appContext.getBean(name);
}
/**
* 通过class获取Bean.
*
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz) {
return appContext.getBean(clazz);
}
/**
* 通过name,以及Clazz返回指定的Bean
*
* @param name
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String name, Class<T> clazz) {
return appContext.getBean(name, clazz);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (appContext == null) {
appContext = applicationContext;
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论