在Word中创建图表
用法
Word中创建图表的方式是一样的
XWPFChart和XSSFChart 一样都是继承自XDDFChart的,因此同样可以用chartBuilder构建数据,然后填充.
ChartFromArrayBuilder等都可以用
案例:把Excel中的那两个案例再用Word实现一遍
1.疫情统计
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| String[] lines = IOHelpers.readAllLines("D:/JavaTest1/疫情/疫情.txt"); String[] countries=new String[lines.length]; Double[] 确诊数量 = new Double[lines.length]; Double[] 死亡数量 =new Double[lines.length]; for (int i =0;i< lines.length;i++) { String line =lines[i]; String[] datas = line.split("\\s"); String 国家 =datas[0]; Double 确诊 = Double.parseDouble(datas[1]); Double 死亡 = Double.parseDouble(datas[2]); countries[i]=国家; 确诊数量[i]=确诊; 死亡数量[i]=死亡; } XWPFDocument doc = WordHelpers.createDocxDocument(); XWPFChart chart = WordHelpers.createChart(doc, 400, 400); ChartFromArrayBuilder<Double> chartBuilder = new ChartFromArrayBuilder<>(ChartTypes.BAR); chart.setTitleText("疫情统计"); chart.getOrAddLegend().setPosition(LegendPosition.LEFT); chartBuilder.setCategoryNames(countries); chartBuilder.putValues("确诊数量",确诊数量); chartBuilder.putValues("死亡数量",死亡数量); chartBuilder.build(chart); WordHelpers.saveToFile(doc,"D:/JavaTest1/疫情/疫情.docx"); WordHelpers.close(doc);
|
2.收入统计
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| Workbook wb = ExcelHelpers.openFile("D:/JavaTest1/公司账目2021年.xlsx"); int sheetCount = wb.getNumberOfSheets(); String[] monthNames = new String[sheetCount]; Double[] sales = new Double[sheetCount]; for(int sheetIndex = 0;sheetIndex<sheetCount;sheetIndex++) { Sheet sheet = wb.getSheetAt(sheetIndex); String month = sheet.getSheetName(); monthNames[sheetIndex]=month; double 学费总额=0; for(int rowIndex =1;rowIndex<=sheet.getLastRowNum();rowIndex++) { String 收支类别 = ExcelHelpers.getCellStringValue(sheet,rowIndex,2); if(收支类别.equals("学费收入")) { double 金额= ExcelHelpers.getCellDoubleValue(sheet,rowIndex,3); 学费总额+=金额; } } sales[sheetIndex] = 学费总额; System.out.println(month+":"+学费总额); }
XWPFDocument doc = WordHelpers.createDocxDocument(); XWPFChart chart = WordHelpers.createChart(doc, 400, 400); ChartFromArrayBuilder<Double> chartBuilder = new ChartFromArrayBuilder<>(ChartTypes.LINE); chartBuilder.setCategoryNames(monthNames); chartBuilder.putValues("学费收入",sales); chartBuilder.build(chart);
WordHelpers.saveToFile(doc,"D:/JavaTest1/学费收入/1.docx"); WordHelpers.close(doc);
ExcelHelpers.close(wb);
|