使用PHPExcel合并工作表 [英] Merge worksheets using PHPExcel

查看:172
本文介绍了使用PHPExcel合并工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将多个Excel工作表中的工作表合并到一个新的Excel文件中的1个工作表中.

I want to merge worksheets from multiple excel sheets into 1 worksheet in a new excel file.

我提到了讨论 https://phpexcel.codeplex.com/discussions/390898

我不明白MarkBaker使用toArray()意味着什么.我的意思是我该如何使用它. 我当前的代码是:

I did not understand what MarkBaker meant by using toArray(). I mean how do I use it. My Current Code is:

$file1="file1.xlsx";
$objPHPExcel1 = PHPExcel_IOFactory::load($file1);
$file2="file2.xlsx";
$outputFile = "output.xlsx";

$objPHPExcel2 = PHPExcel_IOFactory::load($file2);
$sheet = $objPHPExcel2->getActiveSheet();
$objPHPExcel1->addExternalSheet($sheet);

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel1); 
$objWriter->save($outputFile);

借助此功能,我可以将各种文件的不同工作表放入新的工作簿中,但可以作为不同的工作表.

With this I am able to get different worksheets of various files into a new workbook but as different sheets.

我应该怎么做才能像vitman所说的那样将它们全部放在同一张纸上? first_excel_file_with_one_worksheet -空行- second_excel_file_with_one_worksheet -空行- 等

What should I do to get it all onto the same sheet like vitman said? first_excel_file_with_one_worksheet --empty line-- second_excel_file_with_one_worksheet --empty line-- etc.

推荐答案

//从办公网站更新

$filenames = array('doc1.xlsx', 'doc2.xlsx');

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

$reader = PHPExcel_IOFactory::createReader($input_file_type);

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 = PHPExcel_IOFactory::createWriter($bigExcel, 'Excel5');

$file_creation_date = date("Y-m-d");

// name of file, which needs to be attached during email sending
$saving_name = "Report_Name" . $file_creation_date . '.xls';


// save file at some random location    
$writer->save($file_path_location . $saving_name);

// More Detail : with different object: 

在此处说明将多个xls文件合并为一个文件: 我将描述一些不同的地方: http://rosevinod.wordpress.com/2014/03/15/combine-two-or-more-xls-files-as-worksheets-phpexcel/

Merge multiple xls file into single one is explained here: I'm going to describe a bit different: http://rosevinod.wordpress.com/2014/03/15/combine-two-or-more-xls-files-as-worksheets-phpexcel/

这篇关于使用PHPExcel合并工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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