添加新功能

报告生成
This commit is contained in:
2022-12-02 09:43:07 +08:00
parent 48e607e8b1
commit 4b48e7f55e
4 changed files with 345 additions and 184 deletions

View File

@ -1,21 +1,26 @@
package com.ruoyi.common.utils;
import org.apache.poi.hpsf.Section;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.HeaderFooter;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.util.Units;
import org.apache.poi.wp.usermodel.HeaderFooterType;
import org.apache.poi.wp.usermodel.Paragraph;
import org.apache.poi.xddf.usermodel.chart.*;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.*;
import java.io.FileInputStream;
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.List;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSpacing;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STLineSpacingRule;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
/**
@ -26,6 +31,72 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.STLineSpacingRule;
public class BarChart
{
public static Double fun(Double value)
{
BigDecimal bg=new BigDecimal(value);
return bg.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
}
private void setXWPFRunStyle(XWPFRun r1,String font,int fontSize) {
r1.setFontSize(fontSize);
CTRPr rpr = r1.getCTR().isSetRPr() ? r1.getCTR().getRPr() : r1.getCTR().addNewRPr();
CTFonts fonts = rpr.isSetRFonts() ? rpr.getRFonts() : rpr.addNewRFonts();
fonts.setAscii(font);
fonts.setEastAsia(font);
fonts.setHAnsi(font);
}
public static void createHeader(XWPFDocument doc, String orgFullName){
try {
CTSectPr sectPr = doc.getDocument().getBody().addNewSectPr();
XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(doc, sectPr);
XWPFHeader header = headerFooterPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT);
XWPFParagraph paragraph = header.createParagraph();
paragraph.setAlignment(ParagraphAlignment.BOTH); //设置段落左对齐
paragraph.setBorderBottom(Borders.THICK); //设置下划线
// paragraph.setAlignment(ParagraphAlignment.CENTER);
XWPFRun run = paragraph.createRun();
run.setBold(false);
run.setFontSize(9);
/*
* 取到图片的字节流
* */
String logoFilePath="/home/sjs/ruoyi/shate/jpg/shate.png";
if (StringUtils.isNotEmpty(logoFilePath)) {
File file=new File(logoFilePath);
InputStream is = new FileInputStream(file);
XWPFPicture picture = run.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, logoFilePath, Units.toEMU(60), Units.toEMU(30));
String blipID = "";
for(XWPFPictureData picturedata : header.getAllPackagePictures()) { //这段必须有不然打开的logo图片不显示
blipID = header.getRelationId(picturedata);
}
picture.getCTPicture().getBlipFill().getBlip().setEmbed(blipID);
run.addTab();
is.close();
}
/*
* 添加字体页眉
* */
if (StringUtils.isNotEmpty(orgFullName)) {
run = paragraph.createRun();
run.setText(" "+orgFullName);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InvalidFormatException e) {
throw new RuntimeException(e);
}
}
//绘制折线图
public static void drawLine(XWPFDocument document, String[] xAxisData,Double[] yAxisData) throws Exception {
XWPFChart xChart = document.createChart(15 * Units.EMU_PER_CENTIMETER, 10 * Units.EMU_PER_CENTIMETER);
@ -40,7 +111,7 @@ public class BarChart
// 6、Y轴(值轴)相关设置
XDDFValueAxis yAxis = xChart.createValueAxis(AxisPosition.LEFT); // 创建Y轴,指定位置轴标题
XDDFNumericalDataSource<Double> yAxisSource = XDDFDataSourcesFactory.fromArray(yAxisData); // 设置Y轴数据
yAxis.setTitle("h");
yAxis.setTitle("k");
// 7、创建折线图对象
XDDFLineChartData lineChart = (XDDFLineChartData) xChart.createData(ChartTypes.LINE, xAxis, yAxis);
@ -77,7 +148,7 @@ public class BarChart
// Y轴(值轴)相关设置
//chart.createValueAxis(AxisPosition.LEFT);
XDDFValueAxis yAxis = chart.createValueAxis(AxisPosition.LEFT); // 创建Y轴,指定位置
yAxis.setTitle("Area(hm²)"); // Y轴标题
yAxis.setTitle("Area(km²)"); // Y轴标题
yAxis.setCrossBetween(AxisCrossBetween.BETWEEN); // 设置图柱的位置:BETWEEN居中
XDDFNumericalDataSource<Double> yAxisSource = XDDFDataSourcesFactory.fromArray(yAxisData); // 设置Y轴数据
ChartTypes chartTypes = ChartTypes.BAR;
@ -102,7 +173,7 @@ public class BarChart
(fileAddress)) {
run.addPicture(is, XWPFDocument.PICTURE_TYPE_PNG,
fileAddress,
Units.toEMU(185), Units.toEMU(223)); // 200x200 pixels
Units.toEMU(392), Units.toEMU(280)); // 200x200 pixels
} catch (Exception e) {
e.printStackTrace();
}
@ -163,8 +234,8 @@ public class BarChart
title.setAlignment(ParagraphAlignment.CENTER);
XWPFRun runTitle = title.createRun();
runTitle.setText(date);
runTitle.setBold(false);
runTitle.setFontSize(16);
runTitle.setBold(true);
runTitle.setFontSize(20);
runTitle.setFontFamily("Times New Roman");
}
//设置行间距
@ -179,6 +250,7 @@ public class BarChart
//1磅数是20
spacing.setLine(BigInteger.valueOf(size*20));
}
public static String getDoubleNumber(Double d)
{
DecimalFormat df = new DecimalFormat("#.00");