php利用PHPExcel类导出导入Excel用法
PHPExcel类是php一个excel表格处理插件了,下面我来给大家介绍利用PHPExcel类来导入与导出excel表格的应用方法,有需要了解的朋友不防参考参考,PHPExcel自己百度下载这里不介绍了.
导出Excel用法,代码如下:
-
-
- set_include_path('.'. PATH_SEPARATOR . Yii::app()->basePath.'/lib/PHPExcel' . PATH_SEPARATOR .
-
- get_include_path());
-
-
-
- require_once "PHPExcel.php";
- require_once 'PHPExcel/IOFactory.php';
- require_once 'PHPExcel/Writer/Excel5.php';
-
- ,
-
- $resultPHPExcel = new PHPExcel();
-
-
-
- $resultPHPExcel->getActiveSheet()->setCellValue('A1', '季度');
- $resultPHPExcel->getActiveSheet()->setCellValue('B1', '名称');
- $resultPHPExcel->getActiveSheet()->setCellValue('C1', '数量');
- $i = 2;
- foreach($data as $item){
- $resultPHPExcel->getActiveSheet()->setCellValue('A' . $i, $item['quarter']);
- $resultPHPExcel->getActiveSheet()->setCellValue('B' . $i, $item['name']);
- $resultPHPExcel->getActiveSheet()->setCellValue('C' . $i, $item['number']);
- $i ++;
- }
设置导出参数,代码如下:
-
-
- $outputFileName = 'total.xls';
-
- $xlsWriter = new PHPExcel_Writer_Excel5($resultPHPExcel);
-
-
-
- header("Content-Type: application/force-download");
-
- header("Content-Type: application/octet-stream");
-
- header("Content-Type: application/download");
-
- header('Content-Disposition:inline;filename="'.$outputFileName.'"');
-
- header("Content-Transfer-Encoding: binary");
-
- header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
-
- 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");
-
- $xlsWriter->save( "php://output" );
输出有错,默认$xlsWriter->save( "php://output" );可能因为缓存不够大,而显示不完整,所以做个中转,代码如下:
- $finalFileName = (Yii::app()->basePath.'/runtime/'.time().'.xls';
-
- $xlsWriter->save($finalFileName);
-
- echo file_get_contents($finalFileName);
-
-
导入Excel用法,代码如下:
- <?php
- if($_POST['leadExcel'] == "true")
- {
- $filename = $_FILES['inputExcel']['name'];
- $tmp_name = $_FILES['inputExcel']['tmp_name'];
- $msg = uploadFile($filename,$tmp_name);
- echo $msg;
- }
-
-
- function uploadFile($file,$filetempname)
- {
-
- $filePath = 'upFile/';
- $str = "";
-
- require_once '../PHPExcel/PHPExcel.php';
- require_once '../PHPExcel/PHPExcel/IOFactory.php';
- require_once '../PHPExcel/PHPExcel/Reader/Excel5.php';
-
-
- $time=date("y-m-d-H-i-s");
-
- $extend=strrchr ($file,'.');
-
- $name=$time.$extend;
- $uploadfile=$filePath.$name;
-
- $result=move_uploaded_file($filetempname,$uploadfile);
-
- if($result)
- {
- include "conn.php";
- $objReader = PHPExcel_IOFactory::createReader('Excel5');
- $objPHPExcel = $objReader->load($uploadfile);
- $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();
-
- for ($col = 0;$col < $highestColumnIndex;$col++)
- {
- $strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
- }
- $sql = "INSERT INTO te(`1`, `2`, `3`, `4`, `5`) VALUES (
- '{$strs[0]}',
- '{$strs[1]}',
- '{$strs[2]}',
- '{$strs[3]}',
- '{$strs[4]}')";
-
- if(!mysql_query($sql))
- {
- return false;
- echo 'sql语句有误';
- }
- }
- }
- else
- {
- $msg = "导入失败!";
- }
- return $msg;
- }
- ?>
HTML网页代码,代码如下:
- <form action="upload.php" method="post" enctype="multipart/form-data">
- <input type="hidden" name="leadExcel" value="true">
- <table align="center" width="90%" border="0">
- <tr>
- <td>
- <input type="file" name="inputExcel"><input type="submit" value="导入数据">
- </td>
- </tr>
- </table>
- </form>