微擎php上传excel并且读取数据;保存到数据库

html

<form action="{$excelurl}" method="post"  enctype="multipart/form-data">
   <br>
    <br><br>

    <table width="100%">
        <tr>
            <td class="tb_lable" style="width: 100px">选择文件</td>
            <td>
                <input type="file" name="force">
            </td>
        </tr>
        <tr>
            <td class="tb_lable" style="width: 100px">num_iid</td>
            <td>
                <input type="number" name="num_iid">
            </td>
        </tr>
        <tr>
            <td class="tb_lable"></td>
            <td>
                <br>  <input id="sub_btn" type="submit" name="submit" value="导入" class="btn btn-primary" style="border-radius:5px;"/>
            </td>
        </tr>
    </table>
</form>

<?php

if ($_POST['submit']) {

# code...

$force = $_GPC['force'];
        $file = $_FILES['force'];
        if( $file['name'] && $file['error'] == 0){
            $type = @end( explode('.', $file['name']));
            $type = strtolower($type);
            if( !in_array($type, array('xls','xlsx')) ){
                message('文件类型错误!',  '', 'error');
            }

            if(!$_POST['num_iid']){
                message('num_iid不能为空!',  '', 'error');
            }
            //开始导入
            set_time_limit(0);
            include_once IA_ROOT . '/framework/library/phpexcel/PHPExcel.php';
            /** PHPExcel_IOFactory */
            include_once IA_ROOT . '/framework/library/phpexcel/PHPExcel/IOFactory.php';
            if( $type == 'xls' ){
                $inputFileType = 'Excel5';    //这个是读 xls的
            }else{
                $inputFileType = 'Excel2007';//这个是计xlsx的
            }
            $objReader = PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($file['tmp_name']);

            $objWorksheet = $objPHPExcel->getActiveSheet();//取得总行数
            $highestRow = $objWorksheet->getHighestRow();//取得总列数
            for ($row = 2;$row <= $highestRow;$row++){
                $code = $objWorksheet->getCellByColumnAndRow(0, $row)->getValue();
              
                pdo_insert('ka_ulie',array( 'num_iid' => $_POST['num_iid'] , 'xulie' => $code, 'status'=>0));
            }
            message('数据添加成功',  $this->createWebUrl('Xulie'));
          }
        message('文件上传失败!',  '', 'error');
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值