Problem special characters excel - html


Good morning. I'm passing the contents of an excel to a table, by showing me the table data the special characters appear with a . The excel is in xls format. PHP code

    require_once ("Excel/reader.php");  
    $datos = new Spreadsheet_Excel_Reader();  

    $celdas = $datos->sheets[0]['cells']; 

    $i = 1;  
    echo "<table width='300' align='center' border=1>";
        echo "<tr><td width='150' align='center'>".$celdas[$i][1]."</td>
        <td width='150' align='center'>".$celdas[$i][2]."</td>
        <td width='150' align='center'>".$celdas[$i][3]."</td>
        <td width='150' align='center'>".$celdas[$i][4]."</td>
        <td width='150' align='center'>".$celdas[$i][5]."</td>
        <td width='150' align='center'>".$celdas[$i][6]."</td>
        <td width='150' align='center'>".$celdas[$i][7]."</td>
        <td width='150' align='center'>".$celdas[$i][8]."</td></tr> ";
    echo "</table>";

In the headers I used <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> . I have also used the function mb_internal_encoding("UTF-8");

asked by Borja Calvo 17.06.2017 в 10:25

2 answers


We go by levels:

  • The document. Make sure that the HTML encoding is effectively UTF-8. To do this, check the HTML source code where you want to present the data. You can do it by activating the browser's development options and going to the menu Desarrollo / Ver código fuente or something similar. You must check the attribute meta of the HTML and verify that it is not repeated below, it often happens that by mistake there are several metadata, with the last one prevailing in that case.
  • If that's okay. We move to a second level of revision: the handling of data . The default encoding of the PHPExcelReader library is UTF-8. However, there are reports of problems regarding the search on their Github page . For example:

    In version 2.11, the line 771 you decide whether or not a string is   ascii       encoded or not. As it seems this is not reliably working, for only UTF16LE       strings are "decoded" to the requested defaultEncoding.

    My workaround/hack for now is to replace line 771 with this:
    $retstr = ($asciiEncoding) ? iconv('cp1250', $this->_defaultEncoding,
    $retstr) : $this->_encodeUTF16($retstr);
    I'm not fully convinced using an hardcoded encoding of cp1250 is a good
    idea but it seems to work in my testcase.

    What it says is that line 771 has changed because of this:

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

    Note: This report is from 2015 and corresponds to version 2.11 of the library. I do not know if it's the version you're using.

    In SO in English there are several answers with those changes of values in lines but they vary according to the version of the library as normal.

  • Verify the encoding of the data that generates the Excel file . According to the comments, the data may be generated with another encoding. It usually occurs, for example, in Excel files generated in Mac OSx. In that case, a possible solution would be to generate it in Google Sheets or Excel Windows.

  • Change library . If the encoding is well established everywhere and still has a problem, PHPExcel may be a better option.

  • answered by 17.06.2017 / 12:36

    The problem may arise because you are defining the output format, but not the read format. See if this helps you:

    $data = new Spreadsheet_Excel_Reader($_POST['archivo'],true,"UTF-8");

    You can try UTF-16 if it keeps failing.

    answered by 17.06.2017 в 11:07