Commit 493d231f authored by shulidong's avatar shulidong

报告生成工具类

parent 7d626c3e
package cn.wise.sc.energy.power.plant.business.config;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
@Component
public class MyApplicationContextAware implements ApplicationContextAware {
private static ApplicationContext APPLICATION_CONTEXT;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
APPLICATION_CONTEXT = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return APPLICATION_CONTEXT;
}
}
\ No newline at end of file
package cn.wise.sc.energy.power.plant.business.domain.redisTrans;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class BtreeInerAct implements Serializable {
//诊断ID
private String id;
//问题id
private String qid;
private int select;
}
package cn.wise.sc.energy.power.plant.business.poi;
import cn.afterturn.easypoi.entity.ImageEntity;
import lombok.extern.slf4j.Slf4j;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.springframework.util.Assert;
import java.awt.*;
import java.io.*;
import java.util.Map;
@Slf4j
public class JfreeUtil {
private static String tempImgPath="D:\\tempJfree.jpeg";
/**
* 将图片转化为字节数组
* @return 字节数组
*/
private static byte[] imgToByte(){
File file = new File(tempImgPath);
byte[] buffer = null;
try {
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (IOException e) {
log.error(e.getMessage());
}
//删除临时文件
file.delete();
return buffer;
}
public static ImageEntity pieChart(String title, Map<String, Integer> datas, int width, int height) {
//创建主题样式
StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
//设置标题字体
standardChartTheme.setExtraLargeFont(new Font("宋体", Font.BOLD, 20));
//设置图例的字体
standardChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15));
//设置轴向的字体
standardChartTheme.setLargeFont(new Font("宋体", Font.PLAIN, 15));
//设置主题样式
ChartFactory.setChartTheme(standardChartTheme);
//根据jfree生成一个本地饼状图
DefaultPieDataset pds = new DefaultPieDataset();
datas.forEach(pds::setValue);
//图标标题、数据集合、是否显示图例标识、是否显示tooltips、是否支持超链接
JFreeChart chart = ChartFactory.createPieChart(title, pds, true, false, false);
//设置抗锯齿
chart.setTextAntiAlias(false);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setNoDataMessage("暂无数据");
//忽略无值的分类
plot.setIgnoreNullValues(true);
plot.setBackgroundAlpha(0f);
//设置标签阴影颜色
plot.setShadowPaint(new Color(255,255,255));
//设置标签生成器(默认{0})
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1})/{2}"));
try {
ChartUtils.saveChartAsJPEG(new File(tempImgPath), chart, width, height);
} catch (IOException e1) {
log.error("生成饼状图失败!");
}
ImageEntity imageEntity = new ImageEntity(imgToByte(), width, height);
Assert.notNull(imageEntity.getData(),"生成饼状图对象失败!");
return imageEntity;
}
}
\ No newline at end of file
package cn.wise.sc.energy.power.plant.business.poi;
import java.util.ArrayList;
import java.util.HashMap;
import cn.afterturn.easypoi.entity.ImageEntity;
/**
* @author 何昌杰
*/
public class WordDemo1 {
public static void main(String[] args) {
HashMap<String, Object> map = new HashMap<>(4);
long startTime = System.currentTimeMillis();
//模拟饼状图数据
HashMap<String, Integer> datas = new HashMap<>(3);
datas.put("一号",10);
datas.put("二号",20);
datas.put("三号",40);
ImageEntity imageEntity = JfreeUtil.pieChart("测试",datas, 500, 300);
map.put("picture", imageEntity);
//模拟其它普通数据
map.put("username", "张三");
map.put("date", "2019-10-10");
map.put("desc", "测试");
map.put("boo", true);
//模拟表格数据
ArrayList<HashMap<String, String>> list = new ArrayList<>(2);
HashMap<String, String> temp = new HashMap<>(3);
temp.put("sn","1");
temp.put("name","第一个人");
temp.put("age","23");
list.add(temp);
for (int i = 0; i < 100; i++) {
temp = new HashMap<>(3);
temp.put("sn","2");
temp.put("name","第二个人");
temp.put("age","24");
list.add(temp);
}
map.put("personlist",list);
//word模板相对路径、word生成路径、word生成的文件名称、数据源
WordUtil.exportWord("C:\\Users\\admin\\Desktop\\template\\demo1.docx", "D:/", "生成文件.docx", map);
System.out.println(System.currentTimeMillis()-startTime);
}
}
\ No newline at end of file
package cn.wise.sc.energy.power.plant.business.poi;
import cn.afterturn.easypoi.word.WordExportUtil;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.util.Assert;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;
public class WordUtil {
/**
* 导出word
* <p>第一步生成替换后的word文件,只支持docx</p>
* <p>第二步下载生成的文件</p>
* <p>第三步删除生成的临时文件</p>
* 模版变量中变量格式:{{foo}}
*
* @param templatePath word模板地址
* @param temDir 生成临时文件存放地址
* @param fileName 文件名
* @param params 替换的参数
*/
public static void exportWord(String templatePath, String temDir, String fileName, Map<String, Object> params) {
Assert.notNull(templatePath, "模板路径不能为空");
Assert.notNull(temDir, "临时文件路径不能为空");
Assert.notNull(fileName, "导出文件名不能为空");
Assert.isTrue(fileName.endsWith(".docx"), "word导出请使用docx格式");
if (!temDir.endsWith("/")) {
temDir = temDir + File.separator;
}
File dir = new File(temDir);
if (!dir.exists()) {
dir.mkdirs();
}
try {
XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params);
String tmpPath = temDir + fileName;
FileOutputStream fos = new FileOutputStream(tmpPath);
doc.write(fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
package cn.wise.sc.energy.power.plant.business.repository;
import cn.wise.sc.energy.power.plant.business.domain.AlertInfo;
import cn.wise.sc.energy.power.plant.business.domain.BtreeInfoConfig;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
/**
* @description: 阈值更新记录仓储
* @author: qh
* @create: 2020-09-03 14:41
**/
@Repository
public interface BtreeInfoConfigRepository extends
JpaRepository<BtreeInfoConfig, Long>,
JpaSpecificationExecutor<AlertInfo> {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment