使用POI在Excel中动态生成图表工具类(支持柱状、组合、环状图、折线图、等常用图)

使用POI在Excel中动态生成图表工具类

使用POI在Excel中动态生成图表工具类

由于公司是一个生成报表的机构,之前一直使用pageOffice,但是公司领导就是不买,你说公司那样有钱磨磨唧唧干啥,所以只好换成了开源免费的poi,经过两个星期的研究终于完成了一个工具类。
废话不多说直接上代码:

依赖

	<dependency>
	  <groupId>org.apache.poi</groupId>
	  <artifactId>poi</artifactId>
	  <version>3.17</version>
	</dependency>
	<dependency>
	  <groupId>org.apache.poi</groupId>
	  <artifactId>poi-scratchpad</artifactId>
	  <version>3.17</version>
	</dependency>
	<dependency>
	  <groupId>org.apache.poi</groupId>
	  <artifactId>poi-ooxml</artifactId>
	  <version>3.17</version>
	</dependency>
	<dependency>
	  <groupId>fr.opensagres.xdocreport</groupId>
	  <artifactId>xdocreport</artifactId>
	  <version>1.0.6</version>
	</dependency>
	<dependency>  
        <groupId> fr.opensagres.xdocreport</groupId>  
        <artifactId> org.apache.poi.xwpf.converter.core</artifactId>  
        <version> 1.0.6</version>  
	</dependency>  
	<dependency>
		<groupId>fr.opensagres.xdocreport</groupId>
		<artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
		<version>1.0.6</version>
		</dependency>
	<dependency>
	  <groupId>org.apache.poi</groupId>
	  <artifactId>poi-ooxml-schemas</artifactId>
	  <version>3.17</version>
	</dependency>
	<dependency>
	  <groupId>org.apache.poi</groupId>
	  <artifactId>ooxml-schemas</artifactId>
	  <version>1.3</version>
	</dependency>

代码:
package com.jsfund.crm.common.utils;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Chart;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFDrawing;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAreaChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAreaSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTAxDataSource;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBarChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBarSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTBoolean;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTCatAx;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTDLbls;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLegend;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLineChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTLineSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTMarker;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTMarkerStyle;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumDataSource;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTNumRef;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPie3DChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieChart;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPieSer;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTPlotArea;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTScaling;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTSerTx;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTStrRef;
import org.openxmlformats.schemas.drawingml.x2006.chart.CTValAx;
import org.openxmlformats.schemas.drawingml.x2006.chart.STAxPos;
import org.openxmlformats.schemas.drawingml.x2006.chart.STBarDir;
import org.openxmlformats.schemas.drawingml.x2006.chart.STBarGrouping;
import org.openxmlformats.schemas.drawingml.x2006.chart.STCrossBetween;
import org.openxmlformats.schemas.drawingml.x2006.chart.STCrosses;
import org.openxmlformats.schemas.drawingml.x2006.chart.STLblAlgn;
import org.openxmlformats.schemas.drawingml.x2006.chart.STTickMark;
import org.openxmlformats.schemas.drawingml.x2006.chart.STBarGrouping.Enum;
import org.openxmlformats.schemas.drawingml.x2006.chart.STDispBlanksAs;
import org.openxmlformats.schemas.drawingml.x2006.chart.STGrouping;
import org.openxmlformats.schemas.drawingml.x2006.chart.STLegendPos;
import org.openxmlformats.schemas.drawingml.x2006.chart.STMarkerStyle;
import org.openxmlformats.schemas.drawingml.x2006.chart.STOrientation;
import org.openxmlformats.schemas.drawingml.x2006.chart.STTickLblPos;

import com.jsfund.crm.common.bean.BaseFormMap;

public class ExcelChartUtil {

public static void main(String[] args) throws Exception {
	boolean result=false;
	List<BaseFormMap> dataList=new ArrayList<BaseFormMap>();//数据
	List<String> fldNameArr = new ArrayList<String>();// 字段名
	List<String> titleArr = new ArrayList<String>();// 标题
	BaseFormMap baseFormMap=new BaseFormMap();
	List<String> showtailArr=new ArrayList<String>();
	List<String> ispercentArr=new ArrayList<String>();
	baseFormMap.put("value1", "股票");
	baseFormMap.put("value2", new BigDecimal(new java.util.Random().nextDouble()));
	baseFormMap.put("value3", new BigDecimal(new java.util.Random().nextDouble()));
	baseFormMap.put("value4", new BigDecimal(new java.util.Random().nextDouble()));
	
	BaseFormMap baseFormMap1=new BaseFormMap();
	baseFormMap1.put("value1", "货币型基金");
	baseFormMap1.put("value2", new BigDecimal(new java.util.Random().nextDouble()));
	baseFormMap1.put("value3", new BigDecimal(new java.util.Random().nextDouble()));
	baseFormMap1.put("value4", new BigDecimal(new java.util.Random().nextDouble()));
	
	BaseFormMap baseFormMap2=new BaseFormMap();
	baseFormMap2.put("value1", "可转债");
	baseFormMap2.put("value2", new BigDecimal(new java.util.Random().nextDouble()));
	baseFormMap2.put("value3", new BigDecimal(new java.util.Random().nextDouble()));
	baseFormMap2.put("value4", new BigDecimal(new java.util.Random().nextDouble()));
	
	BaseFormMap baseFormMap3=new BaseFormMap();
	baseFormMap3.put("value1", "买入返售");
	baseFormMap3.put("value2", new BigDecimal(new java.util.Random().nextDouble()));
	baseFormMap3.put("value3", new BigDecimal(new java.util.Random().nextDouble()));
	baseFormMap3.put("value4", new BigDecimal(new java.util.Random().nextDouble()));
	
	BaseFormMap baseFormMap4=new BaseFormMap();
	baseFormMap4.put("value1", "通知存款");
	baseFormMap4.put("value2", new BigDecimal(new java.util.Random().nextDouble()));
	baseFormMap4.put("value3", new BigDecimal(new java.util.Random().nextDouble()));
	baseFormMap4.put("value4", new BigDecimal(new java.util.Random().nextDouble()));
	
	BaseFormMap baseFormMap5=new BaseFormMap();
	baseFormMap5.put("value1", "当月累计");
	baseFormMap5.put("value2", new BigDecimal(new java.util.Random().nextDouble()));
	baseFormMap5.put("value3", new BigDecimal(new java.util.Random().nextDouble()));
	baseFormMap5.put("value4", new BigDecimal(new java.util.Random().nextDouble()));
	
	fldNameArr.add("value1");
	fldNameArr.add("value2");
	fldNameArr.add("value3");
	fldNameArr.add("value4");
	
	titleArr.add("类型");
	titleArr.add("买入");
	titleArr.add("卖出");
	titleArr.add("分红");
	
	showtailArr.add("0");
	showtailArr.add("2");
	showtailArr.add("2");
	showtailArr.add("2");
	
	ispercentArr.add("0");
	ispercentArr.add("1");
	ispercentArr.add("1");
	ispercentArr.add("1");
	
	dataList.add(baseFormMap);
	dataList.add(baseFormMap1);
	dataList.add(baseFormMap2);
	dataList.add(baseFormMap3);
	dataList.add(baseFormMap4);
	dataList.add(baseFormMap5);
	
	
	SXSSFWorkbook wb = new SXSSFWorkbook();
	SXSSFSheet sheet = wb.createSheet("Sheet1");
	
	result=createChart(wb,sheet,10,"bar",STBarGrouping.STACKED,false,false,dataList, fldNameArr, titleArr,showtailArr,ispercentArr);
	
	result=createChart(wb,sheet,10+dataList.size()+12,"bar",STBarGrouping.CLUSTERED,true,true,dataList, fldNameArr, titleArr,showtailArr,ispercentArr);
	
	
	
	
	//System.out.println(ctChart);
    System.out.println(result);

    FileOutputStream 
好的,我可以帮您解答这个问题。首先,您需要使用 Apache POI 库来操作 Excel 文件,确保您已经将它添加到您的项目。 接下来,您可以使用 POI 提供的类来创建一个 Excel 文件,并在其创建一个雷达。以下是一个简单的示例代码: ```java // 创建 Workbook 对象 Workbook workbook = new XSSFWorkbook(); // 创建 Sheet 对象 Sheet sheet = workbook.createSheet("雷达"); // 创建 RadarChart 对象 Drawing drawing = sheet.createDrawingPatriarch(); ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15); Chart chart = drawing.createChart(anchor); ChartLegend legend = chart.getOrCreateLegend(); legend.setPosition(LegendPosition.TOP_RIGHT); RadarChartData data = chart.getChartDataFactory().createRadarChartData(); // 添加数据 ChartData chartData = data.addSeries( chart.getChartDataFactory().createCategorySeries( Arrays.asList("A", "B", "C", "D", "E"), Arrays.asList( chart.getChartDataFactory().createCellReference(sheet.getRow(1).getCell(1)), chart.getChartDataFactory().createCellReference(sheet.getRow(2).getCell(1)), chart.getChartDataFactory().createCellReference(sheet.getRow(3).getCell(1)), chart.getChartDataFactory().createCellReference(sheet.getRow(4).getCell(1)), chart.getChartDataFactory().createCellReference(sheet.getRow(5).getCell(1)) ) ) ); // 添加雷达 chart.plot(data, new ChartAxis[] { chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM), chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT) }); // 将数据写入 Excel 文件 FileOutputStream fileOut = new FileOutputStream("radar_chart.xlsx"); workbook.write(fileOut); fileOut.close(); ``` 上述代码演示了如何在 Excel 文件创建一个雷达,并向其添加数据。请注意,您需要将数据写入 Excel 文件,以便它们可以在图表显示出来。 希望这可以帮助您解决问题。如果您还有其他问题,请随时问我。
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值