POI复制指定行到指定位置

网上找的一部分代码,然后修改了一下

package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.util.CellRangeAddress;
public class ExcelCopy {
	public static void main(String[] args) throws Exception {
		File newFile = new File("F://****.xls");  

		// 新文件写入数据,并下载
		InputStream is = null;  
		HSSFWorkbook workbook = null;  
		HSSFSheet sheet = null;  
		POIFSFileSystem ps = null;
		try {  
			is = new FileInputStream(newFile);// 将excel文件转为输入流  
			ps = new POIFSFileSystem(is);
			workbook = new HSSFWorkbook(ps);// 创建个workbook,  
			// 获取第一个sheet  
			sheet = workbook.getSheetAt(0);  
		} catch (Exception e1) {  
			e1.printStackTrace();  
		}  
		HSSFRow startRow = sheet.getRow(1);
		startRow.getCell(0).getStringCellValue();
		System.out.println("-------------------"+startRow.getCell(0).getStringCellValue());
		HSSFRow endRow = sheet.getRow(41);
		copyRows(sheet, 1, 41, 42);
		 // 写数据  
        FileOutputStream fos = new FileOutputStream(newFile);  
		workbook.write(fos); 
        fos.flush();  
        fos.close();  
        workbook.close();
	}
	/** 
	 * 复制行 
	 *  
	 * @param startColIndex 
	 *            起始列 
	 * @param endColIndex 
	 *            结束列
	 * @param pPosition 
	 *           目标起始行位置 
	 */

	public static HSSFSheet copycols(HSSFSheet currentSheet,int startCol, int endCol, int pPosition) {
		int pStartCol= startCol - 1;
		int pEndCol = endCol - 1;
		int targetColFrom;
		int targetColTo;
		int RowCount;
		CellRangeAddress region = null;
		int i;
		int j;
		if (pStartCol == -1 || pEndCol == -1) {
			return null;
		}

		System.out.println(currentSheet.getNumMergedRegions());
		for (i = 0; i < currentSheet.getNumMergedRegions(); i++) {

			region = currentSheet.getMergedRegion(i);

			if ((region.getFirstColumn() >= pStartCol)  
					&& (region.getLastColumn() <= pEndCol)) {

				targetColFrom = region.getFirstColumn() - pStartCol + pPosition;

				targetColTo = region.getLastColumn() - pStartCol + pPosition;

				CellRangeAddress newRegion = region.copy();

				newRegion.setFirstRow(region.getFirstRow());

				newRegion.setFirstColumn(targetColFrom);

				newRegion.setLastRow(region.getLastRow());

				newRegion.setLastColumn(targetColTo);

				currentSheet.addMergedRegion(newRegion);



			}


		}

		for (i = 0; i <= 50; i++) {

			HSSFRow sourceRow = currentSheet.getRow(i);

			if (sourceRow != null) {

				HSSFRow newRow = currentSheet.getRow(i);
				for (j = 0; j < pEndCol; j++) {

					HSSFCell templateCell = sourceRow.getCell(j);

					if(i == 0){
						currentSheet.setColumnWidth(pPosition+j, currentSheet.getColumnWidth(j));

					}
					if (templateCell != null) {

						HSSFCell newCell = newRow.createCell(pPosition+j);

						copyCell(templateCell, newCell);


					}


				}


			}


		}
		return currentSheet;

	}

	public static void copyRows(HSSFSheet currentSheet,int startRow, int endRow, int pPosition) {
		int pStartRow = startRow - 1;

		int pEndRow = endRow - 1;

		int targetRowFrom;

		int targetRowTo;

		int columnCount;

		CellRangeAddress region = null;

		int i;

		int j;
		
		if (pStartRow == -1 || pEndRow == -1) {

			return;


		}

		for (i = 0; i < currentSheet.getNumMergedRegions(); i++) {

			region = currentSheet.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());

				currentSheet.addMergedRegion(newRegion);


			}


		}

		for (i = pStartRow; i <= pEndRow; i++) {

			HSSFRow sourceRow = currentSheet.getRow(i);

			columnCount = sourceRow.getLastCellNum();

			if (sourceRow != null) {

				HSSFRow newRow = currentSheet.createRow(pPosition - pStartRow  
						+ i);

				newRow.setHeight(sourceRow.getHeight());

				for (j = 0; j < columnCount; j++) {

					HSSFCell templateCell = sourceRow.getCell(j);

					if (templateCell != null) {

						HSSFCell newCell = newRow.createCell(j);

						copyCell(templateCell, newCell);


					}


				}


			}


		}


	}


	private static void copyCell(HSSFCell srcCell, HSSFCell distCell) {

		distCell.setCellStyle(srcCell.getCellStyle());
		
		if (srcCell.getCellComment() != null) {

			distCell.setCellComment(srcCell.getCellComment());


		}

		int srcCellType = srcCell.getCellType();

		distCell.setCellType(srcCellType);
		
		if (srcCellType == HSSFCell.CELL_TYPE_NUMERIC) {
			
			if (HSSFDateUtil.isCellDateFormatted(srcCell)) {

				distCell.setCellValue(srcCell.getDateCellValue());
				

			} else {

				distCell.setCellValue(srcCell.getNumericCellValue());


			}
			System.out.println("srcCell------------"+srcCell.getNumericCellValue());
			System.out.println("distCell------------"+distCell.getNumericCellValue());

		} else if (srcCellType == HSSFCell.CELL_TYPE_STRING) {

			distCell.setCellValue(srcCell.getRichStringCellValue());


		} else if (srcCellType == HSSFCell.CELL_TYPE_BLANK) {

			// nothing21  

		} else if (srcCellType == HSSFCell.CELL_TYPE_BOOLEAN) {

			distCell.setCellValue(srcCell.getBooleanCellValue());


		} else if (srcCellType == HSSFCell.CELL_TYPE_ERROR) {

			distCell.setCellErrorValue(srcCell.getErrorCellValue());


		} else if (srcCellType == HSSFCell.CELL_TYPE_FORMULA) {

			distCell.setCellFormula(srcCell.getCellFormula());


		} else {
			// nothing29  


		}

	}
}

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用 Apache POI 库来操作 Excel 文件。具体来说,以下是实现您所描述的操作的大致步骤: 1. 读取 Excel 文件,并获取要复制的行的样式和格式。 2. 创建一个新的行,将获取的样式和格式应用到该行中。 3. 将要插入的行插入到指定位置。 4. 将新创建的行的数据复制到要插入的行中。 5. 根据需要,合并单元格。 下面是一个简单的示例代码,可以帮助您更好地理解这一过程: ``` // 读取 Excel 文件 FileInputStream inputStream = new FileInputStream(new File("example.xlsx")); Workbook workbook = new XSSFWorkbook(inputStream); Sheet sheet = workbook.getSheetAt(0); // 获取要复制的行的样式和格式 Row sourceRow = sheet.getRow(0); CellStyle sourceStyle = sourceRow.getRowStyle(); short sourceHeight = sourceRow.getHeight(); int sourceNumMergedRegions = sheet.getNumMergedRegions(); // 创建新的行并应用样式和格式 Row newRow = sheet.createRow(2); newRow.setHeight(sourceHeight); newRow.setRowStyle(sourceStyle); // 插入新的行 sheet.shiftRows(2, sheet.getLastRowNum(), 1, true, true); // 复制数据到新的行中 for (int i = 0; i < sourceRow.getLastCellNum(); i++) { Cell oldCell = sourceRow.getCell(i); Cell newCell = newRow.createCell(i); if (oldCell != null) { newCell.setCellValue(oldCell.getStringCellValue()); } } // 根据需要合并单元格 for (int i = 0; i < sourceNumMergedRegions; i++) { CellRangeAddress sourceRange = sheet.getMergedRegion(i); if (sourceRange.getFirstRow() == sourceRow.getRowNum()) { CellRangeAddress newRange = new CellRangeAddress(newRow.getRowNum(), newRow.getRowNum() + sourceRange.getLastRow() - sourceRange.getFirstRow(), sourceRange.getFirstColumn(), sourceRange.getLastColumn()); sheet.addMergedRegion(newRange); } } // 保存 Excel 文件 FileOutputStream outputStream = new FileOutputStream("example.xlsx"); workbook.write(outputStream); workbook.close(); outputStream.close(); ``` 请注意,这只是一个简单的示例,并且可能需要根据您的具体要求进行修改。此外,要使用 Apache POI 库,您需要在项目中添加相应的依赖项。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值