douyu8187 2015-05-29 14:11
浏览 37

如何合并数组中excel文件的所有单元格以及如何使自动完成搜索框工作

I have two problems:

1) I am using PHPExcel for retrieving data from cells, I have simple code two print it out.

<?php

    set_include_path(implode(PATH_SEPARATOR, [
        realpath(__DIR__ . '/Classes'), // assuming Classes is in the same directory as this script
        get_include_path()
    ]));
    require_once dirname(__FILE__) . '/Classes/PHPExcel/IOFactory.php';
    require_once 'PHPExcel.php';

    $file= "./uploads/".$_GET["filename"];

    //if you want to try it you can use this and comment line on the above
// $inputFileName = './sampleData/yourexcelname.xls';
        $inputFileName = ($file);

    //  Read your Excel workbook
    try {
        $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcel = $objReader->load($inputFileName);
    } catch(Exception $e) {
        die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
    }

    //  Get worksheet dimensions
    $sheet = $objPHPExcel->getSheet(0); 
    $highestRow = $sheet->getHighestRow(); 
    $highestColumn = $sheet->getHighestColumn();

    //  Loop through each row of the worksheet in turn


    for ($row = 1; $row <= $highestRow; $row++){ 
        //  Read a row of data into an array
        $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                                        NULL,
                                        TRUE,
                                        FALSE);

        var_dump($rowData); //  Insert row data array into your database of choice here


        echo "-----------------as json encode---------------";
        var_dump(json_encode($rowData));

        }
    ?>
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>jQuery UI Autocomplete - Default functionality</title>
      <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
      <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

      <script>
      $(function() {
        var availableTags =<?php echo json_encode($rowData); ?>;
        $( "#tags" ).autocomplete({
          source: availableTags
        });
      });
      </script>
    </head>
    <body>

    <div class="ui-widget">
      <label for="tags">Tags: </label>
      <input id="tags">
    </div>


    </body>
    </html>

however what I want to do is....

instead of this ....

            $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                                            NULL,
                                            TRUE,
                                            FALSE);

i tried to use this:

$total = $sheet->mergeCells('A' . $row . ':' . $highestColumn . $row,  NULL,
                                    TRUE,
                                    FALSE);
 echo "-----------------as Total---------------";
         var_dump($total);

however, it just echos every detail related with document security and property cache info etc. etc. everything but what I want. how can I merge data in cells?

2) I try to turn the php array into js array so that I can put my array into autocomplete search box (autocomplete jquery ) as I used here:

$(function() {
    var availableTags = $.parseJSON('<?php echo json_encode($rowData); ?>');
    $( "#tags" ).autocomplete({

    source: availableTags
    });
  });

however, my suggested options are not appearing when I put this json_encode convertion. question: how can I make this converted array echo my data?

Useful handy helps will be appreciated.

  • 写回答

1条回答 默认 最新

  • dsfew215211 2015-06-03 08:44
    关注

    as @MarkBaker suggested I found a solution which works pretty fine:

    $kova=array();
    
    for ($row = 1; $row <= $highestRow; $row++)
    { 
        //  Read a row of data into an array
    
        $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                                        NULL,
                                        TRUE,
                                       FALSE);
    
        echo "-----------------as rowData---------------";
    
        var_dump($rowData); //  Insert row data array into your database of choice here
    
          echo "-----------------returning into single array---------------";
    
        array_push($kova,$rowData);
        var_dump($kova);
        $myFlatArray = PHPExcel_Calculation_Functions::flattenArray($kova); 
        echo    "<br>";
    
        echo "----------------- converting into json encode---------------";
        var_dump(json_encode($myFlatArray));
    
    
    
    
    }
    
    ?>
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>jQuery UI Autocomplete - Default functionality</title>
      <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
      <script src="//code.jquery.com/jquery-1.10.2.js"></script>
      <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    
    
      <script>
      $(function() {
        var availableTags = $.parseJSON('<?php echo json_encode($myFlatArray); ?>');
        $( "#tags" ).autocomplete({
    
        source: availableTags
        });
      });
      </script>
    </head>
    <body>
     <br>
     <br>
     <br>
    <div class="ui-widget">
      <label for="tags">Tags: </label>
      <input id="tags">
      <br>
      <br>
      <br>
      <br>
    
    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛