将一张表的内容和样式克隆到另一张 - PHP Excel [英] Cloning the content and styling of one sheet to another - PHP Excel

查看:416
本文介绍了将一张表的内容和样式克隆到另一张 - PHP Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将Excel工作簿中的一个工作表的内容复制到新的excel工作簿中的工作表。问题是,我不知道表单包含或格式。但是,它每次只能是第一张。

I need to copy the content of one sheet in an Excel Workbook to a sheet in a new excel workbook. The issue is, I have no idea what the sheets contain or their formatting. However, it will only be the first sheet every time.

我尝试过一种方法,但每次都用完内存。所以我以为我会一行一行地行10万行。无论如何,我开始一排,它得到的数据,但没有运气写在一张新的表格。我该怎么做?

I've tried an approach but I run out of memory every time. So I thought I'd do it row by row for 100 000 rows. Anyway, I started with a row, and it gets the data, but no luck in writing it to a new sheet. How would I do that?

这是我迄今为止所做的:

Here is what I have so far:

// 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);

我做错了什么?

那么还有更简单的方法呢?我不想写所有这些代码,最终都是重新发明的轮?

Then also, is there an easier way to do this? I don't want to write all this code and in the end it's all reinventing the wheel?

提前感谢

推荐答案

有一个内置的方法,专门为您做这些:

There's a built-in method that's specifically written to do this for you:

$objPHPExcel1 = PHPExcel_IOFactory::load($path);
$objPHPExcel2 = new PHPExcel();

// Copy active worksheet from $objPHPExcel1 to $objPHPExcel2
$worksheet = $objPHPExcel1->getActiveSheet();
$objPHPExcel2->addExternalSheet($worksheet)

如果你用完了的内存,在内存中构建大型数组以手动尝试和复制不会有帮助。

and if you're running out of memory, building large arrays in memory to try and copy manually isn't going to help.

有些方法旨在减少内存,如单元缓存,寻求使用那些减少内存使用量的消息

There are approaches designed to reduce memory such as cell caching, look to use those to reduce memory usage

这篇关于将一张表的内容和样式克隆到另一张 - PHP Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆