|
|
@ -4,6 +4,7 @@ import cn.afterturn.easypoi.word.WordExportUtil; |
|
|
|
import cn.hutool.core.bean.BeanUtil; |
|
|
|
import com.epmet.commons.tools.annotation.LoginUser; |
|
|
|
import com.epmet.commons.tools.aop.NoRepeatSubmit; |
|
|
|
import com.epmet.commons.tools.exception.ExceptionUtils; |
|
|
|
import com.epmet.commons.tools.page.PageData; |
|
|
|
import com.epmet.commons.tools.utils.DateUtils; |
|
|
|
import com.epmet.commons.tools.security.dto.TokenDto; |
|
|
@ -16,6 +17,8 @@ import com.epmet.commons.tools.validator.group.UpdateGroup; |
|
|
|
import com.epmet.dto.MemoWorkDiaryDTO; |
|
|
|
import com.epmet.dto.form.MemoWorkDiaryFormDTO; |
|
|
|
import com.epmet.service.MemoWorkDiaryService; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.io.IOUtils; |
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFDocument; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
@ -24,8 +27,13 @@ import org.springframework.web.bind.annotation.*; |
|
|
|
import javax.servlet.ServletOutputStream; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.FileOutputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.OutputStream; |
|
|
|
import java.net.URL; |
|
|
|
import java.nio.file.Files; |
|
|
|
import java.nio.file.Path; |
|
|
|
import java.nio.file.Paths; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Optional; |
|
|
@ -37,6 +45,7 @@ import java.util.Optional; |
|
|
|
* @author generator generator@elink-cn.com |
|
|
|
* @since v1.0.0 2022-03-15 |
|
|
|
*/ |
|
|
|
@Slf4j |
|
|
|
@RestController |
|
|
|
@RequestMapping("memoWorkDiary") |
|
|
|
public class MemoWorkDiaryController { |
|
|
@ -88,8 +97,7 @@ public class MemoWorkDiaryController { |
|
|
|
|
|
|
|
@PostMapping("/{id}/exportWord") |
|
|
|
public void exportWord(@PathVariable("id") String id, HttpServletResponse response) throws Exception { |
|
|
|
URL resource = this.getClass().getClassLoader().getResource("templates/memo_work_diary_export_template.docx"); |
|
|
|
|
|
|
|
String templateFilePath = loadTemplate("memo_work_diary_export_template.docx"); |
|
|
|
MemoWorkDiaryFormDTO form = new MemoWorkDiaryFormDTO(); |
|
|
|
form.setId(id); |
|
|
|
MemoWorkDiaryDTO data = memoWorkDiaryService.get(form); |
|
|
@ -98,10 +106,9 @@ public class MemoWorkDiaryController { |
|
|
|
String createTimeStr = DateUtils.format(data.getCreatedTime(), "yyyy年MM月dd日 HH:mm"); |
|
|
|
map.put("createTime", createTimeStr); |
|
|
|
|
|
|
|
XWPFDocument doc = WordExportUtil.exportWord07(resource.getFile(), map); |
|
|
|
XWPFDocument doc = WordExportUtil.exportWord07(templateFilePath, map); |
|
|
|
|
|
|
|
String filePath = resource.getFile(); |
|
|
|
String suffix = filePath.substring(filePath.lastIndexOf(".")); |
|
|
|
String suffix = templateFilePath.substring(templateFilePath.lastIndexOf(".")); |
|
|
|
|
|
|
|
response.setHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "Content-Disposition"); |
|
|
|
response.setHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); |
|
|
@ -117,4 +124,51 @@ public class MemoWorkDiaryController { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 加载模板 |
|
|
|
* @param templateFileName |
|
|
|
* @return |
|
|
|
* @throws IOException |
|
|
|
*/ |
|
|
|
private String loadTemplate(String templateFileName) throws IOException { |
|
|
|
String homeDir = System.getProperty("user.home"); |
|
|
|
Path templates = Paths.get(homeDir, "epmet_files", "templates"); |
|
|
|
if (Files.notExists(templates)) { |
|
|
|
Files.createDirectory(templates); |
|
|
|
} |
|
|
|
|
|
|
|
Path templateFilePath = templates.resolve(templateFileName); |
|
|
|
String templateFilePathStr = templateFilePath.toString(); |
|
|
|
if (Files.exists(templateFilePath)) { |
|
|
|
return templateFilePathStr; |
|
|
|
} |
|
|
|
|
|
|
|
// 将项目中的模板拷贝至用户家目录中
|
|
|
|
OutputStream os = null; |
|
|
|
InputStream is = null; |
|
|
|
try { |
|
|
|
is = this.getClass().getClassLoader().getResourceAsStream("templates/" + templateFileName); |
|
|
|
os = new FileOutputStream(templateFilePathStr); |
|
|
|
IOUtils.copy(is, os); |
|
|
|
} finally { |
|
|
|
try { |
|
|
|
if (is != null) { |
|
|
|
is.close(); |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
String errorMsg = ExceptionUtils.getThrowableErrorStackTrace(e); |
|
|
|
log.error("【导出工作日志doc】关闭输入流出错:{}", errorMsg); |
|
|
|
} |
|
|
|
try { |
|
|
|
if (os != null) { |
|
|
|
os.close(); |
|
|
|
} |
|
|
|
} catch (IOException e) { |
|
|
|
String errorMsg = ExceptionUtils.getThrowableErrorStackTrace(e); |
|
|
|
log.error("【导出工作日志doc】关闭输出流出错:{}", errorMsg); |
|
|
|
} |
|
|
|
} |
|
|
|
return templateFilePathStr; |
|
|
|
} |
|
|
|
} |
|
|
|