获取最新的Excel文件并使用PHP Excel Reader转换为HTML



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

我有一个工作正常的 Excel 阅读器,可以将我的 Excel 转换为 HTML,并且我有一个代码可以检测目录中的最新文件

我的问题是,我应该放什么代码来显示或使用 PHP Excel 阅读器在目录中获取最新的 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 "tt<td>$cell</td>n";  
        $y++;
      }  
      echo "t</tr>n";
      $x++;
    }
    ?>    
    </table><br/>

  </body>
</html>

您将从getLatestReport()返回一个数组,因此请使用Excel阅读器列表中的第一个文件。 如果您可以返回唯一必需的文件名表单getLatestReport()则只需使用$excel->read($lastest->getLatestReport('battery-report.xls'));

试试这个

<table>
<?php
$latestFiles=$lastest->getLatestReport('battery-report.xls');
$excel->read($latestFiles[0]);    
$x=1;

最新更新