`
andymu1117
  • 浏览: 36736 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

POI读取EXCEL进行sheet复制

    博客分类:
  • java
 
阅读更多
/********************************************************************
 *
 * (C) Copyright ISFnet Japan, Ltd. 2011 All rights reserved.
 *
 ********************************************************************/
package excel;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

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.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;

/**
 * <p>
 * 
 * @author ISFnet DALIAN muzongqin
 * @since 2011/08/10
 * @version 1.0
 */
public class CopyExcel {

	/**
	 * 
	 * 
	 * 
	 * @param args
	 *            void
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException {
		// テンプレートとなるExcelファイルのパスを取得します。
		final String INVOICE_FILE = "C:\\work\\jyuchu\\仕様書\\出力帳票\\修正済【完成】3_派遣労働者通知書.xls";
		// ファイルを読み込みます。
		POIFSFileSystem filein = new POIFSFileSystem(new FileInputStream(INVOICE_FILE));
		// ワークブックを読み込みます。
		HSSFWorkbook wb = new HSSFWorkbook(filein);
		// シートを読み込みます。
		HSSFSheet sheet1 = wb.getSheet("派遣労働者通知書");
		HSSFSheet sheet2 = wb.createSheet("派遣労働者通知書1");
		sheet2 = copySheet(sheet1, sheet2);
		FileOutputStream fileOut = new FileOutputStream("d:\\test1.xls");
		wb.write(fileOut);
		fileOut.close();

	}

	private static HSSFSheet copySheet(HSSFSheet sheetFrom, HSSFSheet sheetTo) {

		// 初期化
		CellRangeAddress region = null;
		Row rowFrom = null;
		Row rowTo = null;
		Cell cellFrom = null;
		Cell cellTo = null;
		
		//セル結合のコピー
		for (int i = 0; i < sheetFrom.getNumMergedRegions(); i++) {
			region = sheetFrom.getMergedRegion(i);
			if ((region.getFirstColumn() >= sheetFrom.getFirstRowNum())
					&& (region.getLastRow() <= sheetFrom.getLastRowNum())) {
				sheetTo.addMergedRegion(region);
			}
		}

		//セルのコピー
		for (int intRow = sheetFrom.getFirstRowNum(); intRow < sheetFrom.getLastRowNum(); intRow++) {
			rowFrom = sheetFrom.getRow(intRow);
			rowTo = sheetTo.createRow(intRow);
			if (null == rowFrom)
				continue;
			rowTo.setHeight(rowFrom.getHeight());
			for (int intCol = 0; intCol < rowFrom.getLastCellNum(); intCol++) {
				//セル幅のコピー
				sheetTo.setDefaultColumnStyle(intCol, sheetFrom.getColumnStyle(intCol));
				sheetTo.setColumnWidth(intCol, sheetFrom.getColumnWidth(intCol));
				cellFrom = rowFrom.getCell(intCol);
				cellTo = rowTo.createCell(intCol);
				if (null == cellFrom)
					continue;
				//セルスタイルとタイプのコピー
				cellTo.setCellStyle(cellFrom.getCellStyle());
				cellTo.setCellType(cellFrom.getCellType());
				
				//タイトル内容のコピー
				if (null != cellFrom.getStringCellValue() && !"".equals(cellFrom.getStringCellValue().trim()))
					cellTo.setCellValue(cellFrom.getStringCellValue());
			}
		}
		
		//枠線の設定
		sheetTo.setDisplayGridlines(false);
		//Excelのズーム設定
		sheetTo.setZoom(80, 100);
		
		//シートを戻る。
		return sheetTo;

	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics