实现效果
测试类
public class ChartTest { // 开始行 public static int chartRowStart = 3; // 结束行 public static int chartRowEnd = 20; public static ChartPosition chartPosition; public static void main(String[] args) throws IOException { // 填充数据 XSSFWorkbook workbook = createExcel(); FileOutputStream fileOut = null; try { // 将输出写入excel文件 String filename = UUID.randomUUID() + ".xlsx"; fileOut = new FileOutputStream(filename); workbook.write(fileOut); DesktopHelpers.openFile(filename); } catch (Exception e) { e.printStackTrace(); } finally { workbook.close(); if (fileOut != null) { fileOut.close(); } } } public static XSSFWorkbook createExcel() { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet(); //-------------------------折线图-------------------------- List lineCharts = initLineChart(); for (LineChart lineChart : lineCharts) { // 图表位置(左上角坐标,右下角坐标) 左上角坐标的(列,行),(右下角坐标)列,行,偏移量均为0 chartPosition = new ChartPosition() .setRow1(chartRowStart) .setCol1(0) .setRow2(chartRowEnd) .setCol2(lineChart.getXAxisList().size() + 3); ChartUtils.createLine(sheet, chartPosition, lineChart); } chartRowStart = chartRowEnd + 2; chartRowEnd = chartRowStart + 20; //-------------------------柱状图-------------------------- XSSFRow row = sheet.createRow(chartRowStart); row.setHeight((short) 500); XSSFCell cell0 = row.createCell(0); cell0.setCellValue("测试柱状图"); cell0.setCellStyle(ChartUtils.tableNameCellStyle(workbook)); sheet.addMergedRegion(new CellRangeAddress(chartRowStart, chartRowStart, 0, 2)); // 获取数据 List pieCharts = initPieChart(); for (PieChart pieChart : pieCharts) { // 图表位置(左上角坐标,右下角坐标) 左上角坐标的(列,行),(右下角坐标)列,行,偏移量均为0 chartPosition = new ChartPosition() .setRow1(chartRowStart + 1) .setCol1(0) .setRow2(chartRowEnd) .setCol2(8); ChartUtils.createBar(sheet, chartPosition, pieChart); } chartRowStart = chartRowEnd + 2; chartRowEnd = chartRowStart + 15; //-------------------------饼图-------------------------- XSSFRow row1 = sheet.createRow(chartRowStart); row1.setHeight((short) 500); XSSFCell cell1 = row1.createCell(0); cell1.setCellValue("测试饼图"); cell1.setCellStyle(ChartUtils.tableNameCellStyle(workbook)); sheet.addMergedRegion(new CellRangeAddress(chartRowStart, chartRowStart, 0, 2)); // 获取数据 for (PieChart pieChart : pieCharts) { // 图表位置(左上角坐标,右下角坐标) 左上角坐标的(列,行),(右下角坐标)列,行,偏移量均为0 chartPosition = new ChartPosition() .setRow1(chartRowStart + 1) .setCol1(0) .setRow2(chartRowEnd) .setCol2(8); ChartUtils.createPie(sheet, chartPosition, pieChart); } chartRowStart = chartRowEnd + 2; chartRowEnd = chartRowStart + 15; //-------------------------Excel-------------------------- // 获取列表数据 ChartUtils.createTable(chartRowStart, workbook, sheet); // 去除网格线 // sheet.setDisplayGridlines(false); return workbook; } public static List initLineChart() { List lineCharts = new ArrayList<>(); lineCharts.add(new LineChart() .setChartTitle("折线图") .setTitleList(Arrays.asList("2020年", "2021年")) .setDataList(Arrays.asList(Arrays.asList(1, 2, 3, 3, 1, 6, 3, 7, 12, 11, null, null), Arrays.asList(5, 4, 0, null, 12, 3, 8, 9, 11, 9, 2, 1))) .setXAxisList(Arrays.
实体类
@Accessors(chain = true)@Datapublic class ChartPosition { private int col1; private int row1; private int col2; private int row2; private int dx1 = 0; private int dy1 = 0; private int dx2 = 0; private int dy2 = 0;}
@Data@Accessors(chain = true)public class LineChart { private String chartTitle; private List titleList; private List> dataList; private List
@Data@Accessors(chain = true)public class PieChart { private List titleList; private List dataList; private String titleName;}
工具类
public class ChartUtils { private static XSSFChart createDrawingPatriarch(XSSFSheet sheet, ChartPosition chartPosition, String chartTitle) { //创建一个画布 XSSFDrawing drawing = sheet.createDrawingPatriarch(); //前偏移量四个默认0 XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, chartPosition.getCol1(), chartPosition.getRow1(), chartPosition.getCol2(), chartPosition.getRow2()); //创建一个chart对象 XSSFChart chart = drawing.createChart(anchor); //标题 chart.setTitleText(chartTitle); //标题是否覆盖图表 chart.setTitleOverlay(false); return chart; } public static void createBar(XSSFSheet sheet,ChartPosition chartPosition, PieChart pieChart){ String titleName = pieChart.getTitleName(); List titleList = pieChart.getTitleList(); List dataList = pieChart.getDataList(); XSSFChart chart = createDrawingPatriarch(sheet, chartPosition, titleName); //分类轴标(X轴),标题位置 XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM); //值(Y轴)轴,标题位置 XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT); //分类轴标数据 XDDFDataSource xData = XDDFDataSourcesFactory.fromArray(titleList.toArray(new String[]{})); XDDFNumericalDataSource values = XDDFDataSourcesFactory.fromArray(dataList.toArray(new Integer[]{})); //bar:条形图 XDDFBarChartData bar = (XDDFBarChartData) chart.createData(ChartTypes.BAR, bottomAxis, leftAxis); leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN); //设置为可变颜色 bar.setVaryColors(true); //条形图方向,纵向/横向:纵向 bar.setBarDirection(BarDirection.COL); //图表加载数据,条形图1 XDDFBarChartData.Series series1 = (XDDFBarChartData.Series) bar.addSeries(xData, values); //条形图例标题 XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(PresetColor.BLUE_VIOLET)); //条形图,填充颜色 series1.setFillProperties(fill); //绘制 chart.plot(bar); } public static void createPie(XSSFSheet sheet, ChartPosition chartPosition, PieChart pieChart) { String titleName = pieChart.getTitleName(); List titleList = pieChart.getTitleList(); List dataList = pieChart.getDataList(); XSSFChart chart = createDrawingPatriarch(sheet, chartPosition, titleName); //图例位置 XDDFChartLegend legend = chart.getOrAddLegend(); legend.setPosition(LegendPosition.BOTTOM); //分类轴标数据 XDDFDataSource countries = XDDFDataSourcesFactory.fromArray(titleList.toArray(new String[]{})); XDDFNumericalDataSource values = XDDFDataSourcesFactory.fromArray(dataList.toArray(new Integer[]{})); XDDFChartData data = chart.createData(ChartTypes.PIE, null, null); //设置为可变颜色 data.setVaryColors(true); //图表加载数据 data.addSeries(countries, values); //绘制 chart.plot(data); CTDLbls ctdLbls = chart.getCTChart().getPlotArea().getPieChartArray(0).getSerArray(0).addNewDLbls(); ctdLbls.addNewShowVal().setVal(false); ctdLbls.addNewShowLegendKey().setVal(false); //类别名称 ctdLbls.addNewShowCatName().setVal(false); //百分比 ctdLbls.addNewShowSerName().setVal(false); ctdLbls.addNewShowPercent().setVal(true); //引导线 ctdLbls.addNewShowLeaderLines().setVal(false); //分隔符为分行符 ctdLbls.setSeparator("\n"); //数据标签内 ctdLbls.addNewDLblPos().setVal(STDLblPos.Enum.forString("inEnd")); } public static void createLine(XSSFSheet sheet, ChartPosition chartPosition, LineChart lineChart) { List
完整代码地址:https://gitee.com/Szw99/create-excel.git
来源地址:https://blog.csdn.net/weixin_42151235/article/details/131169539