Bill Garrison
Bill Garrison

Reputation: 2237

PHPExcel: How to get html entities to show up correctly in the HTML export

Okay, so I am exporting the same information in two different ways...'html' and 'xlsx'. In order to get up and down arrows in my xlsx I did the following:

$upArrow = html_entity_decode('↑',ENT_QUOTES,'UTF-8');
$downArrow = html_entity_decode('↓',ENT_QUOTES,'UTF-8');

This works great. However, this does not work for the HTML export so i did the following:

$upArrow = ($_REQUEST['t'] == 'html') ? '↑' : html_entity_decode('↑',ENT_QUOTES,'UTF-8');
$downArrow = ($_REQUEST['t'] == 'html') ? '↓' : html_entity_decode('↓',ENT_QUOTES,'UTF-8');

However, now I am getting the literal string ↓ instead of ↓.

Upvotes: 0

Views: 4801

Answers (1)

Mark Baker
Mark Baker

Reputation: 212522

The HTML Writer will treat strings for worksheet cells as strings, not as html entities, because that's what it's expecting from PHPExcel.... use the UTF-8 up and down arrow characters, and that should work for all writers

EDIT

include 'PHPExcel.php';

$phpe = new PHPExcel();
$sheet = $phpe->getActiveSheet();

// Write a UTF-8 up-arrow character to cell A1
$sheet->setCellValue(
    'A1', 
    html_entity_decode('↑',ENT_QUOTES,'UTF-8') . ' up arrow'
);
// Write a UTF-8 down-arrow character to cell A2
$sheet->setCellValue(
    'A2', 
    html_entity_decode('↓',ENT_QUOTES,'UTF-8') . ' down arrow'
);

// Save as an OpenOfficeXML .xlsx file
$writer = new PHPExcel_Writer_Excel2007($phpe);
$writer->save('./test.xlsx');

// Save as a BIFF-8 .xls file
$writer = new PHPExcel_Writer_HTML($phpe);
$writer->save('./test.html');

// Save as an HTML file
$writer = new PHPExcel_Writer_Excel5($phpe);
$writer->save('./test.xls');

EDIT 2

The Markup generated is:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<!-- Generated by PHPExcel - http://www.phpexcel.net -->
<html>
  <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <title>Untitled Spreadsheet</title>
      <meta name="author" content="Unknown Creator" />
      <meta name="title" content="Untitled Spreadsheet" />
      <meta name="company" content="Microsoft Corporation" />
    <style type="text/css">
      html { font-family:Calibri, Arial, Helvetica, sans-serif; font-size:11pt; background-color:white }
      table { border-collapse:collapse; page-break-after:always }
      .gridlines td { border:1px dotted black }
      .b { text-align:center }
      .e { text-align:center }
      .f { text-align:right }
      .inlineStr { text-align:left }
      .n { text-align:right }
      .s { text-align:left }
      td.style0 { vertical-align:bottom; border-bottom:none #000000; border-top:none #000000; border-left:none #000000; border-right:none #000000; color:#000000; font-family:'Calibri'; font-size:11pt; background-color:white }
      table.sheet0 col.col0 { width:42pt }
      table.sheet0 tr { height:15pt }
      table.sheet0 tr.row0 { height:15pt }
      table.sheet0 tr.row1 { height:15pt }
    </style>
  </head>

  <body>
<style>
@page { left-margin: 0.7in; right-margin: 0.7in; top-margin: 0.75in; bottom-margin: 0.75in; }
body { left-margin: 0.7in; right-margin: 0.7in; top-margin: 0.75in; bottom-margin: 0.75in; }
</style>
    <table border="0" cellpadding="0" cellspacing="0" id="sheet0" class="sheet0 gridlines">
        <col class="col0">
        <tbody>
          <tr class="row0">
            <td class="column0 style0 s">↑ up arrow</td>
          </tr>
          <tr class="row1">
            <td class="column0 style0 s">↓ down arrow</td>
          </tr>
        </tbody>
    </table>
  </body>
</html>

Upvotes: 3

Related Questions