原生PHP網頁匯出和匯入excel檔案例項
原生PHP實現的網頁匯出和匯入excel檔案例項,包括上傳也是用的原生。還可在exportExcel方法裡設定字型等表格樣式。
匯出和匯入表單程式碼:
<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; }
本文轉自:
轉載請註明出處!
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31545264/viewspace-2665660/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 使用PHP原生匯出Excel和CSV檔案PHPExcel
- php讀取excel檔案資料的匯入和匯出PHPExcel
- 如何使用JavaScript匯入和匯出Excel檔案JavaScriptExcel
- kxcel, 方便匯入和匯出 ExcelExcel
- PHP 匯出 ExcelPHPExcel
- PHP匯出EXCELPHPExcel
- php 匯出excelPHPExcel
- 【匯入匯出】sqlldr 匯入案例SQL
- NPOI匯出和匯入Excel,Word和PDFExcel
- ASP.NET Excel匯入和匯出ASP.NETExcel
- 如何使用 JavaScript 匯入和匯出 ExcelJavaScriptExcel
- EasyExcel完成excel檔案的匯入匯出Excel
- vue excel匯入匯出VueExcel
- 前端實現Excel匯入和匯出功能前端Excel
- (轉)excel和sql server的匯入匯出ExcelSQLServer
- Angular Excel 匯入與匯出AngularExcel
- SQL SERVER 和EXCEL的資料匯入匯出SQLServerExcel
- vue + element + 匯入、匯出excel表格VueExcel
- 轉java操作excel匯入匯出JavaExcel
- php匯出資料excelPHPExcel
- 匯入excel檔案Excel
- excel匯出、mysql分頁ExcelMySql
- ASP.NET EXCEL資料的匯出和匯入ASP.NETExcel
- ThinkPHP6.0 匯出 Excel 案例PHPExcel
- Vue框架下實現匯入匯出Excel、匯出PDFVue框架Excel
- PHP匯出報表(案例)PHP
- Vue + Element 實現匯入匯出ExcelVueExcel
- EasyPoi, Excel資料的匯入匯出Excel
- java 匯入匯出Excel工具類ExcelUtilJavaExcel
- DevExpress GridView匯入匯出ExceldevExpressViewExcel
- Navicat如何匯入和匯出sql檔案SQL
- MATLAB匯入txt和excel檔案技巧彙總:批量匯入、單個匯入MatlabExcel
- PHP 匯出 Excel 的優化PHPExcel優化
- java匯出Excel檔案JavaExcel
- poi--excel --匯出例項Excel
- Java 通過Xml匯出Excel檔案,Java Excel 匯出工具類,Java匯出Excel工具類JavaXMLExcel
- excel的匯入與匯出---通用版Excel
- Java之POI操作Excel表-匯入匯出JavaExcel