/** * 根據圖片寬高,合併單元格對象,計算圖片固定寬時,行高多少 * |---若是到最後一行都沒有達到自適應高度,那就取最後一行 * @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元打賞不要更多。^-^圖片