模板生成
This commit is contained in:
@@ -1,11 +1,61 @@
|
||||
package com.example.ts_obj.controller;
|
||||
|
||||
import com.deepoove.poi.XWPFTemplate;
|
||||
import com.example.ts_obj.bean.ReturnCodeAndMsgEnum;
|
||||
import com.example.ts_obj.bean.ReturnValue;
|
||||
import com.google.common.collect.Maps;
|
||||
import io.swagger.annotations.Api;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/dataApi")
|
||||
@Api(value = "提交数据库方法接口")
|
||||
public class DataapiController {
|
||||
private final static Logger logger = LoggerFactory.getLogger(DataapiController.class);
|
||||
|
||||
|
||||
private String BASE_DIR="d:/upload/";
|
||||
// private String BASE_DIR = "/home/wwwroot/ts.tmrjournals.com/upload/";
|
||||
|
||||
/**
|
||||
* 模板生成work文档
|
||||
* @param map
|
||||
* @return 文档路径
|
||||
*/
|
||||
@PostMapping("/createTemplate")
|
||||
public ReturnValue createTemplate (Map<String, Object> map){
|
||||
HashMap<String, Object> result = Maps.newHashMap();
|
||||
if (map.size() == 0) {
|
||||
return new ReturnValue(ReturnCodeAndMsgEnum.No_Data);
|
||||
}
|
||||
XWPFTemplate template = XWPFTemplate.compile(BASE_DIR + "data_template").render(map);
|
||||
try {
|
||||
Date date = new Date();
|
||||
String dirpath = BASE_DIR + new SimpleDateFormat("yyyyMMdd").format(date);
|
||||
//如果不存在,创建文件夹
|
||||
File f = new File(dirpath);
|
||||
if (!f.exists()) {
|
||||
f.mkdirs();
|
||||
}
|
||||
String goFileName = System.currentTimeMillis() + ".docx";
|
||||
template.writeAndClose(new FileOutputStream(dirpath + "/" + goFileName));
|
||||
logger.info("创建成功 fileName = {}", goFileName);
|
||||
result.put("file", new SimpleDateFormat("yyyyMMdd").format(date) + "/" + goFileName);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return new ReturnValue(ReturnCodeAndMsgEnum.Success);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user