2013-10-05 71 views
0

我想將工作表從多個Excel工作表合併到一個新的Excel文件中的工作表中。使用PHPExcel合併工作表

我提到的討論https://phpexcel.codeplex.com/discussions/390898

我不明白MarkBaker使用指定者指()。我的意思是我如何使用它。 我當前的代碼是:

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

有了這個,我能得到的各種文件不同的工作表到新工作簿,但不同的表。

我應該怎麼做才能像vitman說的那樣把它全部放在同一張紙上? first_excel_file_with_one_worksheet --empty line-- second_excel_file_with_one_worksheet --empty line-- 等

回答

0

//從辦公室網站更新

$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/