因为nbcio-boot项目用到了poi5.0.0,但本身我看autoPOI现在只能支持poi4.12,所以需要改造这个jar,以便使用,随便也上传一下到maven中央库里去。
主要如下:
1、POM父文件修改成自己的版本,同时根据上传maven库要求进行修改;
2、修改cell.getCellTypeEnum()为cell.getCellType(),这个很多文件都要用到;都需要修改;
3、getNumberOfFonts类型变了,由short变成int了,需要修改;
4、ExcelChartBuildService这个文件需要修改,这里修改的地方比较多,所以放在后面了,这样就可以试用poi5.0.0了
/** * */ package org.jeecgframework.poi.excel.graph.builder; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.StringUtils; //import org.apache.poi.ss.usermodel.Chart; import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.Drawing; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xddf.usermodel.chart.*; import org.apache.poi.xssf.usermodel.XSSFSheet; /*import org.apache.poi.ss.usermodel.charts.AxisCrosses; import org.apache.poi.ss.usermodel.charts.AxisPosition; import org.apache.poi.ss.usermodel.charts.ChartAxis; import org.apache.poi.ss.usermodel.charts.ChartDataSource; import org.apache.poi.ss.usermodel.charts.ChartLegend; import org.apache.poi.ss.usermodel.charts.DataSources; import org.apache.poi.ss.usermodel.charts.LegendPosition; import org.apache.poi.ss.usermodel.charts.LineChartData; import org.apache.poi.ss.usermodel.charts.ScatterChartData; import org.apache.poi.ss.usermodel.charts.ValueAxis;*/ import org.apache.poi.ss.util.CellRangeAddress; import org.jeecgframework.poi.excel.graph.constant.ExcelGraphElementType; import org.jeecgframework.poi.excel.graph.constant.ExcelGraphType; import org.jeecgframework.poi.excel.graph.entity.ExcelGraph; import org.jeecgframework.poi.excel.graph.entity.ExcelGraphElement; import org.jeecgframework.poi.excel.graph.entity.ExcelTitleCell; import org.jeecgframework.poi.util.PoiCellUtil; import org.jeecgframework.poi.util.PoiExcelGraphDataUtil; /** * @Description * @author liusq * @data 2022年1月4号 */ public class ExcelChartBuildService { /** * * @param workbook * @param graphList * @param build 通过实时数据行来重新计算图形定义 * @param append */ public static void createExcelChart(Workbook workbook, List<ExcelGraph> graphList, Boolean build, Boolean append) { if(workbook!=null&&graphList!=null){ //设定默认第一个sheet为数据项 Sheet dataSouce=workbook.getSheetAt(0); if(dataSouce!=null){ buildTitle(dataSouce,graphList); if(build){ PoiExcelGraphDataUtil.buildGraphData(dataSouce, graphList); } if(append){ buildExcelChart(dataSouce, dataSouce, graphList); }else{ Sheet sheet=workbook.createSheet("图形界面"); buildExcelChart(dataSouce, sheet, graphList); } } } } /** * 构建基础图形 * @param drawing * @param anchor * @param dataSouce * @param graph */ private static void buildExcelChart(Drawing drawing,ClientAnchor anchor,Sheet dataSouce,ExcelGraph graph){ XDDFChart chart = null; // TODO 图表没有成功 //drawing.createChart(anchor); XDDFChartLegend legend = chart.getOrAddLegend(); legend.setPosition(LegendPosition.TOP_RIGHT); XDDFChartAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM); XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT); leftAxis.setCrosses(AxisCrosses.AUTO_ZERO); ExcelGraphElement categoryElement=graph.getCategory(); XDDFDataSource categoryChart; if(categoryElement!=null&& categoryElement.getElementType().equals(ExcelGraphElementType.STRING_TYPE)){ categoryChart=XDDFDataSourcesFactory.fromStringCellRange((XSSFSheet) dataSouce, new CellRangeAddress(categoryElement.getStartRowNum(),categoryElement.getEndRowNum(),categoryElement.getStartColNum(),categoryElement.getEndColNum())); }else{ categoryChart=XDDFDataSourcesFactory.fromNumericCellRange((XSSFSheet) dataSouce, new CellRangeAddress(categoryElement.getStartRowNum(),categoryElement.getEndRowNum(),categoryElement.getStartColNum(),categoryElement.getEndColNum())); } List<ExcelGraphElement> valueList=graph.getValueList(); List<XDDFDataSource<Double>> chartValueList= new ArrayList<>(); if(valueList!=null&&valueList.size()>0){ for(ExcelGraphElement ele:valueList){ XDDFDataSource<Double> source=XDDFDataSourcesFactory.fromNumericCellRange((XSSFSheet) dataSouce, new CellRangeAddress(ele.getStartRowNum(),ele.getEndRowNum(),ele.getStartColNum(),ele.getEndColNum())); chartValueList.add(source); } } if(graph.getGraphType().equals(ExcelGraphType.LINE_CHART)){ //buildLineChartData(data, categoryChart, chartValueList, graph.getTitle()); XDDFLineChartData data = (XDDFLineChartData) chart.createData(ChartTypes.LINE, bottomAxis, leftAxis); chart.plot(data); } else { XDDFScatterChartData data= (XDDFScatterChartData) chart.createData(ChartTypes.SCATTER, bottomAxis, leftAxis); buildScatterChartData(data, categoryChart, chartValueList,graph.getTitle()); chart.plot(data); } } /** * 构建多个图形对象 * @param dataSouce * @param dataSouce2 * @param graphList */ private static void buildExcelChart(Sheet dataSouce,Sheet dataSouce2,List<ExcelGraph> graphList){ int len=graphList.size(); if(len==1) { buildExcelChart(dataSouce, dataSouce2, (List<ExcelGraph>) graphList.get(0)); } else { int drawStart=0; int drawEnd=20; Drawing drawing = PoiExcelGraphDataUtil.getDrawingPatriarch(dataSouce2); for(int i=0;i<len;i++){ ExcelGraph graph=graphList.get(i); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, drawStart, 15, drawEnd); buildExcelChart(drawing, anchor, dataSouce, graph); drawStart=drawStart+drawEnd; drawEnd=drawEnd+drawEnd; } } } /** * 构建图形对象 * @param dataSourceSheet * @param tragetSheet * @param graph */ private static void buildExcelChart(XSSFSheet dataSourceSheet,Sheet tragetSheet,ExcelGraph graph){ Drawing drawing = PoiExcelGraphDataUtil.getDrawingPatriarch(tragetSheet); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 0, 15, 20); buildExcelChart(drawing, anchor, dataSourceSheet, graph); } /** * 构建Title * @param sheet * @param graph */ private static void buildTitle(Sheet sheet,ExcelGraph graph){ int cellTitleLen=graph.getTitleCell().size(); int titleLen=graph.getTitle().size(); if(titleLen>0){ }else{ for(int i=0;i<cellTitleLen;i++){ ExcelTitleCell titleCell=graph.getTitleCell().get(i); if(titleCell!=null){ graph.getTitle().add(PoiCellUtil.getCellValue(sheet,titleCell.getRow(),titleCell.getCol())); } } } } /** * 构建Title * @param sheet * @param graphList */ private static void buildTitle(Sheet sheet,List<ExcelGraph> graphList){ if(graphList!=null&&graphList.size()>0){ for(ExcelGraph graph:graphList){ if(graph!=null) { buildTitle(sheet, graph); } } } } /** * * @param data * @param categoryChart * @param chartValueList * @param title */ private static void buildLineChartData(XDDFLineChartData data,XDDFDataSource categoryChart,List<XDDFDataSource<Number>> chartValueList,List<String> title){ if(chartValueList.size()==title.size()) { int len=title.size(); for(int i=0;i<len;i++){ //TODO 更新版本 //data.addSerie(categoryChart, chartValueList.get(i)).setTitle(title.get(i)); } } else { int i=0; for(XDDFDataSource<Number> source:chartValueList){ String temp_title=title.get(i); if(StringUtils.isNotBlank(temp_title)){ //data.addSerie(categoryChart, source).setTitle(_title); }else{ //data.addSerie(categoryChart, source); } } } } /** * * @param data * @param categoryChart * @param chartValueList * @param title */ private static void buildScatterChartData(XDDFScatterChartData data,XDDFDataSource categoryChart,List<XDDFDataSource<Double>> chartValueList,List<String> title){ if(chartValueList.size()==title.size()) { int len=title.size(); for(int i=0;i<len;i++){ data.addSeries(categoryChart, (XDDFNumericalDataSource<? extends Number>) chartValueList.get(i)).setTitle(title.get(i).toString(), null); } } else { int i=0; for(XDDFDataSource<Double> source:chartValueList){ String temp_title=title.get(i); if(StringUtils.isNotBlank(temp_title)){ data.addSeries(categoryChart, (XDDFNumericalDataSource<? extends Number>) source).setTitle(temp_title,null); }else{ data.addSeries(categoryChart, (XDDFNumericalDataSource<? extends Number>) source); } } } } }