zk框架获取POI Excel图片 以及 设置图片位置和大小的方法

我们项目使用的是03excel,但是问题是,获取到的workbook可以得到文本信息,图片信息获取不到。经过仔细研究,发现其实图片流是能获取到的,但是保存的时候,不会保存图片信息。所以我们采用的办法是这样的,首先获取的book转成流,然后再将这个流信息,通过POI转换成我们想要的HSSFWorkBook流,再单独将图片添加进去。希望能帮助大家,进行一个分享。

需要说明一下,zk获取到的Book和zk框架封住好的Book底层好像是spreadsheet的SBook,而poi是workbook(两个子类,HSSFWorkBook和XSSFWorkBook)

首先获取到Book,然后转换成流信息保存成模板。

       /**
	 * 保存模板
	 * @param ctx
	 * @throws Exception
	 */
	public void repSave(UserActionContext ctx) throws Exception {
		IReportFacade reportImpl = BeanFactory.getBean("reportImpl",
				IReportFacade.class);
		
		Report report = this.designer.getReport();
		//获取到BOOK
		Book book = this.designer.getBook();
		byte[] data = ctx.getSpreadsheet().getSBook().getSheet(0).getPictures().get(0).getData();
		ViewAnchor anchor = ctx.getSpreadsheet().getSBook().getSheet(0).getPictures().get(0).getAnchor();
		
		try {
			//获取到Book的流信息,但是这里没有图片
			byte[] file = ZkSheetUtil.getWorkBookData(book);
			report.setOwcContent(file);
			
			将上面的模板流转换成新的HSSFWorkbook ,再单独添加图片。
			HSSFWorkbook wb  = (HSSFWorkbook) WorkbookFactory.create(new ByteArrayInputStream(file));
			
			int pictureIdx = wb.addPicture(data, HSSFWorkbook.PICTURE_TYPE_JPEG);
			CreationHelper helper = wb.getCreationHelper();
			int sheetsNum = wb.getNumberOfSheets();
			for (int i = 0; i < sheetsNum; i++) {
				Drawing drawing = wb.getSheetAt(i).createDrawingPatriarch();  
				ClientAnchor anchorw = helper.createClientAnchor();
				//设置图片属性
				getAnchorw(wb.getSheetAt(i),anchor,anchorw);
				//保存图片
				drawing.createPicture(anchorw, pictureIdx);
			}
			//将完成的workbook转成成流信息就行保存
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			wb.write(bos);
			
			report.setOwcContent(bos.toByteArray());
			bos.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		reportImpl.updateReportOWC(this.designer.getReport());
		Clients.evalJavaScript("parent.DesignerCtrl.showMessage('保存成功!');");
	}
这里是将Book保存成流的方法:

 	/** 
	 * @param book
	 * @return
	 * @throws IOException
	 */
	public static byte[] getWorkBookData(Book book) throws IOException {
		String type = "excel";
		switch (book.getType()) {
		case XLS:
			type = "xls";
			break;
		case XLSX:
		default:
			type = "xlsx";
			break;
		}
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		Exporters.getExporter(type).export(book, bos);
		bos.close();
		
		return bos.toByteArray();
	}


这里是设置图片属性的方法。(这里是重点)

刚开始我从网上查了很多资料,知道了设置图片的几个重要属性,就是dx1,dy1,dx2,dy2,col1,row1,col2.row2 这是确定图片位置属性。

图片是网上别人博客偷过来了,不好意思。

其中:

      dx1:the x coordinate within the first cell。

      dy1:the y coordinate within the first cell。

      dx2:the x coordinate within the second cell。

      dy2:the y coordinate within the second cell。

      col1:the column (0 based) of the first cell。

      row1:the row (0 based) of the first cell。

      col2:the column (0 based) of the second cell。

      row2:the row (0 based) of the second cell。

这里dx1、dy1定义了该图片在开始cell的起始位置,dx2、dy2定义了在终cell的结束位置。col1、row1定义了开始cell、col2、row2定义了结束cell。所以只要把这几个属性设置好了,图片就不会变形,也不会乱跑了。

通过自己的计算,粗略的知道了,图片宽度和单元格宽度的比例是32:1,图片高度和单元格高度的比例是15 : 1,还知道,row的默认高度是255,col的默认宽度是2048,然后每个单元格的像素填满时,宽是1024,高是256。所以知道了这些固定值,就能大概计算出图片的位置了。

private void getAnchorw(HSSFSheet sheet, ViewAnchor anchor,ClientAnchor anchorw) {
		
		int col2 = anchor.getColumnIndex();
		int dx2 = 0;
		
		float colwidth  = (float) (32*anchor.getWidth());
		for(int col = anchor.getColumnIndex();;col++){
			int x = (int) (colwidth - sheet.getColumnWidth(col));
			if(colwidth - sheet.getColumnWidth(col) <= 0){
				col2 = col;
				dx2= (int)((colwidth/(float)sheet.getColumnWidth(col))*1024);
				break;
			}else{
				colwidth = colwidth -sheet.getColumnWidth(col);
			}
		}
		
		int row2 = anchor.getRowIndex();
		int dy2 = 0;
		
		float rowHeight  = (float) (15*anchor.getHeight());
		for (int row = anchor.getRowIndex();; row++) {
			if(sheet.getRow(row) == null){
				if(rowHeight - 255 <= 0){
					row2 = row;
					dy2= (int)((rowHeight/255)*256);
					break;
				}else{
					rowHeight = rowHeight - 255;
				}
			}else{
				if(rowHeight - sheet.getRow(row).getHeight() <= 0){
					row2 = row;
					dy2= (int)((rowHeight/(float)sheet.getRow(row).getHeight())*256);
					break;
				}else{
					rowHeight = rowHeight - sheet.getRow(row).getHeight();
				}
			}
		}
		
		
		anchorw.setCol1(anchor.getColumnIndex());
		anchorw.setRow1(anchor.getRowIndex());
		anchorw.setCol2(col2);
		anchorw.setRow2(row2);
		anchorw.setDx2(dx2);
		anchorw.setDy2(dy2);
		anchorw.setDx1(0);
		anchorw.setDy1(0);
	}

效果图:(第一次保存)


第二次保存:


大概就是这样,希望对大家有帮助。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值