PHP使用PHPEXCEL导入Excel

2020-12-13 02:27

阅读:474

标签:style   blog   class   code   java   color   

下载PHPEXCEL 后放到项目里。

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public function InExcel() {
    //设定缓存模式为经gzip压缩后存入cache(PHPExcel导入导出及大量数据导入缓存方式的修改 )
    $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip;
    $cacheSettings = array();
    PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
 
    $objPHPExcel = new PHPExcel();
    //读入上传文件
    if ($_POST) {
        $objPHPExcel = PHPExcel_IOFactory::load($_FILES["inputExcel"]["tmp_name"]);
        //内容转换为数组
        $indata = $objPHPExcel->getSheet(0)->toArray();
        //print_r($indata);
        //excel  sheet个数
        //echo $objPHPExcel->getSheetCount();
        //把数据新增到mysql数据库中
        $arr = array();
        $j = 0;
        $model = D(‘goods_unit‘);
        //根据具体情况修改
        if (count($indata) == 1) {
            //根据具体情况修改
            $arr[0] = array("hang" => 0, "Cause" => "无记录!");
            $this->assign(‘errorss‘, $arr);
            $list = $this->menu();
            $this->assign(‘menu‘, $list);
            $this->display(‘Base:baseunit‘, ‘utf-8‘);
        } else {
            if ($indata[0][0] == "单位名称") {
                for ($i = 1; $i count($indata); $i++) {
                    $all[‘UnitName‘] = $indata[$i][0];
                    $w = $model->where($all)->select();
                    if ($w !== null) {
                        $arr[$j] = array("hang" => $i + 1, "Cause" => "该单位已存在");
                        $j++;
                        continue;
                    }
                    $result = $model->add($all);
                    if (false == $result) {
                        $arr[$j] = array("hang" => $i + 1, "Cause" => "写入单位表时失败");
                        $j++;
                        continue;
                    }
                }
                if ($arr == null) {
                    $arr[0] = array("hang" => 0, "Cause" => "导入Excel成功");
                }
                $this->assign(‘errorss‘, $arr);
                $list = $this->menu();
                $this->assign(‘menu‘, $list);
                $this->display(‘Base:baseunit‘, ‘utf-8‘);
            } else {
                $arr[0] = array("hang" => 0, "Cause" => "上传文件格式不正确!");
                $this->assign(‘errorss‘, $arr);
                $list = $this->menu();
                $this->assign(‘menu‘, $list);
                $this->display(‘Base:baseunit‘, ‘utf-8‘);
            }
        }
    } else {
        $arr[0] = array("hang" => 0, "Cause" => "服务器未检测到有上传文件!");
        $this->assign(‘errorss‘, $arr);
        $list = $this->menu();
        $this->assign(‘menu‘, $list);
        $this->display(‘Base:baseunit‘, ‘utf-8‘);
    }
}

 

  以下是前台代码

选择上传文件

  用ajax不知道怎么做。

 

PHP使用PHPEXCEL导入Excel,搜素材,soscw.com

PHP使用PHPEXCEL导入Excel

标签:style   blog   class   code   java   color   

原文地址:http://www.cnblogs.com/aroundight/p/3718450.html


评论


亲,登录后才可以留言!