0

你能帮我解决这个问题吗?我不知道该放什么或开始解决这个问题。

我有一个运行良好的 excel 阅读器,可以将我的 excel 转换为 html,并且我有一个代码可以检测目录中的最新文件

我的问题是,我应该使用 php excel reader 在目录中显示或获取最新的 excel 文件的代码

这是我拥有的代码。请帮我

    <?php

class ReportViewer
{

    public $extension = array ( 'xlsx', 'xls', 'html', 'htm', 'csv' );

    public function getLatestReport($report)
    {
        $reports_directory = preg_split("/[\-]/", $report);

        //$latest_files = array();

        if (!ctype_alpha($report))
            $directory = $reports_directory[0].'/'.$reports_directory[1];
        else
            $directory = $reports_directory[0];

        $dir_contents = new RecursiveDirectoryIterator($directory);

        foreach (new RecursiveIteratorIterator($dir_contents) as $filename => $file)
        {
            if (preg_match("/\.(" . implode("|", $this->extension) . ")*$/i", $file->getFileName()/*, $filename*/))
            {
                $latest_files[$file->getMTime()] = array($directory, $file->getFileName(), $file->getPath());
                //echo $file->getFileName() . "\n";
            }

        }

        krsort($latest_files);

        //print_r($latest_files);
       // print_r($timeMod);

        return $latest_files;
    }

}


?>

<html>
  <head>
    <style type="text/css">
    table {
        border-collapse: collapse;
    }        
    td {
        border: 1px solid black;
        padding: 0 0.5em;
    }        
    </style>
  </head>
  <body>
    <?php
    include 'reader.php';
    $excel = new Spreadsheet_Excel_Reader();
    $lastest = new ReportViewer();
    ?>
    Sheet 1:<br/><br/>
    <table>
    <?php

    $excel->read('battery-report.xls');    
    $x=1;
    while($x<=$excel->sheets[0]['numRows']) {
      echo "\t<tr>\n";
      $y=1;
      while($y<=$excel->sheets[0]['numCols']) {
        $cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';
        echo "\t\t<td>$cell</td>\n";  
        $y++;
      }  
      echo "\t</tr>\n";
      $x++;
    }
    ?>    
    </table><br/>


  </body>
</html>
4

1 回答 1

0

您正在返回一个数组,getLatestReport()因此请为您的 Excel 阅读器使用列表中的第一个文件。如果您可以返回唯一需要的文件名形式getLatestReport(),您可以简单地使用$excel->read($lastest->getLatestReport('battery-report.xls'));

尝试这个

<table>
<?php
$latestFiles=$lastest->getLatestReport('battery-report.xls');
$excel->read($latestFiles[0]);    
$x=1;
于 2013-09-12T08:30:02.827 回答