Je cherche partout sur la façon de faire cela avec deux fichiers existants, on dirait que toute la documentation consiste à créer de nouveaux fichiers. J'aimerais prendre l'un des fichiers et ajouter le deuxième fichier en tant que nouvelle feuille de calcul puis enregistrez-la sur le serveur. J'essaie sans être disponible comme ceci:
$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);
3 Réponses :
Quelqu'un n'a-t-il jamais lu la documentation ces jours-ci? Il y a tout un document dans le dossier appelé / documentation code> sur les fichiers de lecture sur phpexcel Objets (c'est appelé
Documentation utilisateur phpexcel - Lecture de fichiers de feuille de calcul code>), avec des dizaines d'exemples (le / Documentation / Exemples / Reader CODE> Le dossier est un bon endroit pour regarder) et aucun d'entre eux n'utilise
nouveau phpexcel ($ fichier) code>. Ni font aucun des exemples ni aucun des documents ne disent à utiliser File_Put_Contents () lors de l'enregistrement.
$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');
Je suis vraiment prêt à abandonner n'importe quoi de documenter avec PHPExcel ... Je ne peux plus faire plus évident que la documentation est dans le dossier appelé / documentation code> ou que les documents sur la lecture de fichiers de feuille de calcul puissent être appelé
lire des fichiers de feuille de calcul code>
Hey! Vous êtes le développeur pour cela! Je reconnais ton visage de Github. J'ai essayé de chercher à travers les docs dessus. Pardon. J'ai utilisé File_Put_Contents () pour enregistrer le fichier sur le disque.
@MarkBaker Si cela vous fait sentir mieux, j'aime phpexcel. Ça fait ma vie super facile. Continuez le bon travail. N'abandonnez pas. Je suis sûr qu'il y a des milliers qui apprécient vraiment l'effort que vous avez mis dans cette situation! :)
Erreur fatale: appel à la méthode indéfinie phpexcel :: getsworksheets () code> pourquoi suis-je l'erreur ci-dessus .. ?? :-(
Essayez $ objphpexcel2-> getallsheets () code> à la place.
// 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";