【问题标题】:XLS (All sheets) TO CSV With PHPXLS(所有工作表)到 CSV 与 PHP
【发布时间】:2012-12-08 16:32:05
【问题描述】:

以下是我的 php 代码,它正在读取 .xls 文件并将其转换为 .csv。 脚本工作得很好,但我面临的问题是它只适用于默认工作表。我一直在尝试做的是,应该修改以下脚本,使其从 .xls 文件中存在的所有工作表中读取数据,然后将输出保存到 data.csv。请让我知道我该怎么做。

谢谢,

我正在使用以下库:

http://sourceforge.net/projects/phpexcelreader/

  require_once 'lib/reader.php';
    $excel = new Spreadsheet_Excel_Reader();
    $excel->setOutputEncoding('CP1251');
    $excel->read('Student.xls');
    $x=1;
    $sep = ",";
    ob_start();
    while($x<=$excel->sheets[0]['numRows']) {
     $y=1;
     $row="";
     while($y<=$excel->sheets[0]['numCols']) {
         $cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';
         $row.=($row=="")?"\"".$cell."\"":"".$sep."\"".$cell."\"";
         $y++;
     } 
     echo $row."\n"; 
     $x++;
    }
    $fp = fopen("data.csv",'a');
    fwrite($fp,ob_get_contents());
    fclose($fp);
    ob_end_clean();
echo "CSV file created successfully";

【问题讨论】:

    标签: php csv xls


    【解决方案1】:

    您的代码使用$excel-&gt;sheets[0] 访问 Excel 文件中的第一个工作表,因此您需要为每个工作表修改这些访问权限。像这样的:

    $nbSheets = count($excel->sheets);
    for($i = 0; $i < $nbSheets; $i++) {
        while($x<=$excel->sheets[$i]['numRows']) {
            $y=1;
            $row="";
            while($y<=$excel->sheets[$i]['numCols']) {
                $cell = isset($excel->sheets[$i]['cells'][$x][$y]) ? $excel->sheets[$i]['cells'][$x][$y] : '';
                $row.=($row=="")?"\"".$cell."\"":"".$sep."\"".$cell."\"";
                $y++;
            } 
            echo $row."\n"; 
            $x++;
        }
    }
    

    【讨论】:

    • 谢谢所有我想得到张数,你的解决方案做到了非常感谢..说真的,我一直在谷歌上搜索这个..
    猜你喜欢
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    相关资源
    最近更新 更多