Apache POI复制行和复制单元格

本文深入探讨了如何使用Apache POI库在Java中高效地复制Excel工作表中的行和单元格。通过实例代码,展示了如何实现数据的批量复制,提升Excel处理能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package test;

import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;

/**
 * POI工具类
 * @author suyunlong
 *
 */
public class POIUtil {
	/**
	 * 复制行
	 * @param startRow 开始行
	 * @param endRow 结束行
	 * @param pPosition 目标行
	 * @param sheet 工作表对象
	 */
	public static void copyRows(int startRow,int endRow,int pPosition,XSSFSheet sheet){
		int pStartRow=startRow;
		int pEndRow=endRow;
		int targetRowFrom;
		int targetRowTo;
		int columnCount;
		CellRangeAddress region=null;
		int i;
		int j;
		if(pStartRow == -1 || pEndRow == -1) {
			return;
		}
		// 拷贝合并的单元格
		for(i=0;i<sheet.getNumMergedRegions();i++){
			region=sheet.getMergedRegion(i);
			if((region.getFirstRow() >= pStartRow) && (region.getLastRow() <= pEndRow)) {
				targetRowFrom=region.getFirstRow()-pStartRow+pPosition;
				targetRowTo=region.getLastRow()-pStartRow+pPosition;
				CellRangeAddress newRegion=region.copy();
				newRegion.setFirstRow(targetRowFrom);
				newRegion.setFirstColumn(region.getFirstColumn());
				newRegion.setLastRow(targetRowTo);
				newRegion.setLastColumn(region.getLastColumn());
				sheet.addMergedRegion(newRegion);
			}
		}
		// 设置列宽
		for(i=pStartRow;i<=pEndRow;i++){
			XSSFRow sourceRow=sheet.getRow(i);
			columnCount=sourceRow.getLastCellNum();
			if(sourceRow != null){
				XSSFRow newRow=sheet.createRow(pPosition - pStartRow + i);
				newRow.setHeight(sourceRow.getHeight());
				for(j=0;j<columnCount;j++){
					XSSFCell templateCell=sourceRow.getCell(j);
					if(templateCell != null){
						XSSFCell newCell=newRow.createCell(j);
						copyCell(templateCell,newCell);
					}
				}
			}
		}
	}
	/**
	 * 复制单元格
	 * @param srcCell 原始单元格
	 * @param distCell 目标单元格
	 */
	public static void copyCell(XSSFCell srcCell,XSSFCell distCell){
		distCell.setCellStyle(srcCell.getCellStyle());
		if(srcCell.getCellComment() != null){
			distCell.setCellComment(srcCell.getCellComment());
		}
		int srcCellType=srcCell.getCellType();
		distCell.setCellType(srcCellType);
		if(srcCellType==XSSFCell.CELL_TYPE_NUMERIC){
			if(HSSFDateUtil.isCellDateFormatted(srcCell)){
				distCell.setCellValue(srcCell.getDateCellValue());
			}
			else{
				distCell.setCellValue(srcCell.getNumericCellValue());
			}
		}
		else if(srcCellType==XSSFCell.CELL_TYPE_STRING){
			distCell.setCellValue(srcCell.getRichStringCellValue());
		}
		else if(srcCellType==XSSFCell.CELL_TYPE_BLANK){
			// nothing21
		}
		else if(srcCellType==XSSFCell.CELL_TYPE_BOOLEAN){
			distCell.setCellValue(srcCell.getBooleanCellValue());
		}
		else if(srcCellType==XSSFCell.CELL_TYPE_ERROR){
			distCell.setCellErrorValue(srcCell.getErrorCellValue());
		}
		else if(srcCellType==XSSFCell.CELL_TYPE_FORMULA){
			distCell.setCellFormula(srcCell.getCellFormula());
		}
		else{ // nothing29

		}
	}
	/**
	 * 表格中指定位置插入行
	 * @param sheet 工作表对象
	 * @param rowIndex 指定的行数
	 * @return 当前行对象
	 */
	public static XSSFRow insertRow(XSSFSheet sheet,int rowIndex) {
		XSSFRow row=null;
		if(sheet.getRow(rowIndex) != null) {
			int lastRowNo=sheet.getLastRowNum();
			sheet.shiftRows(rowIndex,lastRowNo,1);
		}
		row=sheet.createRow(rowIndex);
		return row;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值