博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poi Excel复制指定行案例
阅读量:4261 次
发布时间:2019-05-26

本文共 3993 字,大约阅读时间需要 13 分钟。

一、poi excel复制指定行

    

package com.smart.test;import java.io.FileInputStream;import java.io.FileOutputStream;import org.apache.poi.hssf.usermodel.HSSFCell;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.hssf.util.Region;import org.apache.poi.poifs.filesystem.POIFSFileSystem;public class ExcelCopy {	public static void main(String[] args) throws Exception {		copyTemplate("Template.xls",				"workbook.xls");	}	public static void copyTemplate(String exTemplateFilePath,			String targetFilePath) throws Exception {		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(				exTemplateFilePath));		HSSFWorkbook wb = new HSSFWorkbook(fs);		HSSFSheet sheet = wb.getSheetAt(0);		int lastRow = sheet.getLastRowNum() + 1;		copyRows(sheet, wb.getSheetAt(0), 1, lastRow, 20);		FileOutputStream fileOut = new FileOutputStream(targetFilePath);		wb.write(fileOut);		fileOut.flush();		fileOut.close();	}	@SuppressWarnings("all")	public static void copyRows(HSSFSheet sourceSheet, HSSFSheet targetSheet,			int startRow, int endRow, int targetPosition) {		int pStartRow = startRow - 1;		int pEndRow = endRow - 1;		int pPosition = targetPosition - 1;		HSSFRow sourceRow = null, targetRow = null;		HSSFCell sourceCell = null, targetCell = null;		Region region = null;		int cType, i, j, targetRowFrom, targetRowTo;		if (pStartRow < 0 || pEndRow < 0 || pStartRow > pEndRow) {			return;		}		// 获取合并行单元格的数目,并设定目标单元格合并属性		int mergedc = sourceSheet.getNumMergedRegions();		for (i = 0; i < mergedc; i++) {			region = sourceSheet.getMergedRegionAt(i);			int rf = region.getRowFrom();			int rt = region.getRowTo();			if ((rf >= pStartRow) && (rt <= pEndRow)) {				targetRowFrom = rf - pStartRow + pPosition;				targetRowTo = rt - pStartRow + pPosition;				region.setRowFrom(targetRowFrom);				region.setRowTo(targetRowTo);				targetSheet.addMergedRegion(region);			}		}		//设定个单元格的列宽		for (i = pStartRow; i <= pEndRow; i++) {			sourceRow = sourceSheet.getRow(i);			if (sourceRow != null) {				int firstC = sourceRow.getFirstCellNum();				for (j = sourceRow.getLastCellNum(); j > firstC; j--) {					targetSheet							.setColumnWidth(j, sourceSheet.getColumnWidth(j));					targetSheet.setColumnHidden(j, false);				}				break;			}		}		//填充数据		for (; i <= pEndRow; i++) {			sourceRow = sourceSheet.getRow(i);			if (sourceRow == null) {				continue;			}			targetRow = targetSheet.createRow(i - pStartRow + pPosition);			targetRow.setHeight(sourceRow.getHeight());			int psy = sourceRow.getPhysicalNumberOfCells();			for (j = sourceRow.getFirstCellNum(); j < psy; j++) {				sourceCell = sourceRow.getCell(j);				if (sourceCell == null) {					continue;				}				targetCell = targetRow.createCell(j);				targetCell.setCellStyle(sourceCell.getCellStyle());				cType = sourceCell.getCellType();				targetCell.setCellType(cType);				switch (cType) {				case HSSFCell.CELL_TYPE_BOOLEAN:					targetCell.setCellValue(sourceCell.getBooleanCellValue());					break;				case HSSFCell.CELL_TYPE_ERROR:					targetCell							.setCellErrorValue(sourceCell.getErrorCellValue());					break;				case HSSFCell.CELL_TYPE_FORMULA:					targetCell.setCellFormula(parseFormula(sourceCell							.getCellFormula()));					break;				case HSSFCell.CELL_TYPE_NUMERIC:					targetCell.setCellValue(sourceCell.getNumericCellValue());					break;				case HSSFCell.CELL_TYPE_STRING:					targetCell							.setCellValue(sourceCell.getRichStringCellValue());					break;				}			}		}	}	private static String parseFormula(String pPOIFormula) {		final String cstReplaceString = "ATTR(semiVolatile)"; //$NON-NLS-1$ 		StringBuffer result = null;		int index;		result = new StringBuffer();		index = pPOIFormula.indexOf(cstReplaceString);		if (index >= 0) {			result.append(pPOIFormula.substring(0, index));			result.append(pPOIFormula.substring(index					+ cstReplaceString.length()));		} else {			result.append(pPOIFormula);		}		return result.toString();	}}

转载地址:http://zhxei.baihongyu.com/

你可能感兴趣的文章
如何判断一个对象是否可回收,GC回收对象的过程方式,finilized函数
查看>>
java普通for循环和增强for循环中做集合增删会不会出错?
查看>>
抽象类和接口区别
查看>>
JVM学习之对象内存布局,对象头
查看>>
Python urllib模块访问网络
查看>>
JVM学习之java线程安全&锁优化技术
查看>>
兼容ProgressBar圆形设置颜色
查看>>
git detached HEAD 修改后如何提交修改到其他分支
查看>>
Android获取系统中的其他应用信息
查看>>
Android视频编解码之MediaCodec简单入门
查看>>
Android原始视频格式YUV,NV12,NV21,YV12,YU12(I420)
查看>>
View绘制01-Android渲染系统中的View
查看>>
View绘制02-View生命周期
查看>>
View绘制系列(3)-自定义View简介
查看>>
View绘制系列(5)-Canvas基础图形绘制
查看>>
Android横竖屏切换
查看>>
判断SD是否存在及其容量查询
查看>>
linux查看文本的5+1种方式
查看>>
Linux 查看服务器开放的端口号
查看>>
端口状态说明 LISTENING、ESTABLISHED、TIME_WAIT及CLOSE_WAIT
查看>>