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条)

报告相同问题?

悬赏问题

  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥20 关于#stm32#的问题:STM32串口发送问题,偶校验(even),发送5A 41 FB 20.烧录程序后发现串口助手读到的是5A 41 7B A0
  • ¥15 C++map释放不掉
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题
  • ¥15 求用二阶有源低通滤波将3khz方波转为正弦波的电路