亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Excel/xls/xlsx工具類

張軍 2566 0
package zj.excel.util;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;

import zj.arithmetic.excel.ExcelFormulaUtil;
import zj.check.util.CheckUtil;
import zj.date.util.DateUtil;
import zj.excel.bean.DatasKeySheets;
import zj.excel.bean.Excel;
import zj.excel.bean.ExcelI;
import zj.excel.bean.ExcelTemplate;
import zj.excel.bean.FormulaTemplate;
import zj.excel.bean.SheetDatas;
import zj.java.util.JavaUtil;
import zj.type.TypeUtil;

/**
 * 概況 :Excel/xls/xlsx<br>
 * 
 * @version 1.00 (2011.12.02)
 * @author SHNKCS 張軍 {@link  <a target=_blank href="http://m.eyofj.com">張軍個人網站</a>&nbsp;&nbsp;&nbsp;&nbsp;<a target=_blank href="http://user.qzone.qq.com/360901061/">張軍QQ空間</a>} <br>
 */
public final class ExcelUtil implements Serializable {
	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger(ExcelUtil.class.getName());

	private ExcelUtil() {
	}

	/**
	 * Excel寫入
	 * 
	 * @see zj.excel.util.ExcelRWUtil#writeExcel(List, Excel)
	 * @param sheetDatas
	 *            所有行列數據對象
	 * @param excel
	 *            excel對象
	 * @throws Exception
	 */
	@Deprecated
	public static final void writeExcel(final List<SheetDatas> sheetDatas, final Excel excel) throws Exception {
		writeExcel(sheetDatas, excel, null);
	}

	/**
	 * Excel寫入
	 * 
	 * @see zj.excel.util.ExcelRWUtil#writeExcel(List, Excel, ExcelI)
	 * @param sheetDatas
	 *            數據集合
	 * @param excel
	 *            對象
	 * @param excelI
	 *            接口
	 * @throws Exception
	 */
	@Deprecated
	public static final void writeExcel(final List<SheetDatas> sheetDatas, Excel excel, final ExcelI excelI) throws Exception {
		ExcelRWUtil.writeExcel(sheetDatas, excel, excelI);
	}

	// /**
	// * 合并單元格
	// *
	// * @param sheet
	// * @param datasLst
	// */
	// private static void mergedRegion(Sheet sheet, List<List<SheetData>> datasLst) {
	// // 合并單元格
	// // org.apache.poi.ss.util.CellRangeAddress
	// // // 合并第一行到第二行的第一列到第二列
	// // sheet.addMergedRegion(new CellRangeAddress(0, (short) 1, 0, (short) 1));
	// // // 合并第三行到第五行的第一列到第二列
	// // sheet.addMergedRegion(new CellRangeAddress(2, (short) 4, 0, (short) 1));
	// // HSSFRow row4 = sheet.createRow(4);
	// //
	// // // 合并第三行到第五行的第一列
	// // sheet.addMergedRegion(new Region(2, (short) 0, 4, (short) 0));
	// //
	// // // 合并第三行到第五行的第二列
	// // sheet.addMergedRegion(new Region(2, (short) 1, 4, (short) 1));
	// //
	// // // 合并第三行的第三列到第AA指定的列
	// // int aa = 2 * fialList.size() + 1;
	// // sheet.addMergedRegion(new Region(2, (short) 2, 2, (short) aa));
	// //
	// // int start = aa + 1;
	// //
	// // sheet.addMergedRegion(new Region(2, (short) start, 2,
	// // (short) (number - 1)));
	// //
	// // // 循環合并第四行的行,并且是每2列合并成一列
	// // for (int i = 2; i < number; i = i + 2) {
	// // sheet.addMergedRegion(new Region(3, (short) i, 3, (short) (i + 1)));
	// //
	// // }
	// }

	/**
	 * 默認行樣式
	 * 
	 * @param cellStyle
	 */
	public static final void rowExcel(final Row row, final int rowIndex) {
		// row.setHeight((short) 1000);
	}

	/**
	 * 默認單元格樣式
	 * 
	 * @param cellStyle
	 */
	public static final void cellStyle(final CellStyle cellStyle) {
		// 設置邊框樣式
		cellStyle.setBorderTop(CellStyle.BORDER_THIN);
		cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
		cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
		cellStyle.setBorderRight(CellStyle.BORDER_THIN);
		// 設置邊框顏色
		cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
		cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
		cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
		cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
		// // 設置背景色
		// cellStyle.setFillForegroundColor(HSSFColor.BLUE.index);
		// cellStyle.setFillBackgroundColor(HSSFColor.RED.index);
		// // 上面程序只指定了「ForegroundColor」,填充模式是「SOLID_FOREGROUND」,因此顔色應該是全部充滿整個單元格的
		// cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		// // 單元格內容
		// cellStyle.setAlignment(CellStyle.ALIGN_CENTER); // 指定單元格居中對齊
		cellStyle.setAlignment(CellStyle.ALIGN_LEFT); // 指定單元格左對齊
		cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 指定單元格垂直居中對齊
		// cellStyle.setWrapText(true);// 指定單元格自動換行
	}

	/**
	 * 默認字體樣式
	 * 
	 * @param cellStyle
	 */
	public static final void font(final Font font) {
		// 單元格字體
		// font.setBoldweight(Font.BOLDWEIGHT_BOLD);
		font.setFontName("宋體");
		// 設置字體高度
		// font.setFontHeight((short) 550);
	}

	// #####################################################################
	/**
	 * 讀取excel
	 * 
	 * @param excel
	 *            excel對象
	 * @return 所有行列數據對象
	 * @throws Exception
	 */
	@Deprecated
	public static final DatasKeySheets readExcel(final Excel excel) throws Exception {
		return readExcel(excel, null);
	}

	/**
	 * 讀取excel
	 * 
	 * @param excel
	 *            excel對象
	 * @param excelI
	 *            excelI接口
	 * @return 所有行列數據對象
	 * @throws Exception
	 */
	@Deprecated
	public static final DatasKeySheets readExcel(final Excel excel, final ExcelI excelI) throws Exception {
		return ExcelRWUtil.readExcel(excel, excelI);
	}

	/**
	 * 合并單元格
	 * 
	 * @param sheet
	 * @param firstR
	 *            開始行
	 * @param lastR
	 *            結束行
	 * @param firstC
	 *            開始列
	 * @param lastC
	 *            結束列
	 */
	public static final void mergerRegion(final Sheet sheet, final int firstR, final int lastR, final int firstC, final int lastC) {
		sheet.addMergedRegion(new CellRangeAddress(firstR, lastR, firstC, lastC));
	}

	/**
	 * 復制原有sheet的合并單元格到新創建的sheet
	 * 
	 * @param sheetCreat
	 *            新創建sheet
	 * @param sheet
	 *            原有的sheet
	 */
	public static final void mergerRegion(final Sheet fromSheet, final Sheet toSheet) {
		int sheetMergerCount = fromSheet.getNumMergedRegions();
		for (int i = 0; i < sheetMergerCount; i++) {
			CellRangeAddress mergedRegionAt = fromSheet.getMergedRegion(i);
			toSheet.addMergedRegion(mergedRegionAt);
		}
	}

	/**
	 * 復制一個單元格樣式到目的單元格樣式
	 * 
	 * @param srcStyle
	 *            源樣式
	 * @param destStyle
	 *            目標樣式
	 */
	public static final void copyCellStyle(final CellStyle srcStyle, final CellStyle destStyle) {
		destStyle.setAlignment(srcStyle.getAlignment());
		// 邊框和邊框顏色
		destStyle.setBorderBottom(srcStyle.getBorderBottom());
		destStyle.setBorderLeft(srcStyle.getBorderLeft());
		destStyle.setBorderRight(srcStyle.getBorderRight());
		destStyle.setBorderTop(srcStyle.getBorderTop());
		destStyle.setTopBorderColor(srcStyle.getTopBorderColor());
		destStyle.setBottomBorderColor(srcStyle.getBottomBorderColor());
		destStyle.setRightBorderColor(srcStyle.getRightBorderColor());
		destStyle.setLeftBorderColor(srcStyle.getLeftBorderColor());

		// 背景和前景
		destStyle.setFillBackgroundColor(srcStyle.getFillBackgroundColor());
		destStyle.setFillForegroundColor(srcStyle.getFillForegroundColor());

		destStyle.setDataFormat(srcStyle.getDataFormat());
		destStyle.setFillPattern(srcStyle.getFillPattern());
		// destStyle.setFont(srcStyle.getFont(null));
		destStyle.setHidden(srcStyle.getHidden());
		destStyle.setIndention(srcStyle.getIndention());// 首行縮進
		destStyle.setLocked(srcStyle.getLocked());
		destStyle.setRotation(srcStyle.getRotation());// 旋轉
		destStyle.setVerticalAlignment(srcStyle.getVerticalAlignment());
		destStyle.setWrapText(srcStyle.getWrapText());
	}

	/**
	 * 拷貝單元格
	 * 
	 * @param srcCell
	 *            源單元格
	 * @param destCell
	 *            目標單元格
	 */
	public static final void copyCell(final ExcelTemplate excel, final Cell srcCell, final Cell destCell) {
		copyCell(excel, srcCell, destCell, true, true, true);
	}

	/**
	 * 根據目標單元格類型轉換值
	 * 
	 * @param destCell
	 *            目標單元格
	 * @param value
	 *            待轉換的值
	 */
	public static final Object convertValueByDestCell(final Cell destCell, Object value) {
		try {
			if (CheckUtil.isNull(value)) {
				return null;
			}
			Object newValue = value;
			switch (destCell.getCellType()) {
			case Cell.CELL_TYPE_STRING:
				if (value instanceof Date) {
					newValue = DateUtil.dateParse(value, "yyyy-MM-dd HH:mm:ss");
				}
				break;
			case Cell.CELL_TYPE_NUMERIC:
				if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(destCell)) {
					if (value instanceof Date) {
						newValue = DateUtil.dateParse(value, "yyyy-MM-dd HH:mm:ss");
					}
				} else {
					if (value instanceof String) {
						newValue = new BigDecimal(JavaUtil.objToStr(value));
					}
				}
				break;
			case Cell.CELL_TYPE_BLANK:
				break;
			case Cell.CELL_TYPE_BOOLEAN:
				// 一般用不到轉換
				if (value instanceof String) {
					newValue = TypeUtil.Primitive.booleanValueNoCatchError(value);
				}
				break;
			case Cell.CELL_TYPE_ERROR:
				break;
			case Cell.CELL_TYPE_FORMULA:
				break;
			default:
				break;
			}
			return newValue;
		} catch (Exception e) {
			logger.error("值轉換出錯,取默認值:" + value, e);
			return value;
		}

	}

	/**
	 * 拷貝單元格
	 * 
	 * @param srcCell
	 *            源單元格
	 * @param destCell
	 *            目標單元格
	 * @param copyStyle
	 *            是否拷貝樣式
	 * @param copyValue
	 *            是否拷貝內容
	 * @param copyCellComment
	 *            是否拷貝評論
	 */
	public static final void copyCell(final ExcelTemplate excel, final Cell srcCell, final Cell destCell, final boolean copyValue, final boolean copyStyle, final boolean copyCellComment) {
		// 樣式
		if (copyStyle) {
			// 引用隔開,clone一個cellStyle對象
			try {
				CellStyle newStyle = destCell.getSheet().getWorkbook().createCellStyle();
				newStyle.cloneStyleFrom(srcCell.getCellStyle());
				destCell.setCellStyle(newStyle);
			} catch (Exception e) {
				// TODO 目前不做處理
			}
			// //全局引用相同
			// CellStyle newStyle = destCell.getCellStyle();
			// newStyle.cloneStyleFrom(srcCell.getCellStyle());
			// destCell.setCellStyle(newStyle);
			// //部分引用相同
			// // destCell.setCellStyle(srcCell.getCellStyle());
		}
		// 評論
		if (copyCellComment) {
			if (srcCell.getCellComment() != null) {
				destCell.setCellComment(srcCell.getCellComment());
			}
		}
		// 不同數據類型處理
		if (copyValue) {
			switch (srcCell.getCellType()) {
			case Cell.CELL_TYPE_STRING:
				destCell.setCellValue(srcCell.getRichStringCellValue());
				break;
			case Cell.CELL_TYPE_NUMERIC:
				destCell.setCellValue(srcCell.getNumericCellValue());
				if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(srcCell)) {
					destCell.setCellValue(srcCell.getDateCellValue());
				} else {
					destCell.setCellValue(srcCell.getNumericCellValue());
				}
				break;
			case Cell.CELL_TYPE_BLANK:
				destCell.setCellType(Cell.CELL_TYPE_BLANK);
				break;
			case Cell.CELL_TYPE_BOOLEAN:
				destCell.setCellValue(srcCell.getBooleanCellValue());
				break;
			case Cell.CELL_TYPE_ERROR:
				destCell.setCellErrorValue(srcCell.getErrorCellValue());
				break;
			case Cell.CELL_TYPE_FORMULA:
				if (excel.isAutoFormulaIndex()) {
					int cKey = srcCell.getColumnIndex();
					String tempFormulaIndex = excel.tempRowFormulaIndex.get(cKey);
					if (CheckUtil.isNull(tempFormulaIndex)) {
						tempFormulaIndex = srcCell.getCellFormula();
					}
					String nextFormualIndex = ExcelFormulaUtil.autoFormualIndexToString(tempFormulaIndex);
					destCell.setCellFormula(nextFormualIndex);
					// 設置新公式
					excel.tempRowFormulaIndex.put(cKey, nextFormualIndex);
				} else {
					destCell.setCellFormula(srcCell.getCellFormula());
				}
				break;
			default:
				break;
			}
		}
		try {
			destCell.setCellType(srcCell.getCellType());
		} catch (Exception e) {
			// 復制格式
			logger.error("設置excel類型出錯,不用理會:行[" + srcCell.getRowIndex() + "]列[" + srcCell.getColumnIndex() + "]", e);
		}
	}

	/**
	 * 格式化值
	 * 
	 * @param sheet
	 *            對象
	 * @param row
	 *            行對象
	 * @param cell
	 *            列對象
	 * @param rowIndex
	 *            行索引
	 * @param colIndex
	 *            列索引
	 * @param value
	 *            值
	 */
	public static final void setCellValue(final Sheet sheet, final Row row, final Cell cell, final int rowIndex, final int colIndex, final Object value) {
		setCellValue(sheet, row, cell, rowIndex, colIndex, value, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 格式化值
	 * 
	 * @param sheet
	 *            對象
	 * @param row
	 *            行對象
	 * @param cell
	 *            列對象
	 * @param rowIndex
	 *            行索引
	 * @param colIndex
	 *            列索引
	 * @param value
	 *            值
	 * @param partten
	 *            格式化日期:yyyy-MM-dd HH:mm:ss
	 */
	public static final void setCellValue(Sheet sheet, Row row, Cell cell, int rowIndex, int colIndex, Object value, String partten) {
		// String strValue = "";
		// if (value instanceof String) {
		// strValue = value == null ? "" : "" + value;
		// } else if (value instanceof Date) {
		// strValue = sdf.format((Date) value);
		// } else if (value instanceof Boolean) {
		// strValue = value == null ? "" : "" + value;
		// } else if (value instanceof Double) {
		// strValue = value == null ? "" : "" + value;
		// } else if (value instanceof Calendar) {
		// strValue = sdf.format(((Calendar) value).getTime());
		// } else if (value instanceof Float) {
		// strValue = value == null ? "" : "" + value;
		// } else {
		// strValue = value == null ? "" : "" + value;
		// }
		try {
			if (value == null || "".equals(value)) {
				// 設置空白字符
				cell.setCellType(Cell.CELL_TYPE_BLANK);
				return;
			}
			if (value instanceof String) {
				cell.setCellValue(JavaUtil.objToStr(value));
			} else if (value instanceof Calendar) {
				if (CheckUtil.isNull(partten)) {
					cell.setCellValue((Calendar) value);
				} else {
					// 日期格式化成字符串
					cell.setCellValue(DateUtil.dateParse(value, partten));
				}
			} else if (value instanceof Date || value instanceof Timestamp) {
				if (CheckUtil.isNull(partten)) {
					cell.setCellValue((Date) value);
				} else {
					// 日期格式化成字符串
					cell.setCellValue(DateUtil.dateParse(value, partten));
				}
			} else if (value instanceof Boolean) {
				cell.setCellValue((Boolean) value);
			} else if (value instanceof Number) {
				// 數字
				cell.setCellValue(Double.parseDouble(String.valueOf(value)));
			} else if (value instanceof FormulaTemplate) {
				FormulaTemplate ft = (FormulaTemplate) value;
				// 數字
				cell.setCellFormula(ft.value);
			} else {
				cell.setCellValue(JavaUtil.objToStr(value));
			}
		} catch (Exception e) {
			cell.setCellValue(JavaUtil.objToStr(value));
			e.printStackTrace();
		}
	}

	/**
	 * 插入單元格
	 * 
	 * @param sheet
	 *            sheet對象
	 * @param startRow
	 *            開始行號,從0開始
	 * @param rows
	 *            插入行數
	 */
	public static final void insertRows(final ExcelTemplate excel, final Sheet sheet, final int startRow, final int rows) {
		insertRows(excel, sheet, startRow, rows, false);
	}

	/**
	 * 插入單元格
	 * 
	 * @param sheet
	 *            sheet對象
	 * @param startRow
	 *            開始行號,從0開始
	 * @param rows
	 *            插入列數
	 */
	public static final void insertColumns(final ExcelTemplate excel, final Sheet sheet, final int startRow, final int columns, final boolean copyValue) {
		// 取得第當前行
		Row srcRow = sheet.getRow(startRow);
		for (int i = 0; i < columns; i++) {
			// 獲取源行的單元格
			Cell srcCell = srcRow.getCell(i);
			if (srcCell == null) {
				srcCell = srcRow.createCell(i);
			}
			Cell newCell = srcRow.createCell(i);
			// 如果有往下或往上合并的數據,則不賦值
			if (copyValue) {
				// 如果有往下或往上合并的數據,則不賦值
				CellRangeAddress ca = getMergedRegion(sheet, srcRow.getRowNum(), srcCell.getColumnIndex(), ConstantForEnum.MergedRegionStatus.CONSTANTS_ROW_COL);
				if (ca == null) {
					// 非合并單元格,賦值
					ExcelUtil.copyCell(excel, srcCell, newCell, true, true, true);
				} else {
					ca = getMergedRegion(sheet, srcRow.getRowNum(), srcCell.getColumnIndex(), ConstantForEnum.MergedRegionStatus.EQUALS_ROW);
					if (ca == null) {
						// 多行合并單元格,不用賦值
						ExcelUtil.copyCell(excel, srcCell, newCell, false, true, true);
					} else {
						// 一行多列合并單元格,賦值
						ExcelUtil.copyCell(excel, srcCell, newCell, true, true, true);
					}
				}
			} else {
				ExcelUtil.copyCell(excel, srcCell, newCell, false, true, true);
			}
		}
	}

	/**
	 * 插入單元格
	 * 
	 * @param sheet
	 *            sheet對象
	 * @param startRow
	 *            開始行號,從0開始
	 * @param rows
	 *            插入行數
	 */
	public static final void insertRows(final ExcelTemplate excel, final Sheet sheet, final int startRow, final int rows, final boolean copyValue) {
		try {
			if (sheet != null) {
				int lastRowNum = sheet.getLastRowNum();
				if (startRow == lastRowNum) {
					lastRowNum++;
				}
				if (startRow >= lastRowNum) {
					logger.warn("開始行[" + startRow + "]已超出excel最大行[" + lastRowNum + "]");
					return;
				}
				int tempStartRow = startRow;
				// startRow the row to start shifting
				// endRow the row to end shifting
				// n the number of rows to shift
				// copyRowHeight whether to copy the row height during the shift
				// resetOriginalRowHeight whether to set the original row's height to the default
				// 從開始行下一行開始操作
				// Set<CellRangeAddress> srcCas = new HashSet<CellRangeAddress>();
				// for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
				// CellRangeAddress ca = sheet.getMergedRegion(i);
				// logger.debug(ca + "[" + ca.getFirstRow() + "-" + ca.getLastRow() + "," + ca.getFirstColumn() + "-" + ca.getLastColumn() + "]");
				// }
				// logger.debug("==========================");
				if (rows > 0) {
					sheet.shiftRows(tempStartRow + 1, lastRowNum, rows, true, false);
				}
				// 取得第一行
				Row srcRow = sheet.getRow(tempStartRow);
				if (srcRow == null) {
					return;
				}
				for (int i = 0; i < rows; i++) {
					// 循環添加合并單元格
					// 創建新行
					Row newRow = sheet.createRow(++tempStartRow);
					// 創建單元格
					// 合并的單元格式集合
					if (srcRow.getFirstCellNum() >= 0 && srcRow.getLastCellNum() >= 0) {
						// 如果源行每一列和最后一個>=0
						for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) {
							// 循環添加列
							// 獲取源行的單元格
							Cell srcCell = srcRow.getCell(j);
							if (srcCell == null) {
								continue;
							}
							// 獲取源行的單元格
							Cell newCell = newRow.getCell(j);
							if (newCell == null) {
								newCell = newRow.createCell(j);
							}
							// 如果有往下或往上合并的數據,則不賦值
							if (copyValue) {
								// 如果有往下或往上合并的數據,則不賦值
								CellRangeAddress ca = getMergedRegion(sheet, srcRow.getRowNum(), srcCell.getColumnIndex(), ConstantForEnum.MergedRegionStatus.CONSTANTS_ROW_COL);
								if (ca == null) {
									// 非合并單元格,賦值
									ExcelUtil.copyCell(excel, srcCell, newCell, true, true, true);
								} else {
									ca = getMergedRegion(sheet, srcRow.getRowNum(), srcCell.getColumnIndex(), ConstantForEnum.MergedRegionStatus.EQUALS_ROW);
									if (ca == null) {
										// 多行合并單元格,不用賦值
										ExcelUtil.copyCell(excel, srcCell, newCell, false, true, true);
									} else {
										// 一行多列合并單元格,賦值
										ExcelUtil.copyCell(excel, srcCell, newCell, true, true, true);
									}
								}
							} else {
								ExcelUtil.copyCell(excel, srcCell, newCell, false, true, true);
							}
						}
					}
				}
				Set<CellRangeAddress> srcCas = new HashSet<CellRangeAddress>();
				for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
					CellRangeAddress ca = sheet.getMergedRegion(i);
					// logger.debug(ca + "[" + ca.getFirstRow() + "-" + ca.getLastRow() + "," + ca.getFirstColumn() + "-" + ca.getLastColumn() + "]");
					srcCas.add(ca);
				}
				// 合并單元格
				Set<CellRangeAddress> rowsCas = new HashSet<CellRangeAddress>();
				// 如果單元格在合并單元格范圍內
				for (CellRangeAddress ca : srcCas) {
					// logger.debug(ca + "[" + ca.getFirstRow() + "-" + ca.getLastRow() + "," + ca.getFirstColumn() + "-" + ca.getLastColumn() + "]");
					if (ca.getFirstRow() == ca.getLastRow() && ca.getFirstRow() <= startRow && ca.getLastRow() > startRow) {
						// 循環列合并
						rowsCas.add(ca);
					}
					if (ca.getFirstRow() != ca.getLastRow() && ca.getFirstRow() <= startRow + rows && ca.getLastRow() > startRow + rows) {
						// 由于插入行,則合并列改變為插入后的行列單元格了,因此加rows
						// 如果起始行, 結束行, 起始列, 結束列在單元格行列中
						int caStartRow = ca.getFirstRow() - rows;
						if (caStartRow < 0) {
							continue;
						}
						mergerRegion(sheet, caStartRow, ca.getLastRow(), ca.getFirstColumn(), ca.getLastColumn());
					}
				}
				// for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
				// CellRangeAddress ca = sheet.getMergedRegion(i);
				// logger.debug(ca + "[" + ca.getFirstRow() + "-" + ca.getLastRow() + "," + ca.getFirstColumn() + "-" + ca.getLastColumn() + "]");
				// if (ca.getFirstRow() <= startRow && ca.getLastRow() >= startRow) {
				// // 循環列合并
				// rowsCas.add(ca);
				// }
				// if (ca.getFirstRow() != ca.getLastRow() && ca.getFirstRow() <= startRow + rows && ca.getLastRow() >= startRow + rows) {
				// // 由于插入行,則合并列改變為插入后的行列單元格了,因此加rows
				// // 如果起始行, 結束行, 起始列, 結束列在單元格行列中
				// mergerRegion(sheet, ca.getFirstRow() - rows, ca.getLastRow(), ca.getFirstColumn(), ca.getLastColumn());
				// }
				// }
				for (CellRangeAddress ca : rowsCas) {
					for (int i = startRow; i < rows + startRow; i++) {
						// 如果起始行, 結束行, 起始列, 結束列在單元格行列中
						mergerRegion(sheet, ca.getFirstRow() + i, ca.getLastRow() + i, ca.getFirstColumn(), ca.getLastColumn());
					}
				}
			}
		} catch (Exception e) {
			logger.error("插入行錯誤", e);
		}
	}

	/**
	 * 獲取合并單元格對象
	 * 
	 * @param sheet
	 *            當前sheet對象
	 * @param row
	 *            行號
	 * @param col
	 *            列號
	 * @param mergedRegionStatus
	 *            參考{@link zj.excel.util.ConstantForEnum.MergedRegionStatus}
	 * @return 合并單元格對象
	 */
	public static final CellRangeAddress getMergedRegion(final Sheet sheet, final int row, final int col, final ConstantForEnum.MergedRegionStatus mergedRegionStatus) {
		for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
			CellRangeAddress ca = sheet.getMergedRegion(i);
			switch (mergedRegionStatus) {
			case CONSTANTS_ROW_COL:
				// 獲得合并單元格的起始行, 結束行, 起始列, 結束列, 行數
				if (ca.getFirstRow() <= row && ca.getLastRow() >= row && ca.getFirstColumn() <= col && ca.getLastColumn() >= col) {
					// 如果起始行, 結束行, 起始列, 結束列在單元格行列中
					return ca;
				}
				break;
			case EQUALS_ROW:
				// 獲得合并單元格的起始行, 結束行, 起始列, 結束列, 行數
				if (ca.getFirstRow() == row && ca.getLastRow() == row && ca.getFirstColumn() <= col && ca.getLastColumn() >= col) {
					// 如果起始行, 結束行, 起始列, 結束列在單元格行列中
					return ca;
				}
				break;
			case EQUALS_COL:
				// 獲得合并單元格的起始行, 結束行, 起始列, 結束列, 行數
				if (ca.getFirstRow() <= row && ca.getLastRow() >= row && ca.getFirstColumn() == col && ca.getLastColumn() == col) {
					// 如果起始行, 結束行, 起始列, 結束列在單元格行列中
					return ca;
				}
				break;
			}
		}
		return null;
	}

	/**
	 * 獲取合并單元格對象索引
	 * 
	 * @param sheet
	 *            當前sheet對象
	 * @param row
	 *            行號
	 * @param col
	 *            列號
	 * @return 合并單元格對象索引
	 */
	public static final int getMergedRegionIndex(final Sheet sheet, final int row, final int col) {
		for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
			CellRangeAddress ca = sheet.getMergedRegion(i);
			// 獲得合并單元格的起始行, 結束行, 起始列, 結束列, 行數
			if (ca.getFirstRow() <= row && ca.getLastRow() >= row && ca.getFirstColumn() <= col && ca.getLastColumn() >= col) {
				// 如果起始行, 結束行, 起始列, 結束列在單元格行列中
				return i;
			}
		}
		return -1;
	}
}



更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 欧美一级看片a免费视频 | 成人久久久观看免费毛片 | 99免费在线 | 韩国网站爱久久 | 色五婷婷 | 91精品一区二区三区久久久久 | 久热精品免费 | 韩国日本美国免费毛片 | a级毛片毛片免费观看久潮 a级毛片免费 | 一道本免费视频 | 97热在线| 久久草在线播放 | 伊人久久久综在合线久久在播 | 欧美一级看片a免费观看 | 欧美3区| 亚洲精品色婷婷在线影院麻豆 | 狠狠色噜噜狠狠狠狠色综合网 | 久久最近最新中文字幕大全 | 欧美激情一区二区三区视频 | 免费观看四虎精品成人 | 亚洲九九爱 | 欧美肥婆videos另类 | 毛片免费全部播放一级 | 日韩国产欧美在线观看 | 欧美美女被爆操 | 不卡午夜视频 | 日韩高清一区二区 | 精品视频在线观看一区二区三区 | 国产精品香蕉在线观看不卡 | 99久久影院| 久久精品国产亚洲精品 | 日韩欧美国产高清在线观看 | 成人在色线视频在线观看免费大全 | 日韩欧美黄色片 | 国产精品亚洲第一区广西莫菁 | 日本高清视频一区二区三区 | 亚洲精品久久久久久久久久久网站 | 久久美女免费视频 | 国内精品视频免费观看 | 99热国产这里只有精品99 | 天天操伊人 |