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
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值