PHPExcel can't read special char

Topics: Developer Forum, User Forum
Sep 2, 2014 at 3:46 PM
I'm using somethings from this site to convert table html to excel file . https://phpexcel.codeplex.com/discussions/275807

I realised when Im open the excel file ,it not include my data that have special char < .Example,I have a columns that have data like this , <1004> . I think I have to change something about DOM but I'm really new in this topic.Anyone have a experience about this.Any help will be greatly appreciate.Thanks.
<?php
include_once(dirname(__FILE__) . '/../php/ixcdr.php');
session_start();
$callhistoryTable .='<html>';
$callhistoryTable .='<head>';
$callhistoryTable .='<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
$callhistoryTable .='</head>';
$callhistoryTable .='<div class="contentlist">';
$callhistoryTable .='<form id="history_form">';
$callhistoryTable .='<table style="font-size:11px;" width="1050" border="0" id="allhist" class="tablesorter-blue">';
$callhistoryTable .='<thead>';
$callhistoryTable .='<tr>';
$callhistoryTable .='<th width="50">Call Date</th>';
$callhistoryTable .='<th width="200">CallerId</th>';
$callhistoryTable .='<th width="100">Origination</th>';
$callhistoryTable .='<th width="100">Destination</th>';
$callhistoryTable .='<th width="100">Duration</th>';
$callhistoryTable .='<th width="200">Account Code</th>';
$callhistoryTable .='</tr>';
$callhistoryTable .='</thead>';
$callhistoryTable .='<tbody>';

                $userid = $_POST['searchnum'];
                $fromdate = $_POST['fromdate'];
                $todate = $_POST['todate'];

                $getCdrList = getAllCdr($userid, $fromdate, $todate);
                if (count($getCdrList) > 0) {
                    for ($i = 0; $i < count($getCdrList); $i ++) {
                        $getCdrListRecord = $getCdrList[$i];
$callhistoryTable .='<tr>';
$callhistoryTable .='<td style="font-weight: bold;">'.$getCdrListRecord->get_CallDate().'</td>';
$callhistoryTable .='<td style="font-weight: bold;">'.$getCdrListRecord->get_Clid().'</td>';
$callhistoryTable .='<td style="font-weight: bold;">'.$getCdrListRecord->get_Src().'</td>';
$callhistoryTable .='<td style="font-weight: bold;">'.$getCdrListRecord->get_Dst().'</td>';
$callhistoryTable .='<td style="font-weight: bold;">'.$getCdrListRecord->get_Duration().'</td>';
$callhistoryTable .='<td>'.$getCdrListRecord->get_AccountCode().'</td>';
$callhistoryTable .='</tr>';
                    }
                } else {
$callhistoryTable .='<tr>';
$callhistoryTable .='<td colspan="6">No cdr to display</td>';
$callhistoryTable .='</tr>';
                }

                echo $callhistoryTable;
                ?>
            </tbody>
        </table>
        <table width="1000px" style="margin: 10px 30px 15px;">
            <tr><td>
                    <div id="pager" class="pager">
                        <form>
                            <a href="/Blackbox/ippbx/load/exportxls.php?var=tableConvertxls" target="_blank" title="Export table to Excel"><img src="img/32x32/file_extension_xls.png" alt="Export table to Excel" hspace="3" /></a>
                        </form>
                    </div>
                </td></tr>
        </table>
    </form>
</div>
<html>
<?php
$_SESSION['tableConvertxls'] = $callhistoryTable;
exit;

?>
Send to this FILE

``` <?php
session_start();
ini_set("memory_limit", "-1");
ini_set("set_time_limit", "0");
set_time_limit(0);

if (!isset($_GET['var'])) {
echo "<br />No Table Variable Present, nothing to Export.";
exit;
} else {
$tablevar = $_GET['var'];
}
if (!isset($_SESSION[$tablevar]) OR $_SESSION[$tablevar] == '') {
echo "<br />Empty HTML Table, nothing to Export.";
exit;
} else {
$htmltable = $_SESSION[$tablevar];  //table data as html format
}
if (strlen($htmltable) == strlen(strip_tags($htmltable))) { //if data_table format not as expected
echo "<br />Invalid HTML Table after Stripping Tags, nothing to Export.";
exit;
}
$htmltable = strip_tags($htmltable, "<table><thead><tr><th></th></tr></thead><tbody><tr><td></td></tr></tbody>");
$htmltable = str_replace("<br />", "\n", $htmltable);
$htmltable = str_replace("<br/>", "\n", $htmltable);
$htmltable = str_replace("<br>", "\n", $htmltable);
$htmltable = str_replace(" ", " ", $htmltable);
$htmltable = str_replace("\n\n", "\n", $htmltable);

//
// Extract HTML table contents to array
//
$dom = new domDocument;
//$dom->loadHTML($htmltable);
//$dom->loadHTML(mb_convert_encoding($htmltable, 'HTML-ENTITIES', 'UTF-8'));
$dom->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$htmltable);
if (!$dom) {
echo "<br />Invalid HTML DOM, nothing to Export.";
exit;
}
$dom->preserveWhiteSpace = false; // remove redundant whitespace
$tables = $dom->getElementsByTagName('table');

if (!is_object($tables)) {
echo "<br />Invalid HTML Table DOM, nothing to Export.";
exit;
}
if ($debug) {
fwrite($handle, "\nTable Count: " . $tables->length);
}
$tbcnt = $tables->length - 1; // count minus 1 for 0 indexed loop over tables
if ($tbcnt > $limit) {
$tbcnt = $limit;
}
//
//
// Create new PHPExcel object with default attributes
//
require_once ('/../classes/PHPExcel.php');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getDefaultStyle()->getFont()->setName('Arial');
$objPHPExcel->getDefaultStyle()->getFont()->setSize(9);
$tm = date(Ymd);
$name = "historyrecord";
$tfn = $name . "_" . $tm . ".xlsx";
//$fname = "AuditLog/".$tfn;
$fname = $tfn;
$objPHPExcel->getProperties()->setCreator($_SESSION['IX_USERNAME'])
    ->setLastModifiedBy($_SESSION['IX_USERNAME'])
    ->setTitle("Automated Export")
    ->setSubject("Automated Report Generation")
    ->setDescription("Automated report generation.")
    ->setKeywords("Exported File")
    ->setCompany($usercompany)
    ->setCategory("Export");
//
// Loop over tables in DOM to create an array, each table becomes a worksheet
//
for ($z = 0; $z <= $tbcnt; $z++) {
$maxcols = 0;
$totrows = 0;
$headrows = array();
$bodyrows = array();
$r = 0;
$h = 0;
$rows = $tables->item($z)->getElementsByTagName('tr');
$totrows = $rows->length;
if ($debug) {
    fwrite($handle, "\nTotal Rows: " . $totrows);
}
foreach ($rows as $row) {
    $ths = $row->getElementsByTagName('th');
    if (is_object($ths)) {
        if ($ths->length > 0) {
            $headrows[$h]['colcnt'] = $ths->length;
            if ($ths->length > $maxcols) {
                $maxcols = $ths->length;
            }
            $nodes = $ths->length - 1;
            for ($x = 0; $x <= $nodes; $x++) {
                $thishdg = $ths->item($x)->nodeValue;
                $headrows[$h]['th'][] = $thishdg;
                $headrows[$h]['bold'][] = findBoldText(innerHTML($ths->item($x)));
                if ($ths->item($x)->hasAttribute('style')) {
                    $style = $ths->item($x)->getAttribute('style');
                    $stylecolor = findStyleColor($style);
                    if ($stylecolor == '') {
                        $headrows[$h]['color'][] = findSpanColor(innerHTML($ths->item($x)));
                    } else {
                        $headrows[$h]['color'][] = $stylecolor;
                    }
                } else {
                    $headrows[$h]['color'][] = findSpanColor(innerHTML($ths->item($x)));
                }
                if ($ths->item($x)->hasAttribute('colspan')) {
                    $headrows[$h]['colspan'][] = $ths->item($x)->getAttribute('colspan');
                } else {
                    $headrows[$h]['colspan'][] = 1;
                }
                if ($ths->item($x)->hasAttribute('align')) {
                    $headrows[$h]['align'][] = $ths->item($x)->getAttribute('align');
                } else {
                    $headrows[$h]['align'][] = 'left';
                }
                if ($ths->item($x)->hasAttribute('valign')) {
                    $headrows[$h]['valign'][] = $ths->item($x)->getAttribute('valign');
                } else {
                    $headrows[$h]['valign'][] = 'top';
                }
                if ($ths->item($x)->hasAttribute('bgcolor')) {
                    $headrows[$h]['bgcolor'][] = str_replace("#", "", $ths->item($x)->getAttribute('bgcolor'));
                } else {
                    $headrows[$h]['bgcolor'][] = 'FFFFFF';
                }
            }
            $h++;
        }
    }
}
foreach ($rows as $row) {
    $tds = $row->getElementsByTagName('td');
    if (is_object($tds)) {
        if ($tds->length > 0) {
            $bodyrows[$r]['colcnt'] = $tds->length;
            if ($tds->length > $maxcols) {
                $maxcols = $tds->length;
            }
            $nodes = $tds->length - 1;
            for ($x = 0; $x <= $nodes; $x++) {
                $thistxt = $tds->item($x)->nodeValue;  //value data
                $bodyrows[$r]['td'][] = $thistxt;
                $bodyrows[$r]['bold'][] = findBoldText(innerHTML($tds->item($x)));
                if ($tds->item($x)->hasAttribute('style')) {
                    $style = $tds->item($x)->getAttribute('style');
                    $stylecolor = findStyleColor($style);
                    if ($stylecolor == '') {
                        $bodyrows[$r]['color'][] = findSpanColor(innerHTML($tds->item($x)));
                    } else {
                        $bodyrows[$r]['color'][] = $stylecolor;
                    }
                } else {
                    $bodyrows[$r]['color'][] = findSpanColor(innerHTML($tds->item($x)));
                }
                if ($tds->item($x)->hasAttribute('colspan')) {
                    $bodyrows[$r]['colspan'][] = $tds->i
Sep 2, 2014 at 4:16 PM
Please someone reply on this !!!!