java导出数据生成图表至excel
使用jfree图表绘制类库绘制图表,并生成到本地或读取至输出流用于在excel中生成;
使用poi操作excel;
依赖
使用maven管理jar包,主要包含poi和jfree:
<!-- POI开始 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.17</version>
</dependency>
<!-- POI结束-->
<!-- 生成图标工具 -->
<!-- https://mvnrepository.com/artifact/jfree/jcommon -->
<dependency>
<groupId>jfree</groupId>
<artifactId>jcommon</artifactId>
<version>1.0.16</version>
</dependency>
<dependency>
<groupId>jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.13</version>
</dependency>
生成饼图
public static JFreeChart createPort(String title, Map<String, Integer> map, Font font) {
try {
Set<Entry<String, Integer>> set = map.entrySet();
DefaultPieDataset pds = new DefaultPieDataset();
Iterator iterator = set.iterator();
Map.Entry entry;
while (iterator.hasNext()) {
entry = (Map.Entry) iterator.next();
pds.setValue(entry.getKey().toString(), Double.parseDouble(entry.getValue().toString()));
}
// 生成一个饼图的图表:显示图表的标题、组装的数据、是否显示图例、是否生成贴士以及是否生成URL链接
JFreeChart chart = ChartFactory.createPieChart(title, pds, true, false, true);
// 设置图片标题的字体
chart.getTitle().setFont(font);
// 得到图块,准备设置标签的字体
PiePlot plot = (PiePlot) chart.getPlot();
//设置分裂效果,需要指定分裂出去的key
// plot.setExplodePercent("摄像机", 0.1); 分裂效果,可选
// 设置标签字体
plot.setLabelFont(font);
// 设置图例项目字体
chart.getLegend().setItemFont(font);
// 设置开始角度
// plot.setStartAngle(new Float(3.14f / 2f)); 开始角度,意义不大
//设置plot的前景色透明度
plot.setForegroundAlpha(0.7f);
//设置plot的背景色透明度
plot.setBackgroundAlpha(0.0f);
//设置标签生成器(默认{0})
//{0}:key {1}:value {2}:百分比 {3}:sum
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1})/{2}")); // 一般在{1}后面加单位,如:{0}({1}次)/{2}
//将内存中的图片写到本地硬盘
// ChartUtilities.saveChartAsJPEG(new File("H:/a.png"), chart, 600, 300);
return chart;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/** * 设备设施 台账页面导出报表 */
@RequestMapping(value = "/exportData", produces = "text/html;charset=UTF-8")
@ResponseBody
public String exportData(HttpServletRequest request, Model model, HttpServletResponse response)
throws IOException
{
// excel2003工作表
HSSFWorkbook wb = new HSSFWorkbook();
// 创建工作表
HSSFSheet sheet = wb.createSheet("Sheet 1");
// 创建字节输出流
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
//如 果不使用Font,中文将显示不出来
Font font = new Font("新宋体", Font.BOLD, 15);
// 创建数据:饼状图就是名称和值(比例)
Map<String, Double> map=new HashMap<String, Double>();
map.put("路口行人标志", (double) 1000);
map.put("编码器", (double) 700);
map.put("摄像机", (double) 600);
map.put("流转码服务器", (double) 400);
// 创建JFreeChart
JFreeChart chart = createPort("故障设备类型比例", map, font);
// 读取chart信息至字节输出流
ChartUtilities.writeChartAsPNG(byteArrayOut, chart, 600, 300);
// 画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
// anchor主要用于设置图片的属性
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 2, (short) 1, (short) 12, (short) 15);
anchor.setAnchorType(3);
// 插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
// excel2003后缀
FileOutputStream fileOut = new FileOutputStream("h://myExcel4.xls");
wb.write(fileOut);
fileOut.close();
}
效果图:
补充:
之前只写了一个饼图的导出,今天下午不太忙就试了一下线图和柱图的导出代码。
线图
public static void main(String[] args) throws Exception {
// excel2003工作表
HSSFWorkbook wb = new HSSFWorkbook();
// 创建工作表
HSSFSheet sheet = wb.createSheet("Sheet 1");
// 创建字节输出流
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
//如 果不使用Font,中文将显示不出来
Font font = new Font("新宋体", Font.BOLD, 15);
// 创建数据
Map<String, Map<String, Double>> datas =new HashMap<String, Map<String,Double>>();
Map<String, Double> map1=new HashMap<String, Double>();
Map<String, Double> map2=new HashMap<String, Double>();
Map<String, Double> map3=new HashMap<String, Double>();
Map<String, Double> map4=new HashMap<String, Double>();
Map<String, Double> map5=new HashMap<String, Double>();
map1.put("设备数量", (double) 1);
map2.put("设备数量", (double) 2);
map3.put("设备数量", (double) 3);
map4.put("设备数量", (double) 2);
map5.put("设备数量", (double) 1);
//压入数据
datas.put("转角护栏", map1);
datas.put("标线", map2);
datas.put("卡口", map3);
datas.put("行人标志", map4);
datas.put("指路标志", map5);
JFreeChart chart = createPort("设备数量", datas, "设备类型", "设备数量", font);
// 读取chart信息至字节输出流
ChartUtilities.writeChartAsPNG(byteArrayOut, chart, 600, 300);
// 画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
// anchor主要用于设置图片的属性
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 2, (short) 1, (short) 12, (short) 15);
// anchor.setAnchorType(3);
// 插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
// excel2003后缀
FileOutputStream fileOut = new FileOutputStream("E://myExcel5.xls");
wb.write(fileOut);
fileOut.close();
}
public static JFreeChart createPort(String title, Map<String, Map<String, Double>> datas, String type, String unit, Font font) {
try {
DefaultCategoryDataset ds = new DefaultCategoryDataset();
Set<Map.Entry<String, Map<String, Double>>> set1 = datas.entrySet();
Iterator iterator1 = set1.iterator();
Iterator iterator2;
HashMap<String, Double> map;
Set<Map.Entry<String, Double>> set2;
Map.Entry entry1;
Map.Entry entry2;
while (iterator1.hasNext()) {
entry1 = (Map.Entry) iterator1.next();
map = (HashMap<String, Double>) entry1.getValue();
set2 = map.entrySet();
iterator2 = set2.iterator();
while (iterator2.hasNext()) {
entry2 = (Map.Entry) iterator2.next();
ds.setValue(Double.parseDouble(entry2.getValue().toString()), entry2.getKey().toString(), entry1.getKey().toString());
}
}
//创建折线图,折线图分水平显示和垂直显示两种
// //2D折线图
JFreeChart chart = ChartFactory.createLineChart(title, type, unit, ds, PlotOrientation.VERTICAL, true, true, true);
// //3D折线图
// JFreeChart chart2 = ChartFactory.createLineChart3D(title, type, unit, ds, PlotOrientation.VERTICAL, true, true, false);
//设置整个图片的标题字体
chart.getTitle().setFont(font);
//设置提示条字体
font = new Font("宋体", Font.BOLD, 15);
chart.getLegend().setItemFont(font);
//得到绘图区
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//得到绘图区的域轴(横轴),设置标签的字体
plot.getDomainAxis().setLabelFont(font);
//设置横轴标签项字体
plot.getDomainAxis().setTickLabelFont(font);
//设置范围轴(纵轴)字体
font = new Font("宋体", Font.BOLD, 18);
plot.getRangeAxis().setLabelFont(font);
// plot.setForegroundAlpha(1.0f);
return chart;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
效果图:
柱图:
public static void main(String[] args) throws Exception {
// excel2003工作表
HSSFWorkbook wb = new HSSFWorkbook();
// 创建工作表
HSSFSheet sheet = wb.createSheet("Sheet 1");
// 创建字节输出流
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
//如 果不使用Font,中文将显示不出来
Font font = new Font("新宋体", Font.BOLD, 15);
// 创建数据
Map<String, Map<String, Integer>> datas =new HashMap<String, Map<String,Integer>>();
Map<String, Integer> map1=new HashMap<String, Integer>();
Map<String, Integer> map2=new HashMap<String, Integer>();
Map<String, Integer> map3=new HashMap<String, Integer>();
Map<String, Integer> map4=new HashMap<String, Integer>();
Map<String, Integer> map5=new HashMap<String, Integer>();
map1.put("设备数", 1);
map2.put("设备数", 3);
map3.put("设备数", 1);
map4.put("设备数", 2);
map5.put("设备数", 1);
//压入数据
datas.put("转角护栏", map1);
datas.put("标线", map2);
datas.put("行人标志", map3);
datas.put("卡口", map4);
datas.put("指路标志", map5);
JFreeChart chart = createPort("设备类型比例",datas,"设备类型","设备数",font);
// 读取chart信息至字节输出流
ChartUtilities.writeChartAsPNG(byteArrayOut, chart, 600, 300);
// 画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
// anchor主要用于设置图片的属性
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 2, (short) 1, (short) 12, (short) 15);
// 插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
// excel2003后缀
FileOutputStream fileOut = new FileOutputStream("E://exportExcle.xls");
wb.write(fileOut);
fileOut.close();
}
public static JFreeChart createPort(String title,Map<String,Map<String,Integer>> datas,String type,String danwei,Font font){
try {
//种类数据集
DefaultCategoryDataset ds = new DefaultCategoryDataset();
//获取迭代器:
Set<Map.Entry<String, Map<String, Integer>>> set1 = datas.entrySet();
Iterator iterator1= set1.iterator();
Iterator iterator2;
HashMap<String, Double> map;
Set<Map.Entry<String,Double>> set2;
Map.Entry entry1;
Map.Entry entry2;
while(iterator1.hasNext()){
entry1=(Map.Entry) iterator1.next();
map=(HashMap<String, Double>) entry1.getValue();
set2=map.entrySet();
iterator2=set2.iterator();
while (iterator2.hasNext()) {
entry2= (Map.Entry) iterator2.next();
ds.setValue(Double.parseDouble(entry2.getValue().toString()), entry2.getKey().toString(), entry1.getKey().toString());
}
}
//创建柱状图,柱状图分水平显示和垂直显示两种
JFreeChart chart = ChartFactory.createBarChart(title, type, danwei, ds, PlotOrientation.VERTICAL, true, true, true);
//设置整个图片的标题字体
chart.getTitle().setFont(font);
//设置提示条字体
font = new Font("宋体", Font.BOLD, 15);
chart.getLegend().setItemFont(font);
//得到绘图区
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//得到绘图区的域轴(横轴),设置标签的字体
plot.getDomainAxis().setLabelFont(font);
//设置横轴标签项字体
plot.getDomainAxis().setTickLabelFont(font);
//设置范围轴(纵轴)字体
plot.getRangeAxis().setLabelFont(font);
plot.setForegroundAlpha(1.0f);
return chart;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
效果图
原文作者:散装程序猿
原文地址: https://blog.csdn.net/qq_42684504/article/details/107951543
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://blog.csdn.net/qq_42684504/article/details/107951543
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
相关文章