简体   繁体   English

PHPexcel CSV主文件

[英]PHPexcel CSV master file

I would like to have multiple csv's and add them in a master csv file. 我想要多个csv,并将它们添加到主csv文件中。 It when I add an extra csv file in the array it throws a exception error. 当我在数组中添加一个额外的csv文件时,它将引发异常错误。

Uncaught exception 'Exception' with message 'Workbook already contains a worksheet named 'Worksheet'. Rename the external sheet first.' in
Please find below my code 请在下面找到我的代码

include'../Classes/PHPExcel.php';
include'../Classes/PHPExcel/IOFactory.php';

$filenames = array('Sheet1.csv','Sheet2.csv');

$bigExcel = new PHPExcel();
$bigExcel->removeSheetByIndex(0);

$reader = new PHPExcel_Reader_CSV();

foreach ($filenames as $filename) {
$excel = $reader->load($filename);
foreach ($excel->getAllSheets() as $sheet) {
    $bigExcel->addExternalSheet($sheet);
}
foreach ($excel->getNamedRanges() as $namedRange) {
    $bigExcel->addNamedRange($namedRange);
}
}
$writer = new PHPExcel_Writer_CSV($bigExcel);
$writer->save('2007-write.csv');

The problem is with the following code: 问题在于以下代码:

foreach ($excel->getAllSheets() as $sheet) {
    $bigExcel->addExternalSheet($sheet);
}

For file 1 you add a sheet called worksheet, and then for file two you try to add a sheet with the same name. 对于文件1,您添加一个名为工作表的工作表,然后对于文件2,您尝试添加具有相同名称的工作表。

I suggest you check if a sheetname has already been added, and if so you can use "setActiveSheetIndex" to switch to the existing sheet. 我建议您检查是否已经添加了工作表名称,如果是这样,则可以使用“ setActiveSheetIndex”切换到现有工作表。

Another option is to change the name to worksheet(2) if you find one that already exists. 如果找到已经存在的名称,另一种选择是将名称更改为worksheet(2)。

But since you called it CSV I dont think you can use worksheets. 但是由于您将其称为CSV,所以我认为您不能使用工作表。 So in the end it should all just go in a single worksheet, so no need to keep adding new workheets. 因此最后,所有这些都应该放在一个工作表中,因此无需继续添加新的工作表。 Just create one and add all the data to that one. 只需创建一个并将所有数据添加到该数据即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM