使用php合并Excel文件 [英] Merge Excel files using php

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

问题描述

我有两个Excel文件,每个文件都有一个工作表.我要使用两个Excel文件,并使用两个工作表创建一个Excel文件.我想用PHP做到这一点.谁能指出我正确的方向?

I have two excel files each with a single worksheet. I want to take the two excel files and create a single excel file with two worksheets. I would like to do this with PHP. Can anyone point me in the right direction?

推荐答案

使用 PHPExcel

$inputFileType1 = 'Excel2007';
$inputFileName1 = 'inputData1.xlsx';
$inputFileType2 = 'Excel5';
$inputFileName2 = 'inputData2.xls';
$outputFileType = 'Excel5';
$outputFileName = 'outputData.xls';

// Load the first workbook (an xlsx file)
$objPHPExcelReader1 = PHPExcel_IOFactory::createReader($inputFileType1);
$objPHPExcel1 = $objPHPExcelReader1->load($inputFileName1);

// Load the second workbook (an xls file)
$objPHPExcelReader2 = PHPExcel_IOFactory::createReader($inputFileType2);
$objPHPExcel2 = $objPHPExcelReader2->load($inputFileName2);

// Merge the second workbook into the first
$objPHPExcel2->getActiveSheet()->setTitle('Unique worksheet name');
$objPHPExcel1->addExternalSheet($objPHPExcel2->getActiveSheet());

// Save the merged workbook under a new name (could save under the original name)
// as an xls file
$objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel1,$outputFileType);
$objPHPExcelWriter->save($outputFileName);

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

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