使用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 
  • 6
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 24
    评论
项目想实现excel图表POI支持所以想到了这个方法。放上来抛砖引玉,感兴趣的欢迎大家交流 注:-------------------------------------------------------------------------- 其实现原理和利用JavaScript实现客户端绘完全一样。后台代码将输入 写入特定区域,前端脚本在页面载入时执行脚本绘。大家都是做B/S开发 的,这方面的只是不需要我多说了吧。 如果我们让一个HTML页面在载入时显示一个提示框用后台编程是不现实的 所以我们就动态生成一段JS: window.onload=function(){ ...... } 用Java输出图表的场景和这个一样,POI、JXL对操作Excel图表无能为力 但Excel支持脚本的,也有工作表载入事件,所以也可以用同样的原理实现 只不过这次客户端不再是浏览器而是Excel应用程序。 这样交代的应该比较明白了吧,请不要再留言抱怨没有Java代码了,因为 这根本跟java代码没多大关系,后台的代码所做的工作仅仅是写入必要的 数据,数据的解析和绘操作是在VBA完成的 ---2009-12-18 今天查找资料翻出来了这段代码,发现里边的类确实是空的,我说怎么总挨骂 呢,当时肯定打包的时候晕透了把另一个本该删除的类打包进去了。我无法 替换文件只能把java代码贴上来了,请自己修改模板文件的路径。 package com.test.poi; import java.io.FileInputStream; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class PoiChart { public static void main(String[] args){ try{ POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream("E:/王建波_bak/我的练习/Module.xls")); HSSFWorkbook wb=new HSSFWorkbook(fs); HSSFSheet sheet=wb.getSheetAt(0); HSSFRow row; //写入表头 String[] aTblHeaders=new String[]{ "姓名","语文","数学","英语" }; row=sheet.createRow(0); for(int i=0,len=aTblHeaders.length;i<len;i++){ row.createCell((short)i).setCellValue(aTblHeaders[i]); } //向模板写入数据 String[] aNames=new String[]{ "张飞","刘备","关羽","曹操","孙权","吕布" }; for(int i=0,nRowLen=aNames.length;i<nRowLen;i++){ row=sheet.createRow(i+1); row.createCell((short)0).setCellValue(aNames[i]); for(int j=1,nColLen=aTblHeaders.length;j<nColLen;j++){ row.createCell((short)j).setCellValue(Math.floor(Math.random()*100)); } } //输出文件 FileOutputStream fout=new FileOutputStream("d:/chart.xls"); wb.write(fout); fout.close(); }catch(Exception e){ e.printStackTrace(); } } }
好的,我可以帮您解答这个问题。首先,您需要使用 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、付费专栏及课程。

余额充值