将工作表从一个工作簿复制到另一个工作簿后,原始工作簿未打开。

时间:2014-11-19 06:18:16

标签: java excel selenium jxl

我想使用jxl将工作表从一个工作簿复制到另一个工作簿 将工作表从一个工作簿复制到另一个工作簿后,原始工作簿未打开。 该代码适用于从另一个工作簿创建工作表的副本。但它正在更改原始工作簿的文件格式.i.e当我打开源码表时,它说fileformat无效。

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 个答案:

没有答案