Wednesday, February 20, 2013

Selenium: Copy data from one Excel Sheet to another

Program to Copy 1 Data from 1 Excel to another



import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import javax.swing.JOptionPane;
import jxl.*;
import jxl.write.*;
import jxl.write.Number;
import jxl.write.biff.RowsExceededException;
import jxl.read.biff.BiffException;
import jxl.write.*;
import Utilityfunction.File_StringCheck;
import java.util.Date;



public class Test
{

public static void main(String[] args) throws BiffException, IOException, WriteException
{
String sTS=Time_stamp();
//String sSrc=
msExcel_Copy_File("D:\\Selenium\\test\\test.xls", "D:\\Selenium\\test\\test_"+sTS+".xls", "Sheet1", "Sheet1");
//System.out.print(;//("D:\\Selenium\\test\\output_"+sTS+".xls"), workbook);
}

public static String Time_stamp()
{
//java.util.Date date = new java.util.Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
return (((dateFormat.format(date)).replace("/", "_")).replace(":", "_")).replace(" ", "_");

}

public static boolean msExcel_Copy_File(String sSrc_File , String sDest_File ,String sSrc_Sheet,String sDest_Sheet) throws BiffException, IOException, RowsExceededException, WriteException
{
String sTemp;
Label Llabel;
Workbook workbook1 = Workbook.getWorkbook(new File(sSrc_File));
Sheet oSheet1=workbook1.getSheet(sSrc_Sheet);

WritableWorkbook workbook2 = Workbook.createWorkbook(new File(sDest_File), workbook1);
WritableSheet oSheet2 = workbook2.getSheet(sDest_Sheet);


for (int i=0 ; i<=oSheet1.getRows()-1;i++)
{
for (int j=0 ; j<=oSheet1.getColumns()-1;j++)
{
sTemp=oSheet1.getCell(j, i).getContents();
Llabel = new Label(j, i, sTemp);
oSheet2.addCell(Llabel);
}
}
workbook2.write();
workbook2.close();
workbook1.close();
return true;
}


}

1 comment:

  1. Hi Deepak,

    I am unable to understand the Strin sTE, also while running the code getting error "Unable to recognize OLE stream". Can you please help?

    ReplyDelete