Apache POI Copy Cell

使用Java操作Excel表格,复制表格的功能。

import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Workbook;

public class ExcelTool {

	private CreationHelper helper = null;
	private Workbook wb = null;

	public ExcelTool(Workbook workbook) {
		this.wb = workbook;
		this.helper = workbook.getCreationHelper();
	}

	// 单元格复制函数
	public void copyCell(Cell oldCell, Cell newCell) {
		CellStyle newStyle = this.wb.createCellStyle();
		copyStyle(oldCell.getCellStyle(), newStyle);
		newCell.setCellStyle(newStyle);
		// 评论
		if (oldCell.getCellComment() != null) {
			newCell.setCellComment(oldCell.getCellComment());
		}
		// 对日期和超链接进行了处理
		switch (oldCell.getCellType()) {
		case Cell.CELL_TYPE_BLANK:
			newCell.setCellType(Cell.CELL_TYPE_BLANK);
			break;
		case Cell.CELL_TYPE_BOOLEAN:
			newCell.setCellValue(oldCell.getBooleanCellValue());
			break;
		case Cell.CELL_TYPE_ERROR:
			newCell.setCellErrorValue(oldCell.getErrorCellValue());
			break;
		case Cell.CELL_TYPE_FORMULA:
			newCell.setCellFormula(oldCell.getCellFormula());
			break;
		case Cell.CELL_TYPE_NUMERIC:
			if (DateUtil.isCellDateFormatted(oldCell)) {
				newCell.setCellValue(oldCell.getDateCellValue());
			} else {
				newCell.setCellValue(oldCell.getNumericCellValue());
			}
			break;
		case Cell.CELL_TYPE_STRING:
			newCell.setCellValue(oldCell.getRichStringCellValue());
			if (oldCell.getHyperlink() != null) {
				Font hlink_font = this.wb.createFont();
				hlink_font.setUnderline(Font.U_SINGLE);
				hlink_font.setColor(IndexedColors.BLUE.getIndex());

				Hyperlink link = helper.createHyperlink(Hyperlink.LINK_URL);
				link.setAddress(oldCell.getHyperlink().getAddress());
				newCell.setHyperlink(link);
				newCell.getCellStyle().setFont(hlink_font);
			}
			break;
		}
	}

	// 单元格样式复制函数
	private void copyStyle(CellStyle oldStyle, CellStyle newStyle) {
		newStyle.setAlignment(oldStyle.getAlignment());
		newStyle.setDataFormat(oldStyle.getDataFormat());
		newStyle.setFillPattern(oldStyle.getFillPattern());
		newStyle.setHidden(oldStyle.getHidden());
		newStyle.setLocked(oldStyle.getLocked());
		newStyle.setIndention(oldStyle.getIndention());
		newStyle.setRotation(oldStyle.getRotation());
		newStyle.setVerticalAlignment(oldStyle.getVerticalAlignment());
		newStyle.setWrapText(oldStyle.getWrapText());
	}

	public void close() {
		try {
			this.wb.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 使用Apache POI可以通过以下方式复制工作表页到目标Excel文件: 1. 首先,打开要复制的工作簿。 2. 然后,使用workbook.cloneSheet(int index)方法复制工作表。该方法接受一个整数参数,表示要复制的工作表在工作簿中的索引。 3. 在复制工作表后,使用workbook.setSheetName(int index, String sheetname)方法给新工作表重命名。 4. 最后,使用workbook.write(OutputStream outputStream) 把这个工作簿写到目标文件里面。 代码如下: ``` import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.FileOutputStream; //... FileInputStream inputStream = new FileInputStream("source.xlsx"); Workbook workbook = new XSSFWorkbook(inputStream); Sheet sheet = workbook.getSheetAt(0); Workbook newWorkbook = new XSSFWorkbook(); Sheet newSheet = newWorkbook.cloneSheet(0); newWorkbook.setSheetName(0, "new_sheet_name"); FileOutputStream outputStream = new FileOutputStream("destination.xlsx"); newWorkbook.write(outputStream); inputStream.close(); outputStream.close(); ``` 输入流会读取源excel,复制后的sheet再写入到新的excel里面,并且可以重新设置sheet名字。 ### 回答2: 复制sheet页到目标Excel可以通过Apache POI的HSSFWorkbook和XSSFWorkbook类中的cloneSheet方法来实现。 首先,我们需要在源Excel中获取到要复制的sheet页。可以通过调用Workbook对象的getSheet方法,并传入要复制的sheet的名称或索引来获取到Sheet对象。 然后,我们需要在目标Excel中创建一个新的sheet页,可以通过调用Workbook对象的createSheet方法来创建一个新的Sheet对象。 接下来,我们可以调用cloneSheet方法来复制源Excel中的sheet到目标Excel中的新sheet对象中。cloneSheet方法会返回一个新的Sheet对象,代表了复制的sheet。 最后,我们可以调用Workbook对象的write方法来将目标Excel写入到文件中,实现保存操作。 以下是一个示例代码: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Sheet; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopySheetExample { public static void main(String[] args) throws IOException { String sourceFilePath = "source.xlsx"; String targetFilePath = "target.xlsx"; String sheetName = "Sheet1"; // 加载源Excel文件 Workbook sourceWorkbook; if (sourceFilePath.endsWith(".xlsx")) { sourceWorkbook = new XSSFWorkbook(new FileInputStream(sourceFilePath)); } else if (sourceFilePath.endsWith(".xls")) { sourceWorkbook = new HSSFWorkbook(new FileInputStream(sourceFilePath)); } else { throw new IllegalArgumentException("Unsupported file format. Only .xlsx and .xls files are supported."); } // 获取源Excel中的要复制的sheet Sheet sourceSheet = sourceWorkbook.getSheet(sheetName); if (sourceSheet == null) { throw new IllegalArgumentException("Sheet " + sheetName + " not found in the source Excel."); } // 创建目标Excel Workbook targetWorkbook = new XSSFWorkbook(); // 创建目标Excel中的新sheet Sheet targetSheet = targetWorkbook.createSheet(sheetName + " Copy"); // 复制源sheet到目标sheet targetWorkbook.cloneSheet(sourceWorkbook.getSheetIndex(sourceSheet)); // 保存目标Excel文件 FileOutputStream outputStream = new FileOutputStream(targetFilePath); targetWorkbook.write(outputStream); outputStream.close(); // 关闭工作簿 sourceWorkbook.close(); targetWorkbook.close(); } } ``` 以上示例代码演示了如何使用Apache POI复制sheet页到目标Excel,并将目标Excel保存到文件中。要使用这个示例,你需要将源Excel的文件路径和目标Excel的文件路径设置为正确的路径。 ### 回答3: Apache POI是一个用于创建、读取和操作Microsoft Office格式文件的Java库。在使用Apache POI复制sheet页到目标Excel时,可以按照以下步骤进行操作: 1. 首先,创建源和目标Excel文件的File对象,例如: ``` File sourceFile = new File("路径/源文件.xlsx"); File destFile = new File("路径/目标文件.xlsx"); ``` 2. 使用WorkbookFactory类的create()方法来加载源Excel文件,创建一个Workbook对象: ``` Workbook sourceWorkbook = WorkbookFactory.create(sourceFile); ``` 3. 使用Workbook对象的createSheet()方法来创建一个新的sheet页副本: ``` Sheet sourceSheet = sourceWorkbook.getSheet("源sheet名称"); Sheet destSheet = sourceWorkbook.createSheet("目标sheet名称"); ``` 4. 使用源sheet页的getLastRowNum()方法获取最后一行的行号,并循环复制每一行到目标sheet页: ``` int lastRowNum = sourceSheet.getLastRowNum(); for (int i = 0; i <= lastRowNum; i++) { Row sourceRow = sourceSheet.getRow(i); Row destRow = destSheet.createRow(i); int lastCellNum = sourceRow.getLastCellNum(); for (int j = 0; j < lastCellNum; j++) { Cell sourceCell = sourceRow.getCell(j); Cell destCell = destRow.createCell(j); destCell.setCellValue(sourceCell.getStringCellValue()); } } ``` 5. 使用Workbook对象的write()方法将目标Workbook写入目标Excel文件: ``` FileOutputStream fos = new FileOutputStream(destFile); sourceWorkbook.write(fos); fos.close(); ``` 完成以上步骤后,源Excel文件中的sheet页将被复制到目标Excel文件中。可以根据需要进行调整和修改以上代码,以适应特定的应用场景。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值