poi循环导出多个jfreechart

1、选择导出位置(JFileChooser)

2、poi循环导出多个图片。(主要是 HSSFPatriarch 的对象,要放在循环外)


public void goExcel() throws Exception {
	    String path;
		//选择文件 导出位置的 一个用法, 提供给 用户 自定义 导出的位置
		JFileChooser fd = new JFileChooser();
		fd.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		int result = fd.showOpenDialog(null);
		// JFileChooser.APPROVE_OPTION是个整型常量,代表0。就是说当返回0的值我们才执行相关操作,否则什么也不做。
		if (result == JFileChooser.APPROVE_OPTION) {
			File file = fd.getSelectedFile();
			// 获得你选择的文件绝对路径。
			path = file.getAbsolutePath();
		} else {
			//没有选择文件路径时 进行的操作。

		}
		// 创建一个webbook,对应一个Excel文件
		HSSFWorkbook wb = new HSSFWorkbook();
		//在webbook中添加一个sheet,对应Excel文件中的sheet
		HSSFSheet sheet = wb.createSheet("统计表");
		// 在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
		HSSFRow row = sheet.createRow((int) 0);
		// 创建单元格,并设置值表头 设置表头居中
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
		HSSFCell cell;
		JFreeChart chart1;
		JFreeChart chart2;
		File outFile;
		File outFile2;
		 //图片单元格
  	   HSSFPatriarch patriarch = sheet.createDrawingPatriarch();  //必须在循环外面
		//统计图
		for(int i=1;i<chartName.length;i++){//2;i++){//
			//画图
			//柱状图
			chart1 = GetChartUtil.getChartZhu();
			//线图
			chart2 = GetChartUtil.getChartXian();
			
			//写图表对象到文件,参照柱状图生成源码
		     FileOutputStream out1 = null;   
		     FileOutputStream out2 = null;   
		     //临时的图片文件
		     outFile = new File(path+"/tempPic1.png");
		     outFile2 = new File(path+"/tempPic2.png");            
		       try {      
		           if (!outFile.getParentFile().exists()) {      
		               outFile.getParentFile().mkdirs();      
		           }   
		           if (!outFile2.getParentFile().exists()) {      
		               outFile2.getParentFile().mkdirs();      
		           }    
		           //写入图片文件
		           out1 = new FileOutputStream(path+"/tempPic1.png");      
		           ChartUtilities.writeChartAsPNG(out1, chart1, 600, 400);  
		           out2 = new FileOutputStream(path+"/tempPic2.png");      
		           ChartUtilities.writeChartAsPNG(out2, chart2, 600, 400);    
		           out1.flush();     
		           out2.flush();       
		       } catch (FileNotFoundException e) {      
		           e.printStackTrace();      
		       } catch (IOException e) {      
		       	e.printStackTrace();      
		       } finally {     
		       	//关闭流
		           if (out1 != null) {      
		               try {      
		                   out1.close();      
		               } catch (IOException e) {  
		               }      
		           }      
		           if (out2 != null) {      
		               try {      
		                   out2.close();      
		               } catch (IOException e) {      
		               }      
		           }            
		       } 
		       //png图片 写入excel 文件 ,读临时文件
		        ByteArrayOutputStream byteArrayOut1 = new ByteArrayOutputStream();
		     	File filepic1 = new File(path+"/tempPic"+1+".png");
		     	BufferedImage bufferImg = ImageIO.read(filepic1);
		     	ImageIO.write(bufferImg, "png", byteArrayOut1);
		     	ByteArrayOutputStream byteArrayOut2 = new ByteArrayOutputStream();
		     	File filepic2 = new File(path+"/tempPic"+2+".png");
		     	BufferedImage bufferImg2 = ImageIO.read(filepic2);
		     	ImageIO.write(bufferImg2, "png", byteArrayOut2);
		     	//写入临时文件   
	     	   HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 100, 50, (short) 0, chartConList.size()+1+20*(i-1)*3, (short) 10, chartConList.size()+1+20*(i-1)*3+20); 
	     	   HSSFClientAnchor anchor2 = new HSSFClientAnchor(0, 0, 100, 50, (short) 0, chartConList.size()+1+20*(i-1)*3+20+1, (short) 10, chartConList.size()+1+20*(i-1)*3+20+1+20);  
	           patriarch.createPicture(anchor1, wb.addPicture(byteArrayOut1.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
	           patriarch.createPicture(anchor2, wb.addPicture(byteArrayOut2.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
		      //删除临时图片文件  
		        outFile.getAbsoluteFile().delete();
		        outFile2.getAbsoluteFile().delete();
		        if(byteArrayOut1!=null){
		        	byteArrayOut1.close();
		        }
		        if(byteArrayOut2!=null){
		        	byteArrayOut2.close();
		        }
		       
		}
		// 将文件存到 上面 用户自定义的位置(path)内
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");//设置日期格式
        String nowtime = df.format(new Date());// new Date()为获取当前系统时间
        //导出文件的命名 :基本事故信息统计表+当前时间精确到秒.xls"
        String newpath = path + "\\基本事故信息统计表"+nowtime+".xls";
		//输出流 写入文件
		FileOutputStream fout = new FileOutputStream(newpath);
		wb.write(fout);
		//关闭流
		if (fout != null) {
			fout.close();
		}
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值