POI插入图片自适应宽高,行列计算宽高

/**
	 * 根据图片宽高,合并单元格对象,计算图片固定宽时,行高多少
	 * |---若是到最后一行都没有达到自适应高度,那就取最后一行
	 * @param bufferImg
	 * @param exclEty
	 * @return
	 */
	public static int getEndRowByPictWH(XSSFSheet sheet1,BufferedImage bufferImg,ExcelEntity exclEty){
		
		int pictHeigth = bufferImg.getHeight();
		int pictWidth = bufferImg.getWidth();
		System.out.println("图片宽===>"+pictWidth+"高====>"+pictHeigth);
		
		//算一下开始列 ~~结束列多少像素
		float colsWith = 0;
		for(int i=exclEty.getStartCol();i<=exclEty.getEndCol();i++) {
			System.out.println(colsWith);
			colsWith = colsWith + sheet1.getColumnWidthInPixels(i); // 单位是像素
		}
		System.out.println("colswidth===>"+colsWith);
		//开始行到结束行,多少像素
		float rowsHeigth = 0;
		for(int i=exclEty.getStartRow();i<=exclEty.getEndRow();i++) {
			System.out.println(rowsHeigth);
			XSSFRow mrow = sheet1.getRow(i);
			if(mrow!=null)
				rowsHeigth = rowsHeigth + (mrow.getHeightInPoints()/72)*96; //单位是twip   像素= (Excel的行高度/72)*DPI

			System.out.println("tttt列比行 :比例===>"+colsWith/rowsHeigth); 
			if(colsWith/rowsHeigth<=(float)pictWidth/(float)pictHeigth){
				System.out.println("rowsHeigth===>"+rowsHeigth);
				System.out.println("列比行 :比例===>"+colsWith/rowsHeigth);  //0.91171765    //0.43750003
				System.out.println("OK真实图片宽比高:比例===>"+(float)pictWidth/(float)pictHeigth);
				return i+1; 
			}
		}
		System.out.println("rowsHeigth===>"+rowsHeigth);
		System.out.println("列比行 :比例===>"+colsWith/rowsHeigth);  //0.91171765    //0.43750003
		System.out.println("END真实图片宽比高:比例===>"+(float)pictWidth/(float)pictHeigth);
		return exclEty.getEndRow()+1; //默认所有占满
	}

简单粗暴直接上代码:合并单元格,让宽占满,高度来自适应。java

这里用到了 一个javabean 合并单元格对象:ExcelEntity算法

public class ExcelEntity {
    /**
     * 开始行
     */
    private int startRow;
    /**
     * 结束行
     */
    private int endRow;
    /**
     * 开始列
     */
    private int startCol;
    /**
     * 结束列
     */
    private int endCol;
    /**
     * 单元格值
     */
    private String value;
}

下面是找到全部合并单元格,放入listcode

//先计算合并行列,下面处理合并的跳过
		List<ExcelEntity> list = new ArrayList<ExcelEntity>();
		List<CellRangeAddress> mergedRegions = sheet1.getMergedRegions();
		System.out.println("merged item num>>>:"+mergedRegions.size());
		for (CellRangeAddress cr : mergedRegions) {
			XSSFRow row = sheet1.getRow(cr.getFirstRow());
			Cell cell = row.getCell(cr.getFirstColumn());
			String value = null;
			if(cell.getCellType()==Cell.CELL_TYPE_STRING){
//				if(cell!=null)cell.setCellType(Cell.CELL_TYPE_STRING);
				value = cell.getStringCellValue();
			}
			ExcelEntity tmpExl = new ExcelEntity(cr.getFirstRow(),cr.getLastRow(), cr.getFirstColumn(), cr.getLastColumn(),value);
//			System.out.println(tmpExl.toString());
			list.add(tmpExl);
		}	
		Iterator<Row> rows = sheet1.rowIterator();
		int rowIndex = 0;
		int colIndex = 0;
		while (rows.hasNext()) {
			XSSFRow row = (XSSFRow) rows.next();
			rowIndex = row.getRowNum();
			Iterator<Cell> cells = row.cellIterator();
			while (cells.hasNext()) {
				XSSFCell cell = (XSSFCell) cells.next();
				colIndex = cell.getColumnIndex();
				String value = null;
//				//先判断是否是合并行
				ExcelEntity exclEty = isMergedCell(cell,list);
                if(exclEty!=null&&exclEty.getValue()==null){
//					System.out.println("countiue===>");
					continue;
				}
				// 这里只获取为字符串的单元格判断
				if (cell.getCellType() == Cell.CELL_TYPE_STRING){
					// if (cell!=null) {
					// cell.setCellType(Cell.CELL_TYPE_STRING);
					// }
					value = cell.getStringCellValue();
				}
//				 System.out.println("CellValue==>>>>"+value);
				if (value == null){
					continue;
                }
.....
......
.......

                    XSSFClientAnchor anchor;
					if(exclEty!=null&&exclEty.getValue()!=null){
//						System.out.println(exclEty.toString());
						int endRow = getEndRowByPictWH(sheet1,bufferImg,exclEty);
						
						//图片居中算法,简易用行数算吧: (总行-图占行)/2 = 下移行数  。开始结束都加上这个数量便可 
						int startRowIdx = exclEty.getStartRow();
						int pictRowNum = endRow-startRowIdx;
						int mgRowNum = (exclEty.getEndRow()+1-startRowIdx);
						int addNum = (mgRowNum-pictRowNum)/2;
//						System.out.println("mgRowNum=======================>"+mgRowNum);
//						System.out.println("pictRowNum=======================>"+pictRowNum);
//						System.out.println("addNum=======================>"+addNum);
						
						//合并单元格,取合并开始结束行列索引
						anchor = new XSSFClientAnchor(0,0,0,0,  exclEty.getStartCol(),startRowIdx+addNum   // 不须要居中直接开始就行 exclEty.getStartRow()
								, exclEty.getEndCol()+1, endRow+addNum);   // 若是所有占满,不计算比例 :exclEty.getEndRow()+1						
					}else{//未合并单元格
						anchor = new XSSFClientAnchor(0,0,0,0, colIndex, rowIndex, colIndex + 1,rowIndex + 1);	
					}
...
....
.....

这里又用到了一个对比是否单元格的方法:isMergedCell 对象

/**
	 * 判断是否合并单元格,并返回合并起始结束
	 * @param cell
	 * @param list
	 * @return
	 */
	public static ExcelEntity isMergedCell(Cell cell,List<ExcelEntity> list){
		int cellRow = cell.getRowIndex();
		int cellCol = cell.getColumnIndex();
		for(ExcelEntity exl:list){
//			System.out.println(exl.getValue()+"ALL合并行===>" + cellRow+":"+cellCol);
			//循环遍历,若是是开始,则返回实际对象
			if(cellRow==exl.getStartRow()&&cellCol==exl.getStartCol()){
				if(5<=cellRow&&cellRow<=10&&1<=cellCol&&cellCol<=1){
					System.out.println(exl.getValue()+"图片合并行===>true>>" + cellRow+":"+cellCol);
				}
				return exl;
			}else if(exl.getStartRow()<=cellRow&&cellRow<=exl.getEndRow()
					&&exl.getStartCol()<cellCol&&cellCol<=exl.getEndCol()){
				//同行,但列在合并行内
//				if(5<=cellRow&&cellRow<=10&&1<=cellCol&&cellCol<=1){
//					System.out.println(exl.getValue()+"合并行===>false>>" + cellRow+":"+cellCol);
//				}
				//若是包含在内,则返回Value为空的对象
				return new ExcelEntity();
			}else if(exl.getStartRow()<cellRow&&cellRow<=exl.getEndRow()
					&&exl.getStartCol()<=cellCol&&cellCol<=exl.getEndCol()){
				//同列,但行在合并行内
//				if(5<=cellRow&&cellRow<=10&&1<=cellCol&&cellCol<=1){
//					System.out.println(exl.getValue()+"合并行===>false>>" + cellRow+":"+cellCol);
//				}
				//若是包含在内,则返回Value为空的对象
				return new ExcelEntity();
			}else if(exl.getStartRow()<cellRow&&cellRow<=exl.getEndRow()
					&&exl.getStartCol()<cellCol&&cellCol<=exl.getEndCol()){
				//行列都在合并行内
//				if(5<=cellRow&&cellRow<=10&&1<=cellCol&&cellCol<=1){
//					System.out.println(exl.getValue()+"合并行===>false>>" + cellRow+":"+cellCol);
//				}
				//若是包含在内,则返回Value为空的对象
				return new ExcelEntity();
			}
		}
		return null;
	}

这下完了,所有代码给你了,整吧。!!!索引

若是个人代码有用的话,欢迎打赏。只求0.1元打赏不要更多。^-^图片

相关文章
相关标签/搜索