drus39136
drus39136
2013-09-12 08:21

获取最新的excel文件并使用php excel reader转换为html

已采纳

Can you help me with this? i don't know what to put or what to start to solve this.

i have an excel reader which is working fine, converting my excel into html and i have a code that detect the latest file in the directory

my problem is, what code should i put to display or get the latest excel file in the directory using php excel reader

here's the code that i have. Please help me

    <?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() . "
";
            }

        }

        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>
";
      $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>
";  
        $y++;
      }  
      echo "\t</tr>
";
      $x++;
    }
    ?>    
    </table><br/>


  </body>
</html>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doulu7258 doulu7258 8年前

    you are returning an array from getLatestReport() so use the first file in the list for your excel reader. if you can return the only required filename form getLatestReport() you can simply use $excel->read($lastest->getLatestReport('battery-report.xls'));

    try this

    <table>
    <?php
    $latestFiles=$lastest->getLatestReport('battery-report.xls');
    $excel->read($latestFiles[0]);    
    $x=1;
    
    点赞 评论 复制链接分享