1、加入依赖
poi-4.1.2.jar
poi-ooxml-4.1.2.jar
poi-ooxml-schemas-4.1.2.jar
2、读取excel中每行的数据,使用LinkedHashMap<String, LinkedHashMap<String, Double>>类型的集合封装。
3、调用函数
/** 将excel中的行数据读取后,调用此函数
* @param workbooks poi工作谱
* @param sheets poi工作表
* @param cellRow 行号
* @param cellCol 列号
* @param datas 数据LinkedHashMap<表头, LinkedHashMap<互动量, 数据>>
* @param path 保存excel的路径
* @throws IOException
*/
public static void creatBrokenLin(Workbook workbooks, Sheet sheets, int cellRow, int cellCol,
LinkedHashMap<String, LinkedHashMap<String, Double>> datas, String path)
throws IOException {
String pathName = new File(path).getName();
if (pathName.endsWith(".xlsx")) {
CreatImageUtils1.create07(workbooks, sheets,cellRow,cellCol,datas,path);
} else if (pathName.endsWith(".xls")) {
CreatImageUtils1.create03(workbooks, sheets,cellRow,cellCol,datas,path);
}
}
/** 03版excel处理
* @param workbooks
* @param sheets
* @param cellRow
* @param cellCol
* @param datas
* @param path
* @throws IOException
*/
private static void create03(Workbook workbooks, Sheet sheets, int cellRow, int cellCol,
LinkedHashMap<String, LinkedHashMap<String, Double>> datas, String path)
throws IOException {
HSSFWorkbook workbook = (HSSFWorkbook) workbooks;
HSSFSheet sheet = (HSSFSheet) sheets;
// 创建字节输出流
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
//如 果不使用Font,中文将显示不出来
Font font = new Font("新宋体", Font.BOLD, 15);
// 创建数据
sheet.setColumnWidth(cellCol, 12000);
//sheet.setColumnWidth(cellCol - 1, 2000);
HSSFRow row = null;
row = sheet.getRow(cellRow);
if (row == null) {
row = sheet.createRow(cellRow);
}
row.setHeight((short) 4000);
JFreeChart chart =createPort("互动量", datas, "时间", "数量", font);
// 读取chart信息至字节输出流
ChartUtilities.writeChartAsPNG(byteArrayOut, chart, 600, 300);
// 画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
// anchor主要用于设置图片的属性
ClientAnchor anchor = patriarch.createAnchor(0, 0, 0, 0, (short) cellCol, cellRow, (short) (cellCol + 1), cellRow + 1);
// anchor.setAnchorType(3);
// 插入图片
patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));
// excel2003后缀
FileOutputStream fileOut = new FileOutputStream(path);
workbook.write(fileOut);
fileOut.close();
}
/** 07版excel处理
* @param workbooks
* @param sheets
* @param cellRow
* @param cellCol
* @param datas
* @param path
* @throws IOException
*/
private static void create07(Workbook workbooks, Sheet sheets, int cellRow, int cellCol, LinkedHashMap<String, LinkedHashMap<String, Double>> datas, String path) throws IOException {
XSSFWorkbook workbook = (XSSFWorkbook) workbooks;
XSSFSheet sheet = (XSSFSheet) sheets;
// 创建字节输出流
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
//如 果不使用Font,中文将显示不出来
Font font = new Font("新宋体", Font.BOLD, 15);
sheet.setColumnWidth(cellCol, 12000);
//sheet.setColumnWidth(cellCol - 1, 2000);
XSSFRow row = null;
row = sheet.getRow(cellRow);
if (row == null) {
row = sheet.createRow(cellRow);
}
row.setHeight((short) 4000);
JFreeChart chart = createPort("互动量", datas, "时间", "数量", font);
// 读取chart信息至字节输出流
ChartUtilities.writeChartAsPNG(byteArrayOut, chart, 600, 300);
// 画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
XSSFDrawing patriarch = sheet.createDrawingPatriarch();
// anchor主要用于设置图片的属性
ClientAnchor anchor = patriarch.createAnchor(5, 5, 5, 5, (short) cellCol, cellRow, (short) (cellCol + 1), cellRow + 1);
// anchor.setAnchorType(3);
// 插入图片
patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), XSSFWorkbook.PICTURE_TYPE_PNG));
// excel2003后缀
FileOutputStream fileOut = new FileOutputStream(path);
workbook.write(fileOut);
fileOut.close();
}
/** 生成统计图
* @param title 标题
* @param datas 所有数据
* @param type 横坐标
* @param unit 纵坐标
* @param font 字体样式
* @return
*/
private static JFreeChart createPort(String title, LinkedHashMap<String, LinkedHashMap<String, Double>> datas, String type, String unit, Font font) {
try {
DefaultCategoryDataset ds = new DefaultCategoryDataset();
Set<Map.Entry<String, LinkedHashMap<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;
}
}
4、生成结果

本文介绍如何使用Java的POI库,根据Excel数据行生成折线统计图。首先,需要引入相关依赖。接着,读取Excel文件,将每行数据存储为LinkedHashMap。然后,调用特定函数进行图表生成。最后,展示生成的折线图结果。

5639

被折叠的 条评论
为什么被折叠?



