将一张表的内容和样式克隆到另一张表-PHP Excel


Cloning the content and styling of one sheet to another - PHP Excel

我需要将Excel工作簿中一张工作表的内容复制到新Excel工作簿中的工作表中。问题是,我不知道图纸包含什么或它们的格式。然而,它每次都只是第一张纸。

我试过一种方法,但每次都会失忆。所以我想我会一排排地做10万排。不管怎样,我从一行开始,它得到了数据,但在把它写到新表中时运气不佳。我该怎么做?

以下是我目前所拥有的:

// Open the current worksheet
App::import('Vendor', 'PHPExcel');
if (!class_exists('PHPExcel')) {
    throw new CakeException('Vendor class PHPExcel not found!');
$this->xls = PHPExcel_IOFactory::load($path);
// Read all content
$this->xls->getActiveSheet()->rangeToArray('A1:ZZZZ1');
// Close current worksheet
$this->xls->disconnectWorksheets();
// Delete worksheet
unlink($destination);
// Open new worksheet
$this->xls = new PHPExcel();
$newWorksheet = new PHPExcel_Worksheet($this->xls, 'Sheet 1');
$this->xls->addSheet($newWorksheet);
$this->xls->setActiveSheetIndexByName('Sheet 1');
// Write all the content
$this->xls->getActiveSheet()->fromArray($array,null,'A1');
// Save current worksheet
$objWriter = PHPExcel_IOFactory::createWriter($this->xls, 'Excel2007');
$objWriter->save($destination);

我做错了什么?

还有,有没有更简单的方法可以做到这一点?我不想写所有这些代码,最终都是在重新发明轮子?

提前感谢

有一个专门为您编写的内置方法:

$objPHPExcel1 = PHPExcel_IOFactory::load($path);
$objPHPExcel2 = new PHPExcel();
// Copy active worksheet from $objPHPExcel1 to $objPHPExcel2
$worksheet = $objPHPExcel1->getActiveSheet();
$objPHPExcel2->addExternalSheet($worksheet)

如果内存不足,那么在内存中构建大型阵列来尝试手动复制是没有帮助的。

有一些方法可以减少内存,如单元缓存,希望使用这些方法来减少内存使用