PHP合并多个excel文件

问题描述:只想用 php 代码合并多个excel 文件,因为 php 环境简单,可能随时移动,那就只要下载一个PhpStudy 就可以使用了。

问题解决:
在这里插入图片描述
目录如下:
在这里插入图片描述

index.php 代码如下:


```php
<?php

function mainExcel($beginNumber, $endNumber, $createFileName) {
    //扫描文件夹    scan file
    $dir =  dirname(__FILE__) . '/xlsfiles/';
    $file = scandir($dir);

    $all_excele_data_arr = [];      // 定义所有excel 文件的数据列表 define excel all data array

    foreach ($file as $key => $value) {
            // 判断是不是文件 whether it is file or not
        if (is_file($dir.$value)) {
            $all_excele_data_arr[] = excelToArray($beginNumber, $endNumber, $value, $all_excele_data_arr);          // 把返回的数组全部放入到一个数组里面
        }

    }

    exportExcel2($all_excele_data_arr, $createFileName);

            // 测试用 for test
    //print_r(count($all_excele_data_arr));                             // 打印数组,看看长度,确定一下有没有 print array length to check collected or not 
    //foreach ($all_excele_data_arr as $key => $value) {                // 第三层数组,才取到数据 third folor , the data can by read
    //    echo $key . "SSSSSSSSSSSS" . $value . "<br />";
    //    foreach ($value as $k => $v) {
    //        echo $k . "=================" . $v . "<br />";
    //        foreach ($v as $n => $m) {
    //            echo $n . "-----------" . $m . "<br />";
    //        }
    //    }
    //}
    
}

function excelToArray($rowBeginNumber, $columnEndNumber, $excelfile, $all_excele_data_arr){  
    require_once dirname(__FILE__) . '/PHPExcel/IOFactory.php';  
    //加载excel文件  
    $filename = dirname(__FILE__) . '/xlsfiles/' . $excelfile;
    $objPHPExcelReader = PHPExcel_IOFactory::load($filename);    
  
    $reader = $objPHPExcelReader->getWorksheetIterator();  
    //循环读取sheet  
    foreach($reader as $sheet) {  
        //读取表内容  
        $content = $sheet->getRowIterator();  
        //逐行处理  

        foreach($content as $key => $items) {  
             $rows = $items->getRowIndex();              //行  
             $columns = $items->getCellIterator();       //列  
             $row_arr = array();  
             //确定从哪一行开始读取 the row begin to read  
             if($rows < $rowBeginNumber){
                 continue;
             }
             //逐列读取  read by row
             foreach($columns as $head => $cell) {  
                 //获取cell中数据   get cell data   // array_push($array, "blue", "yellow");
                if ($head == $columnEndNumber) {        // 遇到要终止的列就终止,读下一列
                    break;
                } else {
                    $data = $cell->getValue();
                    $row_arr[] = $data;
                }
             }  
             $all_excele_data_arr[] = $row_arr;         // 把数组放进数组 push array in array
        }  
    }

    return $all_excele_data_arr;                        // 把数组放进数组 push array in array
}


/** 
 * 创建(导出)Excel数据表格 
 * @param  array   $list        要导出的数组格式的数据 
 * @param  string  $filename    导出的Excel表格数据表的文件名 
 * @param  array   $indexKey    $list数组中与Excel表格表头$header中每个项目对应的字段的名字(key值) 
 * @param  array   $startRow    第一条数据在Excel表格中起始行 
 * @param  [bool]  $excel2007   是否生成Excel2007(.xlsx)以上兼容的数据表 
 * 比如: $indexKey与$list数组对应关系如下: 
 *     $indexKey = array('id','username','sex','age'); 
 *     $list = array(array('id'=>1,'username'=>'YQJ','sex'=>'男','age'=>24)); 
 */  

function exportExcel2($list,$filename){  
    require_once dirname(__FILE__) . '/PHPExcel/IOFactory.php';  
    require_once dirname(__FILE__) . '/PHPExcel.php';  
    require_once dirname(__FILE__) . '/PHPExcel/Writer/Excel2007.php';  
      
    $header_arr = array('A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z', 'AA', 'AB', 'AC', 'AD');  
    $indexKey = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 17, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29);  

    // $objPHPExcel = new PHPExcel();                        //初始化PHPExcel(),不使用模板 
    $template = dirname(__FILE__).'/template.xlsx';          //使用模板     use template
    $objPHPExcel = PHPExcel_IOFactory::load($template);     //加载excel文件,设置模板
    $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);  //设置保存版本格式  
      
    //接下来就是写数据到表格里面去  
    $objActSheet = $objPHPExcel->getActiveSheet();
            // 下面 2 行用于代码测试用
    //$objActSheet->setCellValue('A2', iconv('gbk', 'utf-8', '一点都不好了好'));  
    //$objActSheet->setCellValue('C2',  iconv('gbk', 'utf-8', "导出时间:".date('Y-m-d H:i:s')));
    
    $i = 4;
    
    foreach ($list as $key1 => $value1) {
        // 跳过头部,序列 (且如果姓名没有填写,就放弃不要了) if the name is empty, abandon the information
        
        foreach ($value1 as $key2 => $value2) {
            
            
            if (count($value2) > 5 && count($value2) < 40 && trim($value2[1]) != '') {
                foreach ($value2 as $a => $b) {
                        // 不能输出,否则报错 can't output information , or bug comes up 
                    //echo "the target cell is:::".$header_arr[$a].$i.":::and the value is ".$b."<br />";
                    $objActSheet->setCellValue( $header_arr[$a].$i, $b);
                    
                }
                $i++;
            }
            

        }
    }


    /*
    foreach ($list as $key => $row) {
        // 跳过头部,序列 (且如果姓名没有填写,就放弃不要了) if the name is empty, abandon the information
        if (trim($row[1]) != '' ) {
            foreach ($row as $a => $b) {
                    // 不能输出,否则报错 can't output information , or bug comes up 
                //echo "the target cell is:::".$header_arr[$a].$i.":::and the value is ".$b."<br />";
                $objActSheet->setCellValue( $header_arr[$a].$i, $b);
            }
            $i++;
        }
    }
    */

    // 1.保存至本地Excel表格  
    //$objWriter->save($filename.'.xlsx');  
      
    // 2.接下来当然是下载这个表格了,在浏览器输出就好了  
    
    header("Pragma: public");  
    header("Expires: 0");  
    header("Cache-Control:must-revalidate, post-check=0, pre-check=0");  
    header("Content-Type:application/force-download");  
    header("Content-Transfer-Encoding: binary");
    header("Content-Type:application/vnd.ms-execl");  
    header("Content-Type:application/octet-stream");  
    header("Content-Type:application/download");;  
    header('Content-Disposition:attachment;filename="'.$filename.'.xlsx"');  
    header("Pragma: no-cache");
    $objWriter->save('php://output');
}

mainExcel(4, 'AE', 'pharmacy');   // 每个文件起始的 1:行数, 2: 结束的列数; 3: 要生成的文件名;

?>

猜你喜欢

转载自blog.csdn.net/qq_38192709/article/details/112978009