如何在PHP中将Excel文件转换为文本文件?

时间:2017-06-07 11:02:07

标签: php excel file-conversion

我想将excel文件转换为文本文件。我已经尝试过这段代码,但它不起作用:

    $destination = 'products.xls';
    $section = file_get_contents($destination);
    $File = "file.txt"; 
    $File = str_replace(';','#',$File); 
    $Handle = fopen($File, 'w');
    $Data = $section; 
    fwrite($Handle, $Data);
    print "Data Written"; die;

当我打开文本文件时,它会显示特殊字符,当我将csv转换为文本时,它会显示正确的结果。请提供任何有用的链接。

2 个答案:

答案 0 :(得分:0)

您无法以这种方式阅读Excel文件。 Excel文件是遵循XLS格式的二进制文件,而file_get_contents是纯文本文件。您需要一个库(如https://github.com/PHPOffice/PHPExcel)来阅读内容然后写入文本。

答案 1 :(得分:-1)

您不能直接将xls转换为文本;你必须使用像PHPExcel这样的库。

有关实施,请参阅此博客文章:

http://thnghppnsfrreason.blogspot.in/2017/06/phpexcel.html