PHPexcel excel export and import
Export and import form HTML code:
<p style="margin:10px 0"><a href="export.php" class="btn">导出</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="导入" /> </p> </form>
Copy after login
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', '邮箱', '密码'); //设置要导出excel的表头 exportExcel($list, '素材火用户表', $title);
Copy after login
exportExcel method code:
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'); }
Copy after login
PHP excel import
$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,password) values ('" . $v['B'] . "', '" . $v['C'] . "')"; 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; }
Copy after login
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

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article
What's New in Windows 11 KB5054979 & How to Fix Update Issues
4 weeks ago
By DDD
How to fix KB5055523 fails to install in Windows 11?
3 weeks ago
By DDD
InZoi: How To Apply To School And University
1 months ago
By DDD
How to fix KB5055518 fails to install in Windows 10?
3 weeks ago
By DDD
Where to find the Site Office Key in Atomfall
4 weeks ago
By DDD

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)
