After copying sheet from one workbook to another , original workbook is not getting open.

36 views Asked by At

I want to copy sheet from one workbook to another workbook using jxl After copying sheet from one workbook to another , original workbook is not getting open. The code works fine for creating a copy of sheet from another workbook.But it is changing the fileformat of original workbook.i.e When i am opening the sourcebook it saying the fileformat is not valid.

FileInputStream fs = new FileInputStream("D:\\Krishna_B\\Selenium_Projects\\TestData\\data1.xls");


  FileOutputStream os = new FileOutputStream("D:\\Krishna_B\\Selenium_Projects\\TestData\\Outputdata\\outputdata11.xls");
  WritableWorkbook targetbook = Workbook.createWorkbook(os);
  Workbook SourceBook = Workbook.getWorkbook(fs);
  WritableWorkbook writableTempSource = Workbook.createWorkbook(new File("D:\\Krishna_B\\Selenium_Projects\\TestData\\data1.xls"),SourceBook);
  WritableSheet SourceSheet   = writableTempSource.getSheet(1);
  //Sheet sourceSheet = SourceBook.getSheet(1);
  WritableSheet targetSheet = targetbook.createSheet("mm",0);
  System.out.println("Sheet Name ++"+SourceSheet.getName());
  //wriBook.importSheet(e2.getName(), 0,e2);


  System.out.println("sourceSheet rows "+SourceSheet.getRows());
  System.out.println("sourceSheet cols "+SourceSheet.getColumns());
  for (int col = 0 ; col < SourceSheet.getColumns(); col++){
       for (int row = 0 ; row < SourceSheet.getRows() ; row++){


          WritableCell readCell = SourceSheet.getWritableCell(col, row);
          WritableCell newCell = readCell.copyTo(col, row);

          WritableCell targetcell = targetSheet.getWritableCell(col, row);
            jxl.format.CellFormat readFormat = readCell.getCellFormat();


            if(readFormat!=null){

                WritableCellFormat newFormat = new WritableCellFormat(readFormat);
                newCell.setCellFormat(newFormat);

                  targetSheet.addCell(newCell);

            }



       }
    }



  targetbook.write();
  targetbook.close();
  SourceBook.close();
  writableTempSource.close();
0

There are 0 answers