php 导出导入excel

首先需要去官网https://github.com/PHPOffice/PHPExcel/下载PHPExcel,下载后只需要Classes目录下的文件即可。

链接: https://pan.baidu.com/s/1L3huK-3esd6pvM8tUe0zlg 密码: tn57

hui前端图标(http://www.h-ui.net/Hui-overview.shtml)

1、PHPExcel导出方法实现过程

/** 
 * 数据导出 
 * @param array $title   标题行名称 
 * @param array $data   导出数据 
 * @param string $fileName 文件名 
 * @param string $savePath 保存路径 
 * @param $type   是否下载  false--保存   true--下载 
 * @return string   返回文件全路径 
 * @throws PHPExcel_Exception 
 * @throws PHPExcel_Reader_Exception 
 */  
function exportExcel($title=array(), $data=array(), $fileName='', $savePath='./', $isDown=false){  
error_reporting(0); 
include('PHPExcel.php'); $obj = new PHPExcel(); //横向单元格标识 $cellName = 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', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'); $obj->getActiveSheet(0)->setTitle('sheet名称'); //设置sheet名称 $_row = 1; //设置纵向单元格标识 if($title){ $_cnt = count($title); $obj->getActiveSheet(0)->mergeCells('A'.$_row.':'.$cellName[$_cnt-1].$_row); //合并单元格 $obj->setActiveSheetIndex(0)->setCellValue('A'.$_row, '数据导出:'.date('Y-m-d H:i:s')); //设置合并后的单元格内容 $_row++; $i = 0; foreach($title AS $v){ //设置列标题 $obj->setActiveSheetIndex(0)->setCellValue($cellName[$i].$_row, $v); $i++; } $_row++; } //填写数据 if($data){ $i = 0; foreach($data AS $_v){ $j = 0; foreach($_v AS $_cell){ $obj->getActiveSheet(0)->setCellValue($cellName[$j] . ($i+$_row), $_cell); $j++; } $i++; } } //文件名处理 if(!$fileName){ $fileName = uniqid(time(),true); } $objWrite = PHPExcel_IOFactory::createWriter($obj, 'Excel2007'); if($isDown){ //网页下载 header('pragma:public'); header("Content-Disposition:attachment;filename=$fileName.xls"); $objWrite->save('php://output');exit; } $_fileName = iconv("utf-8", "gb2312", $fileName); //转码 $_savePath = $savePath.$_fileName.'.xlsx'; $objWrite->save($_savePath); return $savePath.$fileName.'.xlsx'; } //exportExcel(array('姓名','年龄'), array(array('a',21),array('b',23)), '档案', './', true);

2、PHPExcel导入方法实现过程

/** 
*  数据导入 
* @param string $file excel文件 
* @param string $sheet 
 * @return string   返回解析数据 
 * @throws PHPExcel_Exception 
 * @throws PHPExcel_Reader_Exception 
*/  
function importExecl($file='', $sheet=0){  
    $file = iconv("utf-8", "gb2312", $file);   //转码  
    if(empty($file) OR !file_exists($file)) {  
        die('file not exists!');  
    }  
    include('PHPExcel.php');  //引入PHP EXCEL类  
    $objRead = new PHPExcel_Reader_Excel2007();   //建立reader对象  
    if(!$objRead->canRead($file)){  
        $objRead = new PHPExcel_Reader_Excel5();  
        if(!$objRead->canRead($file)){  
            die('No Excel!');  
        }  
    }  
  
    $cellName = 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', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');  
  
    $obj = $objRead->load($file);  //建立excel对象  
    $currSheet = $obj->getSheet($sheet);   //获取指定的sheet表  
    $columnH = $currSheet->getHighestColumn();   //取得最大的列号  
    $columnCnt = array_search($columnH, $cellName);  
    $rowCnt = $currSheet->getHighestRow();   //获取总行数  
  
    $data = array();  
    for($_row=1; $_row<=$rowCnt; $_row++){  //读取内容  
        for($_column=0; $_column<=$columnCnt; $_column++){  
            $cellId = $cellName[$_column].$_row;  
            $cellValue = $currSheet->getCell($cellId)->getValue();  
             //$cellValue = $currSheet->getCell($cellId)->getCalculatedValue();  #获取公式计算的值  
            if($cellValue instanceof PHPExcel_RichText){   //富文本转换字符串  
                $cellValue = $cellValue->__toString();  
            }  
  
            $data[$_row][$cellName[$_column]] = $cellValue;  
        }  
    }  
  
    return $data;  
}  

  在导出excel表时报错,表导出后打不开

关于phpExcel导出excel之后打开不了的问题

情况说明:

我相信很多人用过phpExcel这个类库都会遇到的一个问题就是,导出excel表格文件后(xls或xlsx文件),打开这个文件既然显示这里写图片描述 
然后网上也很多解决办法,主要看大家的情况而选择用哪用方法。

  • 网上主要的问题以及解决的方法: 

    文件带有UTF-8 BOM:这种情况只要把签名给删掉就可以了,将文件改成不带BOM签名。


  • 我就说一下我这次的问题所在,希望大家遇到这种情况时候也可以帮到各位 

    我这次的情况是这样:我文件没有带UTF-8 BOM,而且在本地测试的时候也没有问题,但是放到服务器上就出现导出EXCEL之后打不开的问题。


  • 然后我就用编译器打开了一个不能开打的excel文件:

    <br /> 
    <b>Warning</b>: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in <b>C:\wtf\enroll_beta\lib\phpExcel\Classes\PHPExcel\Writer\Excel2007\DocProps.php</b> on line <b>172</b><br /> 
    <br /> 
    <b>Warning</b>: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone. in <b>C:\wtf\enroll_beta\lib\phpExcel\Classes\PHPExcel\Writer\Excel2007\DocProps.php</b> on line <b>178</b><br /> 
    PK

  • 然而真正能打开的excel文件里面的代码是这样的额:

504b 0304 1400 0000 0800 3cab 0249 4792 44b2 5801 0000 f004 0000 1300 0000 5b43 6f6e 7465 6e74 5f54 7970 6573 5d2e 786d 6cad 944d 4ec3 3010 85f7 9c22 f216 256e 5920 849a 7641 6109 9528 0730 f6a4 b1ea d896 67fa 777b 2669 0b08 8940 d56e 6245 f67b dff8 79ec d164 dbb8 6c0d 096d f0a5 1816 0391 81d7 c158 bf28 c5db fc29 bf13 1992 f246 b9e0 a114 3b40 3119 5f8d e6bb 0898 b1d8 6329 6aa2 782f 25ea 1a1a 8545 88e0 79a6 0aa9 51c4 bf69 21a3 d24b b500 7933 18dc 4a1d 3c81 a79c 5a0f 311e bd30 3f59 03d9 4c25 7a56 0d63 e4d6 4962 37d8 7f87 05fb 89ec 612f 6cd9 a550 313a ab15 71e1 72ed cd0f 6a1e aaca 6a30 41af 1a96 149d cd75 eb22 7f05 22ed 1ce0 d928 8c09 (太多就不全部打印出来了)
    • 我的猜想是因为服务器时间区域跟phpExcel函数库脸面的时间区域选用不一致,然后报了个warming。虽然warming不影响函数的执行,但是php导出excel文件,是header出来的。这个warning一出来,就把warning的信息都导入到excel文件里面了,自然WPS或者office软件就识别不了里面的代码,自然就无法打开不了文件。

    • 我的解决办法:

      既然是因为warning的影响,那我就把它屏蔽掉吧,然后我就函数第一行加了这行代码: 
      error_reporting(0); 
      这行代码的作用就是把warning,error等等的信息不显示出来。 
      最后执行,导出来的excel文件就可以顺利打开了。

猜你喜欢

转载自www.cnblogs.com/yszr/p/9150165.html