package com.whyc.controller;
|
|
import com.whyc.service.ReportService;
|
import org.docx4j.Docx4J;
|
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
|
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
|
import java.io.File;
|
import java.text.SimpleDateFormat;
|
import java.util.HashMap;
|
|
import static org.docx4j.Docx4J.*;
|
|
/**
|
* 报告生成
|
*/
|
@RestController
|
@RequestMapping("report")
|
public class ReportController {
|
|
@Autowired
|
private ReportService service;
|
|
@GetMapping("generateDocument")
|
public void generateDocument(){
|
try {
|
//获取classpath下的模板
|
String templatePath = this.getClass().getClassLoader().getResource("").getPath()+"resources\\template\\template_01.docx";
|
//加载模板
|
WordprocessingMLPackage wordprocessingMLPackage = WordprocessingMLPackage.load(new File(templatePath));
|
MainDocumentPart documentPart = wordprocessingMLPackage.getMainDocumentPart();
|
|
//查询巡检时候的电流电压温度数据
|
float current = 1.0f;
|
float voltage = 2.0f;
|
float temperature = 3.0f;
|
String reporter = "whyc";
|
String inspectionTime = "2020-01-01 00:00:00";
|
HashMap<String,String> map = new HashMap<>();
|
map.put("current",current+"");
|
map.put("voltage",voltage+"");
|
map.put("temperature",temperature+"");
|
map.put("reporter",reporter);
|
map.put("inspectionTime",inspectionTime);
|
//替换占位符
|
documentPart.variableReplace(map);
|
//输出文件名称
|
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
|
String fileName = "报告_01_"+format.format(System.currentTimeMillis())+".docx";
|
Docx4J.save(wordprocessingMLPackage, new File("C:\\code\\web\\InspectionSystem\\src\\main\\resources\\template\\"+fileName));
|
|
}catch(Exception e){
|
e.printStackTrace();
|
}
|
|
}
|
|
}
|