PHP使用PHPexcel导入导出数据的方法_php技巧_脚本之家

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

'; $objReader = PHPExcel_IOFactory::createReader; $objPHPExcel = $objReader->load; /* $sheet = $objPHPExcel->getSheet; $highestRow = $sheet->getHighestRow(); //取得总行数 $highestColumn = $sheet->getHighestColumn(); //取得总列 */ $objWorksheet = $objPHPExcel->getActiveSheet();//取得总行数 $highestRow = $objWorksheet->getHighestRow();//取得总列数 echo 'highestRow='.$highestRow; echo "
"; $highestColumn = $objWorksheet->getHighestColumn(); $highestColumnIndex = PHPExcel_Cell::columnIndexFromString;//总列数 echo 'highestColumnIndex='.$highestColumnIndex; echo "
"; $headtitle=array(); for ($row = 1;$row <= $highestRow;$row++) { $strs=array(); //注意highestColumnIndex的列数索引从0开始 for ($col = 0;$col < $highestColumnIndex;$col++) { $strs[$col] =$objWorksheet->getCellByColumnAndRow->getValue(); } $info = array( 'word1'=>"$strs[0]", 'word2'=>"$strs[1]", 'word3'=>"$strs[2]", 'word4'=>"$strs[3]", ); //在这儿,你可以连接,你的数据库,写入数据库了 print_r; echo '
'; }?>

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

private function _export_data{ error_reporting; //开启错误 set_time_limit; //脚本不超时 date_default_timezone_set; //设置时间 /** 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 ->setLastModifiedBy ->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; // 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{ $i = 1; foreach ($data as $key => $value) { $newobj = $objPHPExcel->setActiveSheetIndex; $j = 0; foreach  { $index = $letter[$j]."$i"; $objPHPExcel->setActiveSheetIndex->setCellValue; $j++; } $i++; } } $date = date; // Rename worksheet $objPHPExcel->getActiveSheet; $objPHPExcel->setActiveSheetIndex; // Redirect output to a client's web browser  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; exit;}

public function export_data{ # 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; $obj_phpexcel->getActiveSheet()->setCellValue; if{ $i =2; foreach ($data as $key => $value) { # code... $obj_phpexcel->getActiveSheet()->setCellValue; $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 . " GMT"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header; $obj_Writer->save; }

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

发表评论

电子邮件地址不会被公开。 必填项已用*标注