导出excel,去掉图表;

This commit is contained in:
yu 2025-01-09 15:26:22 +08:00
parent 1b6ef3cc28
commit dacf6eda31
2 changed files with 13 additions and 123 deletions

View File

@ -32,7 +32,6 @@
<disruptor.version>3.4.4</disruptor.version> <disruptor.version>3.4.4</disruptor.version>
<aviator.version>5.4.3</aviator.version> <aviator.version>5.4.3</aviator.version>
<minio.version>8.4.3</minio.version> <minio.version>8.4.3</minio.version>
<jfreechart.version>1.5.3</jfreechart.version>
</properties> </properties>
<dependencies> <dependencies>
@ -217,11 +216,6 @@
<artifactId>minio</artifactId> <artifactId>minio</artifactId>
<version>${minio.version}</version> <version>${minio.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>${jfreechart.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -28,35 +28,18 @@ import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xddf.usermodel.chart.*; import org.apache.poi.xddf.usermodel.chart.*;
import org.apache.poi.xssf.usermodel.XSSFChart; import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
import org.apache.poi.xssf.usermodel.XSSFDrawing; import org.apache.poi.xssf.usermodel.XSSFDrawing;
import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.List;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
@ -105,13 +88,9 @@ public class StatisticalAnalysisServiceImpl implements StatisticalAnalysisServic
List<Map<String, Object>> dataList = new ArrayList<>(); List<Map<String, Object>> dataList = new ArrayList<>();
// 遍历数据填充Excel数据集 // 遍历数据填充Excel数据集
setTrendAnalyseExcelValue(mapsList, dataList); setTrendAnalyseExcelValue(mapsList, dataList);
//获取图表类别集
List<String> chartKey = getCharKey(map);
ExcelWriter writer = ExcelUtil.getWriter(RandomUtil.randomInt(100, 1000) + "statistics" + ".xlsx"); ExcelWriter writer = ExcelUtil.getWriter(RandomUtil.randomInt(100, 1000) + "statistics" + ".xlsx");
//设置Excel样式 //设置Excel样式
setExcelStyle(writer, map, dataList); setExcelStyle(writer, map, dataList);
//生成折线图
addChartToExcel(writer,dataList,StatisticalAnalysisConstant.TREND_ANALYSE,chartKey);
//下载Excel //下载Excel
downloadExcel(response, writer,StatisticalAnalysisConstant.TREND_ANALYSE); downloadExcel(response, writer,StatisticalAnalysisConstant.TREND_ANALYSE);
} }
@ -150,18 +129,9 @@ public class StatisticalAnalysisServiceImpl implements StatisticalAnalysisServic
setPowerCurveExcelValue(resultMap, dataList, curveItemEntitieList); setPowerCurveExcelValue(resultMap, dataList, curveItemEntitieList);
//获取功率曲线的列 //获取功率曲线的列
LinkedHashMap<String, String> map = getPowerCurveColumnName(param); LinkedHashMap<String, String> map = getPowerCurveColumnName(param);
//获取图表类别集
List<String> chartKey = getCharKey(map);
ExcelWriter writer = ExcelUtil.getWriter(RandomUtil.randomInt(100, 1000) + "statistics" + ".xlsx"); ExcelWriter writer = ExcelUtil.getWriter(RandomUtil.randomInt(100, 1000) + "statistics" + ".xlsx");
//设置Excel样式 //设置Excel样式
setExcelStyle(writer, map, dataList); setExcelStyle(writer, map, dataList);
//生成折线图
if (param.getDisplayCurve()==1){
addChartToExcel(writer,dataList,StatisticalAnalysisConstant.POWER_CURVE,chartKey);
}else {
//生成散点图
addScattersChartToExcel(writer,dataList,StatisticalAnalysisConstant.POWER_CURVE,chartKey);
}
//下载Excel //下载Excel
downloadExcel(response, writer,StatisticalAnalysisConstant.POWER_CURVE); downloadExcel(response, writer,StatisticalAnalysisConstant.POWER_CURVE);
} }
@ -191,15 +161,11 @@ public class StatisticalAnalysisServiceImpl implements StatisticalAnalysisServic
//自定义别名 别名的key和实体类中的名称要对应上 //自定义别名 别名的key和实体类中的名称要对应上
LinkedHashMap<String, String> map = gettrendContrastColumnName(param); LinkedHashMap<String, String> map = gettrendContrastColumnName(param);
List<Map<String, Object>> dataList = new ArrayList<>(); List<Map<String, Object>> dataList = new ArrayList<>();
//图表类别集
List<String> charKey = getCharKey(map);
// 遍历数据将数据添加到dataList中 // 遍历数据将数据添加到dataList中
setTrendContrastExcelValue(maps, dataList); setTrendContrastExcelValue(maps, dataList);
ExcelWriter writer = ExcelUtil.getWriter(RandomUtil.randomInt(100, 1000) + "statistics" + ".xlsx"); ExcelWriter writer = ExcelUtil.getWriter(RandomUtil.randomInt(100, 1000) + "statistics" + ".xlsx");
//设置Excel样式 //设置Excel样式
setExcelStyle(writer, map, dataList); setExcelStyle(writer, map, dataList);
//生成折线图
addChartToExcel(writer,dataList,StatisticalAnalysisConstant.TREND_CONTRAST,charKey);
//下载Excel //下载Excel
downloadExcel(response, writer,StatisticalAnalysisConstant.TREND_CONTRAST); downloadExcel(response, writer,StatisticalAnalysisConstant.TREND_CONTRAST);
} }
@ -284,22 +250,7 @@ public class StatisticalAnalysisServiceImpl implements StatisticalAnalysisServic
} }
} }
/**
* 获取图表类别集
* @param map Excel的列
* @return 图表类别集
*/
private List<String> getCharKey(LinkedHashMap<String, String> map) {
//获取图表类别集
List<String> chartKey = new ArrayList<>();
for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
String value = stringStringEntry.getValue();
if (!value.contains("时间")){
chartKey.add(value);
}
}
return chartKey;
}
/** /**
* 趋势对比填充Excel数据集 * 趋势对比填充Excel数据集
@ -542,75 +493,20 @@ public class StatisticalAnalysisServiceImpl implements StatisticalAnalysisServic
} }
/** /**
* 使用JFreeChart生成折线图(图片),已弃用 * 获取图表类别集
* @param data excel数据集 * @param map Excel的列
* @param writer excel对象 * @return 图表类别集
* @param dataset 图表数据集
* @param titleStr 标题
*/ */
private void createChart(List<Map<String, Object>> data, ExcelWriter writer, private List<String> getCharKey(LinkedHashMap<String, String> map) {
DefaultCategoryDataset dataset, String titleStr) { //获取图表类别集
// 获取Sheet对象 List<String> chartKey = new ArrayList<>();
XSSFSheet xssfSheet = (XSSFSheet) writer.getSheet(); for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
Workbook workbook = writer.getWorkbook(); String value = stringStringEntry.getValue();
JFreeChart chart = ChartFactory.createLineChart( if (!value.contains("时间")){
titleStr, // 图表标题 chartKey.add(value);
"", // 横轴标签 }
"", // 纵轴标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向
true, // 是否显示图例
true, // 是否使用工具提示
false // 是否生成URL链接
);
// 设置图表标题的字体
TextTitle title = chart.getTitle();
title.setFont(new java.awt.Font("SimSun", java.awt.Font.BOLD, 16));
// 获取图表的绘图区域
CategoryPlot plot = chart.getCategoryPlot();
// 设置横轴标签
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelFont(new java.awt.Font("SimSun", java.awt.Font.PLAIN, 12));
domainAxis.setMaximumCategoryLabelLines(1); // 可以控制标签行数
domainAxis.setCategoryMargin(3); // 控制类别之间的间距
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 旋转横轴标签为45度,90度为:UP_90
domainAxis.setLabelFont(new Font("SansSerif", Font.PLAIN, 7));//调整字体大小
// if (data.size() > 50) {
// domainAxis.setVisible(false); // 隐藏横坐标
// }
// 设置图例的字体
LegendTitle legend = chart.getLegend();
if (legend != null) {
legend.setItemFont(new java.awt.Font("SimSun", java.awt.Font.PLAIN, 12));
} }
// 设置绘图区域的背景颜色 return chartKey;
plot.setBackgroundPaint(Color.WHITE);
// 设置绘图区域的边框
plot.setOutlinePaint(Color.LIGHT_GRAY);
plot.setOutlineVisible(true);
// 设置网格线的颜色
plot.setDomainGridlinePaint(Color.LIGHT_GRAY);
plot.setRangeGridlinePaint(Color.LIGHT_GRAY);
// 设置线条的宽度
CategoryItemRenderer renderer1 = plot.getRenderer();
renderer1.setSeriesStroke(0, new BasicStroke(2.0f)); // 设置线条宽度
// 将图表保存为 PNG 文件
String chartFilePath = "lineChart.png";
// 调整图表尺寸
int width = 750;
int height = 400;
try {
ChartUtils.saveChartAsPNG(new File(chartFilePath), chart, width, height);
byte[] bytes = java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(chartFilePath));
int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
// 计算数据的最后一行,创建图表插入位置的锚点
XSSFClientAnchor anchor = new XSSFClientAnchor(0, 1, 0, 1, 0, data.size() + 2, 15, data.size() + 30);
XSSFDrawing drawing = xssfSheet.createDrawingPatriarch();
drawing.createPicture(anchor, pictureIdx);
} catch (IOException e) {
throw new ServiceException("图表保存失败==" + e);
}
} }
/** /**