douba4824 2012-01-18 07:57
浏览 162
已采纳

从SpreadsheetExcelReader获取最终输出数组中包含的空白列值

I am using the below plugin for reading micrsoft excel file (.xls extension)

http://php-spreadsheetreader.googlecode.com/svn-history/r26/Excel/OLERead.php

As per the current plugin implementation it returns the read excel as an array.

The below code reads the excels and returns it as an array

        include_once('spreadsheet_excel_reader.php');
        $data = new Spreadsheet_Excel_Reader_New();
        $data->setOutputEncoding('CP1251');
        $data->read('full path of file');
        echo '<pre>';print_r($data->sheets[0]['cells']);echo '</pre>';exit;

The array cells contains the number of values equal to number of rows in an excel sheet.

Each of the value itself is an array consisting of number of keys equal to number of columns in the sheet.

The array cells looks as below -

    [cells] => Array( 
                              [1] => Array
                                    (
                                        [1] => a
                                        [2] => b
                                        [3] => c
                                        [4] => d
                                        [5] => e
                                        [6] => f
                                        [7] => g
                                    )

                                [2] => Array
                                    (
                                        [1] => fddfg
                                        [2] => dfgd
                                        [5] => ghjgh 
                                        [6] => dgdf
                                        [7] => uyijkgh
                                    )

                                [3] => Array
                                    (
                                        [1] => fghfg
                                        [2] => gvsfdgdf
                                        [4] => fdg4t4  
                                        [5] => gfdg
                                        [6] => dfgd
                                        [7] => ghfghf
                                    )

                                [4] => Array
                                    (
                                        [1] => fgh
                                        [2] => sfsdf
                                        [5] => fghfgh
                                        [6] => fsdf
                                        [7] => dfgdfg
                                    ) 
)

As seen from the above array the key 1 contains array of headings of the sheet. So in all there are 7 columns in an excel sheet. Currently if any of the cell contains no value in sheet then it skips those values in an array but my requirement is that if a cell contains no values in that case the array should be containing blank values.

In plugin this whole array creation process takes place in below function

function _parsesheet($spos)

I tries adding a new case for handling null entries but with no success.

Any help on the above issue would be great.

  • 写回答

2条回答 默认 最新

  • dpkrh2444 2012-01-18 09:59
    关注
    $max = 0;
    array_walk($data->sheets[0]['cells'],function($row) use (&$max) { $max = max(max(array_keys($row)),$max); });
    array_walk($data->sheets[0]['cells'],function(&$row) use ($max) { $row = $row + array_fill_keys(range(1,$max),NULL); ksort($row); });
    
    var_dump($data->sheets[0]['cells']);
    

    Pads every missing column with a NULL cell Doesn't do anything about missing rows. PHP >= 5.3.0

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条