本篇内容主要讲解“PHP网页怎么导出和导入excel文件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“PHP网页怎么导出和导入excel文件”吧!
导出和导入表单代码:
<p style="margin:10px 0"><a href="export.php" 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>
excel导出:
$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);
exportExcel方法代码:
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'); }
excel导入:
$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; }
到此,相信大家对“PHP网页怎么导出和导入excel文件”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。