PHP導入導出EXCELl,CSV

PHP導入導出Excel,CSV

HTML

<form action="{:U('Admin/Unit/importcsv')}" method="post" name="myform" id="myform" enctype="multipart/form-data">
    <input type="hidden" name="unit_id" value="{$unit_id}"/>
    <h3 style="background:#006699; color:#FFFFFF; width:450px;">{$unit.1.1.num}號樓/{$unit.1.1.unit}單元</h3>
    <p>
        請選擇要導入的CSV文件:
        <br/>
        <input type="file" name="file">
        <input type="submit" name="dosubmit" id="dosubmit" class="dialog btn btn-primary" value="導入CSV">
        <input class="btn btn-default" type="button" onclick="javascript:history.back(-1);" value="{:L('GOBACK')}" >
        <!-- <input type="button" class="btn" value="導出CSV" onclick="window.location.href='do.php?action=export'"> -->
    </p>
</form>

PHP 導入

function importcsvjavascript

/**
     * [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了,具體的你們能夠按本身的意願更改php

function exportcsvjava

/**
     * [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_xlsapp

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 utf8togb2312post

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_numui

function format_xml_num($str) {
//導出格式化數字
    if (strlen((float) $str) >= 12 && is_numeric((float) $str) && (float) $str && !strpos($str, '.')) {
        $str = "'" . (string) $str;
    }
    return $str;
}
相關文章
相關標籤/搜索