如何拆分包含多个工作表的Excel文件?

时间:2012-05-14 13:17:03

标签: php excel

我的Excel文件包含几张纸。如何拆分此文件以将每个工作表作为单独的文件?

2 个答案:

答案 0 :(得分:1)

从PHP开始,您需要phpExcel之类的东西来打开电子表格,并将每个标签重写为新文件。

答案 1 :(得分:1)

使用PHPExcel库:

include 'PHPExcel.php';

$fileType = 'Excel2007';
$inputFileName = 'testExcel.xlsx';

$objPHPExcelReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objPHPExcelReader->load($inputFileName);

$sheetIndex = 0;
$sheetCount = $objPHPExcel->getSheetCount();
while ($sheetIndex < $sheetCount) {
    ++$sheetIndex;
    $workSheet = $objPHPExcel->getSheet(0);

    $newObjPHPExcel = new PHPExcel();
    $newObjPHPExcel->removeSheetByIndex(0);
    $newObjPHPExcel->addExternalSheet($workSheet);

    $objPHPExcelWriter = PHPExcel_IOFactory::createWriter($newObjPHPExcel,$fileType);
    $outputFileTemp = explode('.',$inputFileName);
    $outputFileName = $outputFileTemp[0].$sheetIndex.'.'.$outputFileTemp[1];
    $objPHPExcelWriter->save($outputFileName);
}