php-excel-reader - UTF-8的问题 [英] php-excel-reader - problem with UTF-8

查看:79
本文介绍了php-excel-reader - UTF-8的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 php-excel-reader 2.21转换XLS文件到CSV我写了一个简单的脚本来做到这一点,但是我有unicode字符的一些问题。它不返回某些单元格的值。

I'm using php-excel-reader 2.21 for converting XLS file to CSV. I wrote a simple script to do that, but I have some problems with unicode characters. It does not return values from some cells.

例如,它没有单元格内容的问题ceníkpoložek但是在nákupVÝROBCEPÁSHRUBÝNÁKLADNÍ等等。在这些单元格中,它返回空值()。

For example it doesn't have problems with cell content ceník položek but have problems with nákup, VÝROBCE, PÁS, HRUBÝ,NÁKLADNÍ and some others. In these cells it returns empty value ("").

这是我用于转换的代码片段:

Here is the code snippet I use for conversion:

<?php    
set_time_limit(120);    
require_once 'excel_reader2.php';    
$data = new Spreadsheet_Excel_Reader("cenik.xls", false, 'UTF-8');    

$f = fopen('file.csv', 'w');    
for($row = 1; $row <= $data->rowcount(); $row++)    
{    
    $out = '';    
    for($col = 1; $col <= $data->colcount(); $col++)    
    {    
        $val = $data->val($row,$col);

        // escape " and \ characters inside the cell    
        $escaped = preg_replace(array('#"#u', '#\\\\#u', '#[""]#u'), array('"', '\\\\\\\\', '\"'), $val);    
        if(empty($val))    
            $out .= ',';    
        else    
            $out .= '"' . $escaped . '",';    
    }
    // remove last comma (,)    
    fwrite($f, substr($out, 0, -1));    
    fwrite($f, "\n");
}
fclose($f);

?>

请注意,单元格和行索引从1开始。任何建议?

Note that the cell and row indexes starts from 1. Any suggestions?

推荐答案

我希望与我有同样的问题:
在第1120行的excel_reader2.php中,替换

I hope it's the same problem as I had: In excel_reader2.php on line 1120, replace

$retstr = ($asciiEncoding) ? $retstr : $this->_encodeUTF16($retstr);

$retstr = ($asciiEncoding) ? iconv('cp1250', 'utf-8', $retstr) : $this->_encodeUTF16($retstr);

应该修复它,但是我建议您使用其他excel阅读器,例如 PHPExcel ,以避免这些问题。

请注意,您需要 iconv 在服务器上启用扩展。

That should fix it, however I suggest you use a different excel reader, such as PHPExcel to avoid problems like these.
Note that you need iconv extension enabled on the server.

这篇关于php-excel-reader - UTF-8的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆