## PHP匯入匯出Excel,CSV## HTML``````### PHP 匯入> function importcsv```/** * [importcsv description] * @return [type] [description] */ public function importcsv() { if (IS_POST) { $filename = $_FILES['file']['tmp_name']; if (empty($filename)) { $this->error("csv匯入檔案請不要為空白"); } $handle = fopen($filename, 'r'); function input_csv($handle) { $out = array(); $n = 0; while ($data = fgetcsv($handle, 10000)) { $num = count($data); for ($i = 0; $i < $num; $i++) { $out[$n][$i] = $data[$i]; } $n++; } return $out; } $result = input_csv($handle); //解析csv //$number = count($result, COUNT_RECURSIVE) - count($result); // $len_result = count($result); $len_result = count($result) - 1; if ($len_result == 0) { $this->error("csv匯入資料為空白"); } foreach ($result as $kr => $vr) { //這裡寫你的商務邏輯 if ($kr) { $i = 0; //中文轉碼 $data['estate_id'] = iconv('gb2312', 'utf-8', $vr[$i++]); //樓盤ID $data['num'] = iconv('gb2312', 'utf-8', $vr[$i++]); //樓棟 $data['unit'] = iconv('gb2312', 'utf-8', $vr[$i++]); //單元 $data['floors'] = iconv('gb2312', 'utf-8', $vr[$i++]); //樓層 $data['number'] = iconv('gb2312', 'utf-8', $vr[$i++]); //門牌號 $data['status'] = iconv('gb2312', 'utf-8', $vr[$i++]); //狀態 $data['type'] = iconv('gb2312', 'utf-8', $vr[$i++]); //關聯戶型 $data['market_money'] = iconv('gb2312', 'utf-8', $vr[$i++]); //市場價 //create_time last_time NOW_TIME $data['create_time'] = iconv('gb2312', 'utf-8', NOW_TIME); //建立時間 $data['last_time'] = iconv('gb2312', 'utf-8', NOW_TIME); //修改時間 $list = D('Property')->add($data); } } if ($list) { $unit_info = D('Unit')->where('id=' . I('unit_id'))->find(); $map['estate_id'] = $unit_info['estate_id']; $map['building'] = $unit_info['building']; $buildid = D('Building')->where($map)->getField('id'); $success = D('Unit')->where('id=' . I('unit_id'))->data(array('status' => 1))->save(); if ($success) { $this->success("csv匯入成功!共匯入" . $len_result . "條資料", U('Admin/Unit/index', array('id' => $buildid))); } } else { $this->error("csv匯入失敗!"); } fclose($handle); //關閉指標 } else { $info = M('Unit')->where('id=' . I('id'))->find(); $buid = D('Property')->bulid_property($info); S('buid' . time() . I('id'), $buid); $protype = D('Estate')->get_project_type($info['estate_id']); $this->assign('unit', $buid); $this->assign('unit_id', I('id')); $this->assign('protype', $protype); $this->assign('estate_id', 'buid' . time() . I('id')); $this->display(); } }```### PHP 匯出> 這裡本來想匯出Excel,先匯出成csv了,具體的大家可以按自己的意願更改> function exportcsv``` /** * [exportcsv description]匯出Excel * @return [type] [description] */ public function exportcsv() { //匯出Excel estate_id num unit $id = I('id'); if (isset($id) && !empty($id)) { $unit_info = D('Unit')->where('id=' . $id)->find(); if ($unit_info) { $map = array(); $map['estate_id'] = $unit_info['estate_id']; //樓盤ID $map['num'] = $unit_info['building']; //樓棟 $map['unit'] = $unit_info['unit']; //單元 $result = M('property')->where($map)->select(); if ($result) { $title = array('序列', '項目#樓盤', '樓棟', '單元', '樓層', '門牌號', '狀態', '關聯戶型', '市場價', '建立時間', '修改時間'); foreach ($result as $k => $v) { $data[$k][] = $v['id']; //序列 $data[$k][] = idtoname($v['estate_id'], 'Estate', 'name', 'id') . '#' . $v['estate_id']; //項目#樓盤 $data[$k][] = $v['num']; //樓棟 $data[$k][] = $v['unit']; //單元 $data[$k][] = $v['floors']; //樓層 $data[$k][] = $v['floors'] . '0' . $v['number']; //門牌號 $data[$k][] = $v['status']; //狀態 $data[$k][] = idtoname($v['type'], 'ProType', 'name', 'id'); //關聯戶型 $data[$k][] = format_price($v['market_money']); //市場價 $data[$k][] = time_format($v['create_time']); //建立時間 $data[$k][] = time_format($v['last_time']); //修改時間 } export_csv_xls($title, $data); unset($title, $data);exit; } } else { $this->error("未查詢到樓盤單元資訊!"); } } else { $this->error("csv匯入失敗!"); } }```> function export_csv_xls```function export_csv_xls($title, $array = '', $code = 'gb2312', $filename = "") { if (!$filename) { // $filename = date("Y-m-d") . ".xls"; $filename = randStr(10) . ".xls"; } header("Content-Type: application/vnd.ms-execl"); header("Content-Type: application/vnd.ms-excel; charset=" . $code); header("Content-Disposition: attachment; filename=$filename"); header("Pragma: no-cache"); header("Expires: 0"); if ($code == "gb2312") { $title = array_map("utf8togb2312", $title); } echo implode("\t", $title) . "\n"; if ($array) { foreach ($array as $v) { if ($code == "gb2312") { $v = array_map("utf8togb2312", $v); } echo implode("\t", $v) . "\n"; } }}```> function utf8togb2312```function utf8togb2312($str) {//編碼轉換 $str = format_xml_num($str); $bianma = mb_detect_encoding($str); if ($bianma == "GB2312") { return $str; } else { return iconv($bianma, 'GB2312', trim($str)); }}```> function format_xml_num```function format_xml_num($str) {//匯出格式化數字 if (strlen((float) $str) >= 12 && is_numeric((float) $str) && (float) $str && !strpos($str, '.')) { $str = "'" . (string) $str; } return $str;}```