POI读取EXCEL进行sheet复制
/******************************************************************** * * (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;}}?