使用Apache poi 导出excel含有柱状图

使用poi 在excel中使用chart绘制出柱状图

需要先在maven中将依赖引入

        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poiartifactId>
           <version>4.1.2version>
        dependency>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poi-ooxmlartifactId>
            <version>4.1.2version>
        dependency>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>poi-ooxml-schemasartifactId>
            <version>4.1.2version>
        dependency>
        <dependency>
            <groupId>org.apache.poigroupId>
            <artifactId>openxml4jartifactId>
            <version>1.0-betaversion>
        dependency>

下面就是代码实例

package com.huasu.resAndDev.excelTools;

import com.huasu.resAndDev.commonTools.ConstantKit;
import com.huasu.resAndDev.commonTools.ResponseResult;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xddf.usermodel.PresetColor;
import org.apache.poi.xddf.usermodel.XDDFColor;
import org.apache.poi.xddf.usermodel.XDDFSolidFillProperties;
import org.apache.poi.xddf.usermodel.chart.*;
import org.apache.poi.xssf.usermodel.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * ClassName: ExcelChartUtils 
* Description: 导出excel中绘制图表
* date: 2022/11/17 11:39
* * @author DELL
* @since JDK 1.8 */
public class ExcelChartUtils { public static void main(String[] args) throws IOException { ExcelChartUtils excelChartUtils=new ExcelChartUtils(); excelChartUtils.exportLineDiagram(); } public ResponseResult exportLineDiagram() throws IOException { XSSFWorkbook wb = new XSSFWorkbook(); String sheetName = "Sheet1"; FileOutputStream fileOut = null; try { XSSFSheet sheet = wb.createSheet(sheetName); // 创建一个画布 XSSFDrawing drawing = sheet.createDrawingPatriarch(); // 前四个默认0,[0,5]:从0列5行开始;[7,26]:到7列26行结束 // 默认宽度(14-8)*12 XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 7, 26); // 创建一个chart对象 XSSFChart chart = drawing.createChart(anchor); // 标题 chart.setTitleText("不良品明细统计"); // 标题覆盖 chart.setTitleOverlay(false); // 图例位置 XDDFChartLegend legend = chart.getOrAddLegend(); legend.setPosition(LegendPosition.TOP); // 分类轴标(X轴),标题位置 XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM); // bottomAxis.setTitle("国家"); // 值(Y轴)轴,标题位置 XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT); // leftAxis.setTitle("面积大小"); // CellRangeAddress(起始行号,终止行号, 起始列号,终止列号) // 分类轴标(X轴)数据,单元格范围位置[0, 0]到[0, 6] //XDDFDataSource countries = XDDFDataSourcesFactory.fromStringCellRange(sheet, new CellRangeAddress(0, 0, 0, 6));//从指定表格中的位置上获取对应数据 XDDFCategoryDataSource countries = XDDFDataSourcesFactory.fromArray(new String[] {"严重性能不良","外观问题","包装问题","瑕疵","人为破坏"}); // 数据1,单元格范围位置[1, 0]到[1, 6] // XDDFNumericalDataSource area = XDDFDataSourcesFactory.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, 6)); XDDFNumericalDataSource<Integer> area = XDDFDataSourcesFactory.fromArray(new Integer[] {17098242,9984670,9826675,9596961,8514877}); // bar:条形图, XDDFBarChartData bar = (XDDFBarChartData) chart.createData(ChartTypes.BAR, bottomAxis, leftAxis); leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN); // 设置为可变颜色 bar.setVaryColors(false);// 如果需要设置成自己想要的颜色,这里可变颜色要设置成false // 条形图方向,纵向/横向:纵向 bar.setBarDirection(BarDirection.COL); // 图表加载数据,条形图1 XDDFBarChartData.Series series1 = (XDDFBarChartData.Series) bar.addSeries(countries, area); // 条形图例标题 series1.setTitle("不良品数量", null); XDDFSolidFillProperties fill = new XDDFSolidFillProperties(XDDFColor.from(PresetColor.BLUE)); // 条形图,填充颜色 series1.setFillProperties(fill); // 绘制 chart.plot(bar); // CTBarSer ser = chart.getCTChart().getPlotArea().getBarChartArray(0).getSerArray(0); // CTLegend legend2 = chart.getCTChartSpace().getChart().getLegend();//更详细的图例设置 // 打印图表的xml System.out.println(chart.getCTChart()); // 将输出写入excel文件 // String filename = "排行榜前七的国家.xlsx"; // out = new FileOutputStream(getAbsoluteFile(filename)); String filename = encodingFilename(); fileOut = new FileOutputStream("E:\\QMSDocument\\exportFile"+File.separator+filename); wb.write(fileOut); } catch (Exception e) { e.printStackTrace(); } finally { wb.close(); if (fileOut != null) { fileOut.close(); } } return ResponseResult.success(); } /** * 编码文件名 */ public String encodingFilename() { String filename = UUID.randomUUID().toString() + ".xlsx"; return filename; } /** * 获取下载路径 * * @param filename 文件名称 */ public String getAbsoluteFile(String filename) { String downloadPath = ConstantKit.baseDir+ File.separator+ConstantKit.exportFile+File.separator+filename; File desc = new File(downloadPath); if (!desc.getParentFile().exists()) { desc.getParentFile().mkdirs(); } return downloadPath; } }

运行效果如下
使用Apache poi 导出excel含有柱状图_第1张图片

你可能感兴趣的:(excel)