package poi;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class CopyFromSheet1ToSheet2 {
	//Q: How to copy from one sheet to another sheet?
	
	public static void main(String[] args) {
		copyExcelData();
	}

	static void copyExcelData() {
		Workbook wb = null;
		Sheet sh1 = null;
		Sheet sh2 = null;
		Row row1 = null;
		Row row2 = null;
		Cell cell1 = null;
		Cell cell2 = null;
		FileInputStream fin = null;
		FileOutputStream fout = null;
		String strData = null;
		int rowCount = 0;
		int colCount = 0;
		try {
			fin = new FileInputStream("C:\\MorningBatch_Combined\\Sample.xlsx");
			wb = new XSSFWorkbook(fin);
			sh1 = wb.getSheet("testData");
			
			if(wb.getSheet("Result") == null) {
				sh2 = wb.createSheet("Result");
			}else {
				sh2 = wb.getSheet("Result");
			}
			
			rowCount = sh1.getPhysicalNumberOfRows();
			for(int r=0; r<rowCount; r++) {
				row1 = sh1.getRow(r);
				
				if(sh2.getRow(r) == null) {
					row2 = sh2.createRow(r);
				}else {
					row2 = sh2.getRow(r);
				}
				
				colCount = row1.getPhysicalNumberOfCells();
				
				for(int c=0; c<colCount; c++) {
					cell1 = row1.getCell(c);
					
					if(row2.getCell(c) == null) {
						cell2 = row2.createCell(c);
					}else {
						cell2 = row2.getCell(c);
					}
					
					strData = cell1.getStringCellValue();
					cell2.setCellValue(strData);
				}
			}
			
			fout = new FileOutputStream("C:\\MorningBatch_Combined\\Sample.xlsx");
			wb.write(fout);
		}catch(Exception e) {
			e.printStackTrace();
		}
		finally
		{
			try {
				fout.flush();
				fout.close();
				fout = null;
				fin.close();
				fin = null;
				cell1 = null;
				cell2 = null;
				row1 = null;
				row2 = null;
				sh1 = null;
				sh2 = null;
				wb.close();
				wb = null;
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
	}
}
