PHP使用PHPexcel导入导出数据的方法

作者:jackluo 时间:2024-05-13 09:21:10 

本文实例讲述了PHP使用PHPexcel导入导出数据的方法。分享给大家供大家参考,具体如下:

导入数据:


<?php
error_reporting(E_ALL); //开启错误
set_time_limit(0); //脚本不超时
date_default_timezone_set('Europe/London'); //设置时间
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . 'https://www.jb51.net/../Classes/');//设置环境变量
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
//$inputFileType = 'Excel5'; //这个是读 xls的
$inputFileType = 'Excel2007';//这个是计xlsx的
//$inputFileName = './sampleData/example2.xls';
$inputFileName = './sampleData/book.xlsx';
 echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
 $objReader = PHPExcel_IOFactory::createReader($inputFileType);
 $objPHPExcel = $objReader->load($inputFileName);
 /*
 $sheet = $objPHPExcel->getSheet(0);
 $highestRow = $sheet->getHighestRow(); //取得总行数
 $highestColumn = $sheet->getHighestColumn(); //取得总列
 */
 $objWorksheet = $objPHPExcel->getActiveSheet();//取得总行数
 $highestRow = $objWorksheet->getHighestRow();//取得总列数
 echo 'highestRow='.$highestRow;
 echo "<br>";
 $highestColumn = $objWorksheet->getHighestColumn();
 $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
 echo 'highestColumnIndex='.$highestColumnIndex;
 echo "<br />";
 $headtitle=array();
 for ($row = 1;$row <= $highestRow;$row++)
 {
  $strs=array();
  //注意highestColumnIndex的列数索引从0开始
  for ($col = 0;$col < $highestColumnIndex;$col++)
  {
   $strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
  }
   $info = array(
    'word1'=>"$strs[0]",
    'word2'=>"$strs[1]",
    'word3'=>"$strs[2]",
    'word4'=>"$strs[3]",
   );
   //在这儿,你可以连接,你的数据库,写入数据库了
   print_r($info);
   echo '<br />';
 }
?>

导出数据:

(如果有特殊的字符串 = 麻烦str_replace(array('='),'',$val['roleName']);)


private function _export_data($data = array())
{
error_reporting(E_ALL); //开启错误
set_time_limit(0); //脚本不超时
date_default_timezone_set('Europe/London'); //设置时间
/** Include path **/
set_include_path(FCPATH.APPPATH.'/libraries/Classes/');//设置环境变量
// Create new PHPExcel object
Include 'PHPExcel.php';
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
   ->setLastModifiedBy("Maarten Balliauw")
   ->setTitle("Office 2007 XLSX Test Document")
   ->setSubject("Office 2007 XLSX Test Document")
   ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
   ->setKeywords("office 2007 openxml php")
   ->setCategory("Test result file");
// Add some data
$letter = 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');    
if($data){
 $i = 1;
 foreach ($data as $key => $value) {
 $newobj = $objPHPExcel->setActiveSheetIndex(0);
 $j = 0;
 foreach ($value as $k => $val) {
  $index = $letter[$j]."$i";
  $objPHPExcel->setActiveSheetIndex(0)->setCellValue($index, $val);
  $j++;
 }
  $i++;
 }
}  
$date = date('Y-m-d',time());  
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle($date);
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client's web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="'.$date.'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
}

直接上代码:


public function export_data($data = array())
{
# code...
include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel/Writer/IWriter.php') ;
include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel/Writer/Excel5.php') ;
include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel.php') ;
include_once(APP_PATH.'Tools/PHPExcel/Classes/PHPExcel/IOFactory.php') ;
$obj_phpexcel = new PHPExcel();
$obj_phpexcel->getActiveSheet()->setCellValue('a1','Key');
$obj_phpexcel->getActiveSheet()->setCellValue('b1','Value');
if($data){
 $i =2;
 foreach ($data as $key => $value) {
 # code...
 $obj_phpexcel->getActiveSheet()->setCellValue('a'.$i,$value);
 $i++;
 }
}
$obj_Writer = PHPExcel_IOFactory::createWriter($obj_phpexcel,'Excel5');
$filename = "outexcel.xls";
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$obj_Writer->save('php://output');
}

希望本文所述对大家php程序设计有所帮助。

标签:PHP,PHPexcel,导入,导出
0
投稿

猜你喜欢

  • python模拟鼠标拖动操作的方法

    2021-10-02 15:06:07
  • vue使用百度地图报错BMap is not defined问题及解决

    2024-04-26 17:42:02
  • oracle中110个常用函数介绍

    2023-07-09 03:46:20
  • 长期使用中型Access数据库的一点经验

    2007-12-21 13:23:00
  • Python字符串大小写转换拼接删除空白

    2023-01-24 13:08:01
  • 如何建设一个多语言版的ASP网站?

    2009-11-26 20:36:00
  • 添加到各大流行网摘 书签的代码

    2008-04-20 14:15:00
  • python生成验证码图片代码分享

    2021-08-24 11:52:14
  • python全栈开发语法总结

    2021-08-25 06:16:33
  • python编写脚本之pyautogui的安装和使用教程

    2021-06-17 09:48:11
  • SpringBoot用多线程批量导入数据库实现方法

    2024-01-23 23:03:36
  • Git Submodule管理项目子模块的使用

    2023-10-06 20:34:57
  • 利用Pandas读取表格行数据判断是否相同的方法

    2022-07-30 22:10:20
  • 在Python中处理日期和时间的基本知识点整理汇总

    2021-05-13 07:12:14
  • golang gorm 结构体的表字段缺省值设置方式

    2024-04-28 09:13:53
  • 通过MySQL日志实时查看执行语句以及更新日志的教程

    2024-01-17 03:57:10
  • Python3 基础语法详解

    2023-06-24 06:10:55
  • python单例模式之selenium driver实现单例

    2021-09-30 14:31:03
  • mysql 显示SQL语句执行时间的代码

    2024-01-16 03:25:14
  • 序列化Python对象的方法

    2022-07-09 22:51:59
  • asp之家 网络编程 m.aspxhome.com