Export and import form HTML code:
<p style="margin:10px 0"><a href="export.php" class="btn">導(dǎo)出</a></p> <form action="import.php" method="post" enctype="multipart/form-data"> <p class="control-group"> <label>Excel表格:</label> <input type="file" name="file"/> </p> <p class="control-group"> <input type="submit" value="導(dǎo)入" /> </p> </form>
PHP excel export
$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]['password'] = $row['password']; $i++; } $title = array('ID', '郵箱', '密碼'); //設(shè)置要導(dǎo)出excel的表頭 exportExcel($list, '素材火用戶表', $title);
exportExcel method code:
function exportExcel($data, $savefile = null, $title = null, $sheetname = 'sheet1') { require_once 'PHPExcel.class.php'; //若沒有指定文件名則為當前時間戳 if (is_null($savefile)) { $savefile = time(); } //若指字了excel表頭,則把表單追加到正文內(nèi)容前面去 if (is_array($title)) { array_unshift($data, $title); } $objPHPExcel = new PHPExcel(); //Excel內(nèi)容 $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++; } } //設(shè)置列頭標題 for ($i = 0; $i < $head_num - 1; $i++) { $alpha = chr(65 + $i); $objPHPExcel->getActiveSheet()->getColumnDimension($alpha)->setAutoSize(true); //單元寬度自適應(yīng) $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->setName("Candara"); //設(shè)置字體 $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->setSize(12); //設(shè)置大小 $objPHPExcel->getActiveSheet()->getStyle($alpha . '1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK); //設(shè)置顏色 $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); //設(shè)置當前的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'); }
PHP excel import
$tmp = $_FILES['file']['tmp_name']; if (empty($tmp)) { echo '請選擇要導(dǎo)入的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); //獲取總列數(shù) $allColumn = $currentSheet->getHighestColumn(); //獲取總行數(shù) $allRow = $currentSheet->getHighestRow(); //循環(huán)獲取表中的數(shù)據(jù),$currentRow表示當前行,從哪行開始讀取數(shù)據(jù),索引值從0開始 for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) { //從哪列開始,A表示第一列 for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) { //數(shù)據(jù)坐標 $address = $currentColumn . $currentRow; //讀取到的數(shù)據(jù),保存到數(shù)組$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,password) values ('" . $v['B'] . "', '" . $v['C'] . "')"; mysql_query($sql); } } $sql = "SELECT * FROM user"; $result = mysql_query($sql); $tip = '用戶導(dǎo)入成功' . ',現(xiàn)在' . mysql_num_rows($result) . '條數(shù)據(jù)了!'; echo "<script>alert('" . $tip . "');history.go(-1);</script>"; exit; }
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
Grass Wonder Build Guide | Uma Musume Pretty Derby
1 months ago
By Jack chen
Roblox: 99 Nights In The Forest - All Badges And How To Unlock Them
4 weeks ago
By DDD
Uma Musume Pretty Derby Banner Schedule (July 2025)
1 months ago
By Jack chen
RimWorld Odyssey Temperature Guide for Ships and Gravtech
3 weeks ago
By Jack chen
Windows Security is blank or not showing options
1 months ago
By 下次還敢

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)