原案例來自http://www.sucaihuo.com/ 有修改
1.目錄結構(文件不用解釋,應該都可以看得懂,直接看代碼)
2.注意數據庫表(需要在connect.php中修改數據庫配置)
創建數據庫表
CREATE TABLE `user` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `username` varchar(15) NOT NULL, `email` varchar(20) DEFAULT NULL, `grade` int(11) DEFAULT NULL, `password` varchar(32) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;
3.代碼
視圖index.php
<div class="demo"> <h2 class="title"><a href="http://www.sucaihuo.com/js/140.html">教程:PHPexcel之excel導出和導入</a></h2> <p>class="btn">導出</a></p> <form action="import.php" method="post" enctype="multipart/form-data"> <div class="control-group"> <label>Excel表格:</label> <input type="file" name="file"/> </div> <div class="control-group"> <input type="submit" value="導入" /> </div> </form> </div>
4.導入(import.php)
<?php include_once('connect.php'); $tmp = $_FILES['file']['tmp_name']; if (empty($tmp)) { echo '請選擇要導入的Excel文件!'; exit; } $save_path = "uploads/"; $filename = $save_path . date('Ymdhis') . ".xls"; //上傳後的文件保存路徑和名稱 if (copy($tmp, $filename)) { require_once 'PHPExcel.class.php'; require_once 'PHPExcel/Reader/Excel5.php'; $PHPReader = new PHPExcel_Reader_Excel5(); //PHPExcel_Reader_Excel2007 PHPExcel_Reader_Excel5 //載入文件 $PHPExcel = $PHPReader->load($filename); //獲取表中的第一個工作表,如果要獲取第二個,把0改為1,依次類推 $currentSheet = $PHPExcel->getSheet(0); //獲取總列數 $allColumn = $currentSheet->getHighestColumn(); //獲取總行數 $allRow = $currentSheet->getHighestRow(); //循環獲取表中的數據,$currentRow表示當前行,從哪行開始讀取數據,索引值從0開始 for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) { //從哪列開始,A表示第一列 for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) { //數據坐標 $address = $currentColumn . $currentRow; //讀取到的數據,保存到數組$arr中 $data[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue(); } } $add_time = date('Y-m-d H:i:s', time()); foreach ($data as $k => $v) { if ($k > 1) { $sql = "insert into user (username,email,password) values ('" . $v['B'] . "', '" . $v['C'] . "', '" . $v['D'] . "')"; mysql_query($sql); } } $sql = "SELECT * FROM user"; $result = mysql_query($sql); $tip = '用戶導入成功' . ',現在' . mysql_num_rows($result) . '條數據了!'; echo "<script>alert('" . $tip . "');history.go(-1);</script>"; exit; }
5.導出(export.php)
<?php /** * PHPEXCEL生成excel文件 * @author:素材火 http://blog.csdn.net/winter13292/article/details/8123755 * @desc 支持任意行列數據生成excel文件,暫未添加單元格樣式和對齊 */ include_once('connect.php'); //$list = array( // array('id' => '1', 'email' => "[email protected]", 'password' => 'sucaihuo.com'), // array('id' => '2', 'email' => '[email protected]', 'password' => 'hjl666666'), // array('id' => '3', 'email' => '[email protected]', 'password' => 'zhangqirui'), //); $query = mysql_query("select * from user limit 50"); $i =0; $list = array(); while($row=mysql_fetch_array($query)){ $list[$i]['id'] = $row['id']; $list[$i]['username'] = $row['username']; $list[$i]['email'] = $row['email']; $list[$i]['password'] = $row['password']; $i++; } $title = array('ID', '姓名','郵箱','密碼'); //設置要導出excel的表頭 exportExcel($list, '名單表', $title); function exportExcel($data, $savefile = null, $title = null, $sheetname = 'sheet1') { require_once 'PHPExcel.class.php'; //若沒有指定文件名則為當前時間戳 if (is_null($savefile)) { $savefile = time(); } //若指字了excel表頭,則把表單追加到正文內容前面去 if (is_array($title)) { array_unshift($data, $title); } $objPHPExcel = new PHPExcel(); //Excel內容 $head_num = count($data); foreach ($data as $k => $v) { $obj = $objPHPExcel->setActiveSheetIndex(0); $row = $k + 1; //行 $nn = 0; foreach ($v as $vv) { $col = chr(65 + $nn); //列 $obj->setCellValue($col . $row, $vv); //列,行,值 $nn++; } } //設置列頭標題 for ($i = 0; $i < $head_num - 1; $i++) { $alpha = chr(65 + $i); $objPHPExcel->getActiveSheet()->getColumnDimension($alpha)->setAutoSize(true); //單元寬度自適應 $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->setName("Candara"); //設置字體 $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->setSize(12); //設置大小 $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK); //設置顏色 $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //水平居中 $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //垂直居中 $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->setBold(true); //加粗 } $objPHPExcel->getActiveSheet()->setTitle($sheetname); //題目 $objPHPExcel->setActiveSheetIndex(0); //設置當前的sheet header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $savefile . '.xls"');//文件名稱 header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //Excel5 $objWriter->save('php://output'); } ?>
6.其他文件
源碼地址:http://share.weiyun.com/590da4cea5e53e679fe52e013ab38f6e (密碼:oJzV)
有問題,請聯系QQ1727728211