将两个或多个xls文件合并为工作表


Combine two or more xls files as worksheets PHPExcel

我一直在到处寻找如何使用两个现有文件做到这一点,看起来所有文档都是关于创建新文件的。我想采取其中一个文件,并将第二个文件添加到它作为一个新的工作表,然后将其保存到服务器。我一直在尝试,但没有这样的效果:

$file="test.xls";
$file2="test2.xls";
$outputFile = "final.xls";
$phpExcel = new PHPExcel($file);
$phpExcel->getActiveSheet();
$phpExcel->setActiveSheetIndex(0);
$phpExcel->addSheet($file2);
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$outputFile");
header("Cache-Control: max-age=0");
$objWriter = PHPExcel_IOFactory::createWriter($phpExcel, "Excel5");
file_put_contents($outputFile, $objWriter);

任何帮助都将非常感激。对PHP非常陌生。

现在没有人读文档了吗?在名为/Documentation的文件夹中有一个完整的文档,关于将文件读取到PHPExcel对象(称为PHPExcel User Documentation - Reading Spreadsheet Files),以及数十个示例(/Documentation/Examples/Reader文件夹是一个很好的地方),并且没有一个使用new PHPExcel($file)。也没有任何示例或任何文档说在保存时使用file_put_contents()。

$file1="test.xls";
$file2="test2.xls";
$outputFile = "final.xls";
// Files are loaded to PHPExcel using the IOFactory load() method
$objPHPExcel1 = PHPExcel_IOFactory::load($file1);
$objPHPExcel2 = PHPExcel_IOFactory::load($file2);
// Copy worksheets from $objPHPExcel2 to $objPHPExcel1
foreach($objPHPExcel2->getAllSheets() as $sheet) {
    $objPHPExcel1->addExternalSheet($sheet)
}
// Save $objPHPExcel1 to browser as an .xls file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel1, "Excel5");
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$outputFile");
header("Cache-Control: max-age=0");
$objWriter->save('php://output');
    // update from office site
    $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: 
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/

// Combine all .csv files into one .xls file,
$cache_dir = "/home/user_name/public_html/";
$book1 = $cache_dir . "book1.csv";
$book2 = $cache_dir . "book2.csv";       
$outputFile = $cache_dir . "combined.xls";
$inputFileType = 'CSV'; 
$inputFileNames = array($book1,$book2); 
$objReader = new PHPExcel_Reader_CSV();
/**  Extract the first named file from the array list  **/ 
$inputFileName = array_shift($inputFileNames); 
/**  Load the initial file to the first worksheet in a PHPExcel Object  **/ 
$objPHPExcel = $objReader->load($inputFileName); 
/**  Set the worksheet title (to the filename that we've loaded)  **/ 
$objPHPExcel->getActiveSheet()
            ->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME)); 
/**  Loop through all the remaining files in the list  **/ 
foreach($inputFileNames as $sheet => $inputFileName) { 
     /**  Increment the worksheet index pointer for the Reader  **/ 
     $objReader->setSheetIndex($sheet+1); 
     /**  Load the current file into a new worksheet in PHPExcel  **/ 
     $objReader->loadIntoExisting($inputFileName,$objPHPExcel); 
     /**  Set the worksheet title (to the filename that we've loaded)  **/ 
     $objPHPExcel->getActiveSheet()->setTitle(pathinfo($inputFileName,PATHINFO_BASENAME)); 
} 
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save( $outputFile );
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);
echo "DONE";