前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PhpSpreadsheet读取excel【包含图片】

PhpSpreadsheet读取excel【包含图片】

作者头像
黄啊码
发布2020-05-29 11:16:40
1.5K0
发布2020-05-29 11:16:40
举报

composer require phpoffice/phpspreadsheet=1.8.2

安装 phpspreadsheet

代码语言:javascript
复制
$reader           = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx'); //实例化阅读器对象。
$spreadsheet      = $reader->load($filename);  //将文件读取到到$spreadsheet对象中
$sheet            = $spreadsheet->getSheet(0);//sheet
$highestColumn    = $sheet->getHighestColumn(); // 取得总列数
$highestRow       = $sheet->getHighestRow(); // 取得总行数
代码语言:javascript
复制
      for ($col = 1; $col <= $highestColumnIndex; ++$col) //列数是以A列开始
        {
            $column_name = $sheet->getCellByColumnAndRow($col, 1)->getFormattedValue();
            if(in_array($column_name,array_keys($format_column)))$column[] = ['clo' => $col,'key' => $format_column[$column_name]];
        }
        // 2 根据以上确定的列直接循环取值
        for ($row = 2; $row <= $highestRow; ++$row) //行号从1开始
        {
            $row_data = [];
            foreach ($column as $item){
                switch ($item['key']) {
                    case "tax":
                        $value = $sheet->getCellByColumnAndRow($item['clo'], $row)->getFormattedValue();
                        if(strpos($value, "%") > 0) $value = (float)$value/100;
                        $row_data[$item['key']] = $value;
                        break;
                    case "num" :
                        $row_data[$item['key']] = (int)$sheet->getCellByColumnAndRow($item['clo'], $row)->getCalculatedValue(true);
                        break;
                    case "money":
                        $res  = $sheet->getCellByColumnAndRow($item['clo'], $row)->getFormattedValue();
                        $row_data[$item['key']] =  str_replace(['¥', ','], '', $res);//一般先取出结果,然后自己处理比较方便。
                        break;
                    case "time":
                        $date = $sheet->getCellByColumnAndRow($item['clo'], $row)->getValue();
                        if(!$date){
                            $row_data[$item['key']] = null;
                        }else{
                            $row_data[$item['key']] = gmdate('Y-m-d', ($date - 25569) * 24 * 3600); //gmdate返回UTC的时间
                        }
                        break;
                    default :
                        $row_data[$item['key']]= Common::trim($sheet->getCellByColumnAndRow($item['clo'], $row)->getFormattedValue());
                }
            }
            if(count($row_data) != count($format_column)) throw new ApiException(Code::EXCEL_FORMAT_ERROR);
            //空数据过滤
            $res_data[] = $row_data;
        }
        $this->filterData($res_data);
        return array_values($res_data);

//过滤空数据
    private function filterData(&$row_data)
    {
        foreach ($row_data as $key => $item){
            $is_null = false;
            foreach ($item as $iv) {
                if(!empty($iv)) {
                    $is_null = true;    break;
                }
            }
            if($is_null == false ) unset($row_data[$key]);
        }
    }

这里导入的时候会报错:

simplexml_load_string(): Entity: line 2: parser error : Input is not proper UTF-8...

我看到里边报错的地方在Properties.php,当我输出$propertyData是乱码的,所以对其进行转码

到了这里是没有任何问题的,但当我把写的转码删除居然又成功了,经过反复思考,我觉得是Properties.php这个文件的编码问题,当我用编辑器修改的时候,编码就变回正常的UTF-8了,个人想法是这样的,如果遇到不同看法的可以留言给我,谢谢!

作者:Mark 出处:https://mp.csdn.net/console/editor/html/105730455

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-04-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档