PHP导入导出Excel,CSV
HTML
1<form action="{:U('Admin/Unit/importcsv')}" method="post" name="myform" id="myform" enctype="multipart/form-data"> 2 <input type="hidden" name="unit_id" value="{$unit_id}"/> 3 <h3 style="background:#006699; color:#FFFFFF; width:450px;">{$unit.1.1.num}号楼/{$unit.1.1.unit}单元</h3> 4 <p> 5 请选择要导入的CSV文件: 6 <br/> 7 <input type="file" name="file"> 8 <input type="submit" name="dosubmit" id="dosubmit" class="dialog btn btn-primary" value="导入CSV"> 9 <input class="btn btn-default" type="button" onclick="javascript:history.back(-1);" value="{:L('GOBACK')}" > 10 <!-- <input type="button" class="btn" value="导出CSV" onclick="window.location.href='do.php?action=export'"> --> 11 </p> 12</form>
PHP 导入
function importcsv
1/** 2 * [importcsv description] 3 * @return [type] [description] 4 */ 5 public function importcsv() { 6 if (IS_POST) { 7 $filename = $_FILES['file']['tmp_name']; 8 if (empty($filename)) { 9 $this->error("csv导入文件请不要为空"); 10 } 11 $handle = fopen($filename, 'r'); 12 function input_csv($handle) { 13 $out = array(); 14 $n = 0; 15 while ($data = fgetcsv($handle, 10000)) { 16 $num = count($data); 17 for ($i = 0; $i < $num; $i++) { 18 $out[$n][$i] = $data[$i]; 19 } 20 $n++; 21 } 22 return $out; 23 } 24 $result = input_csv($handle); //解析csv 25 //$number = count($result, COUNT_RECURSIVE) - count($result); 26 // $len_result = count($result); 27 $len_result = count($result) - 1; 28 if ($len_result == 0) { 29 $this->error("csv导入数据为空"); 30 } 31 foreach ($result as $kr => $vr) { 32 //这里写你的业务逻辑 33 if ($kr) { 34 $i = 0; 35 //中文转码 36 $data['estate_id'] = iconv('gb2312', 'utf-8', $vr[$i++]); //楼盘ID 37 $data['num'] = iconv('gb2312', 'utf-8', $vr[$i++]); //楼栋 38 $data['unit'] = iconv('gb2312', 'utf-8', $vr[$i++]); //单元 39 $data['floors'] = iconv('gb2312', 'utf-8', $vr[$i++]); //楼层 40 $data['number'] = iconv('gb2312', 'utf-8', $vr[$i++]); //门牌号 41 $data['status'] = iconv('gb2312', 'utf-8', $vr[$i++]); //状态 42 $data['type'] = iconv('gb2312', 'utf-8', $vr[$i++]); //关联户型 43 $data['market_money'] = iconv('gb2312', 'utf-8', $vr[$i++]); //市场价 44 //create_time last_time NOW_TIME 45 $data['create_time'] = iconv('gb2312', 'utf-8', NOW_TIME); //创建时间 46 $data['last_time'] = iconv('gb2312', 'utf-8', NOW_TIME); //修改时间 47 $list = D('Property')->add($data); 48 } 49 } 50 if ($list) { 51 $unit_info = D('Unit')->where('id=' . I('unit_id'))->find(); 52 $map['estate_id'] = $unit_info['estate_id']; 53 $map['building'] = $unit_info['building']; 54 $buildid = D('Building')->where($map)->getField('id'); 55 $success = D('Unit')->where('id=' . I('unit_id'))->data(array('status' => 1))->save(); 56 if ($success) { 57 $this->success("csv导入成功!共导入" . $len_result . "条数据", U('Admin/Unit/index', array('id' => $buildid))); 58 } 59 } else { 60 $this->error("csv导入失败!"); 61 } 62 fclose($handle); //关闭指针 63 } else { 64 $info = M('Unit')->where('id=' . I('id'))->find(); 65 $buid = D('Property')->bulid_property($info); 66 S('buid' . time() . I('id'), $buid); 67 $protype = D('Estate')->get_project_type($info['estate_id']); 68 $this->assign('unit', $buid); 69 $this->assign('unit_id', I('id')); 70 $this->assign('protype', $protype); 71 $this->assign('estate_id', 'buid' . time() . I('id')); 72 $this->display(); 73 } 74 }
PHP 导出
这里本来想导出Excel,先导出成csv了,具体的大家可以按自己的意愿更改
function exportcsv
1 /** 2 * [exportcsv description]导出Excel 3 * @return [type] [description] 4 */ 5 public function exportcsv() { 6 //导出Excel estate_id num unit 7 $id = I('id'); 8 if (isset($id) && !empty($id)) { 9 $unit_info = D('Unit')->where('id=' . $id)->find(); 10 if ($unit_info) { 11 $map = array(); 12 $map['estate_id'] = $unit_info['estate_id']; //楼盘ID 13 $map['num'] = $unit_info['building']; //楼栋 14 $map['unit'] = $unit_info['unit']; //单元 15 $result = M('property')->where($map)->select(); 16 if ($result) { 17 $title = array('序列', '项目#楼盘', '楼栋', '单元', '楼层', '门牌号', '状态', '关联户型', '市场价', '创建时间', '修改时间'); 18 foreach ($result as $k => $v) { 19 $data[$k][] = $v['id']; //序列 20 $data[$k][] = idtoname($v['estate_id'], 'Estate', 'name', 'id') . '#' . $v['estate_id']; //项目#楼盘 21 $data[$k][] = $v['num']; //楼栋 22 $data[$k][] = $v['unit']; //单元 23 $data[$k][] = $v['floors']; //楼层 24 $data[$k][] = $v['floors'] . '0' . $v['number']; //门牌号 25 $data[$k][] = $v['status']; //状态 26 $data[$k][] = idtoname($v['type'], 'ProType', 'name', 'id'); //关联户型 27 $data[$k][] = format_price($v['market_money']); //市场价 28 $data[$k][] = time_format($v['create_time']); //创建时间 29 $data[$k][] = time_format($v['last_time']); //修改时间 30 } 31 export_csv_xls($title, $data); 32 unset($title, $data);exit; 33 } 34 } else { 35 $this->error("未查询到楼盘单元信息!"); 36 } 37 } else { 38 $this->error("csv导入失败!"); 39 } 40 }
function export_csv_xls
1function export_csv_xls($title, $array = '', $code = 'gb2312', $filename = "") { 2 if (!$filename) { 3 // $filename = date("Y-m-d") . ".xls"; 4 $filename = randStr(10) . ".xls"; 5 } 6 header("Content-Type: application/vnd.ms-execl"); 7 header("Content-Type: application/vnd.ms-excel; charset=" . $code); 8 header("Content-Disposition: attachment; filename=$filename"); 9 header("Pragma: no-cache"); 10 header("Expires: 0"); 11 if ($code == "gb2312") { 12 $title = array_map("utf8togb2312", $title); 13 } 14 echo implode("\t", $title) . "\n"; 15 if ($array) { 16 foreach ($array as $v) { 17 if ($code == "gb2312") { 18 $v = array_map("utf8togb2312", $v); 19 } 20 echo implode("\t", $v) . "\n"; 21 } 22 } 23}
function utf8togb2312
1function utf8togb2312($str) { 2//编码转换 3 $str = format_xml_num($str); 4 $bianma = mb_detect_encoding($str); 5 if ($bianma == "GB2312") { 6 return $str; 7 } else { 8 return iconv($bianma, 'GB2312', trim($str)); 9 } 10}
function format_xml_num
1function format_xml_num($str) { 2//导出格式化数字 3 if (strlen((float) $str) >= 12 && is_numeric((float) $str) && (float) $str && !strpos($str, '.')) { 4 $str = "'" . (string) $str; 5 } 6 return $str; 7}