You are here

class Excel in Forena Reports 8

Provides MS Excel Exports

Plugin annotation


@FrxDocument(
  id= "xls",
  name="Excel Export",
  ext="xls"
)

Hierarchy

Expanded class hierarchy of Excel

File

src/FrxPlugin/Document/Excel.php, line 14

Namespace

Drupal\forena\FrxPlugin\Document
View source
class Excel extends DocumentBase {
  public function __construct() {
    $this->content_type = 'application/msexcel';
  }
  public function header() {
    parent::header();
    $this
      ->write($output);
  }
  public function flush() {
    $body = $this->write_buffer;
    $output = '<?xml version="1.0"?>' . "\n";
    $output .= '<?mso-application progid="Excel.Sheet"?>' . "\n";
    $output .= '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"' . "\n";
    $output .= '  xmlns:o="urn:schemas-microsoft-com:office:office"' . "\n";
    $output .= '  xmlns:x="urn:schemas-microsoft-com:office:excel"' . "\n";
    $output .= '  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"' . "\n";
    $output .= '  xmlns:html="http://www.w3.org/TR/REC-html40">' . "\n";
    $output .= '<Styles>' . "\n";
    $output .= '  <Style ss:ID="Default" ss:Name="Normal">' . "\n";
    $output .= '    <Alignment ss:Vertical="Bottom"/>' . "\n";
    $output .= '    <Borders/>' . "\n";
    $output .= '    <Font/>' . "\n";
    $output .= '    <Interior/>' . "\n";
    $output .= '    <NumberFormat/>' . "\n";
    $output .= '    <Protection/>' . "\n";
    $output .= '  </Style>' . "\n";
    $output .= '</Styles>' . "\n";
    $doc = new \DOMDocument();
    $doc->strictErrorChecking = FALSE;
    libxml_use_internal_errors(true);
    @$doc
      ->loadHTML($body);
    libxml_clear_errors();
    $xml = simplexml_import_dom($doc);
    $tables = $xml ? $xml
      ->xpath('//table') : array();
    $count = 1;
    if ($tables) {
      foreach ($tables as $table) {
        $sheet_name = @(string) $table['data-caption'];
        if (!$sheet_name && @(string) $table->caption) {
          $sheet_name = (string) $table->caption;
        }
        if (!$sheet_name) {
          $sheet_name = 'sheet' . ' ' . $count;
        }
        $output .= '<Worksheet ss:Name="' . $sheet_name . '">' . "\n";
        $count++;
        $output .= '  <Table>' . "\n";
        $rows = $table
          ->xpath('descendant::tr');
        if ($rows) {
          foreach ($rows as $row) {
            $output .= '    <Row>' . "\n";
            foreach ($row as $column) {
              $class = @(string) $column['class'];
              $classes = explode(' ', $class);
              $text_class = array_search('XLSText', $classes) !== FALSE;
              $value = $column
                ->asXML();
              $value = strip_tags($value);
              $tval = trim($value);

              // Find if it contains invalid number characters
              $non_numeric_chars = trim($value, ' +-.,0123456789');

              // Determine if it contains +- in the interior
              // Zero is ok here bu
              $inner_symbols = FALSE;
              if (strpos($tval, '+') || strpos($tval, '-') || strpos($tval, ' ')) {
                $inner_symbols = TRUE;
              }
              if (substr_count($tval, '.') > 1) {
                $inner_symbols = TRUE;
              }
              if (empty($non_numeric_chars) && trim($value) !== '' && !$inner_symbols && !$text_class) {
                $output .= '      <Cell><Data ss:Type="Number">' . $tval . '</Data></Cell>' . "\n";
              }
              else {
                $output .= '      <Cell><Data ss:Type="String">' . $value . '</Data></Cell>' . "\n";
              }
            }
            $output .= '    </Row>' . "\n";
          }
        }
        $output .= '  </Table>' . "\n";
        $output .= '  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">' . "\n";
        $output .= '    <Selected/>' . "\n";
        $output .= '    <ProtectObjects>False</ProtectObjects>' . "\n";
        $output .= '    <ProtectScenarios>False</ProtectScenarios>' . "\n";
        $output .= '  </WorksheetOptions>' . "\n";
        $output .= '</Worksheet>' . "\n";
        $output .= '</Workbook>';
        $output = $this
          ->convertCharset($output);
        return $output;
      }
    }
    return $output;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DocumentBase::$buffer public property
DocumentBase::$charset public property
DocumentBase::$commands protected property
DocumentBase::$content_type public property
DocumentBase::$filename protected property
DocumentBase::$file_name public property
DocumentBase::$format public property
DocumentBase::$headers public property
DocumentBase::$libraries public property
DocumentBase::$parameters_form public property
DocumentBase::$skin protected property
DocumentBase::$skin_name protected property
DocumentBase::$title public property
DocumentBase::$write_buffer protected property
DocumentBase::addAjaxCommand public function
DocumentBase::check_markup public function Wrapper function for check output to default the right type.
DocumentBase::clear public function Clear the buffer Overrides DocumentInterface::clear
DocumentBase::convertCharset public function Perform character set conversion
DocumentBase::footer public function No default footer. Overrides DocumentInterface::footer 1
DocumentBase::getAjaxCommands public function
DocumentBase::setFilename public function Overrides DocumentInterface::setFilename
DocumentBase::setSkin public function Overrides DocumentInterface::setSkin
DocumentBase::write public function Write Overrides DocumentInterface::write
Excel::flush public function Write the output to disk. Overrides DocumentBase::flush
Excel::header public function Default implementation to put in content type based headers. Overrides DocumentBase::header
Excel::__construct public function
FrxAPI::app public function Returns containing application service
FrxAPI::currentDataContext public function Get the current data context.
FrxAPI::currentDataContextArray public function
FrxAPI::dataManager public function Returns the data manager service
FrxAPI::dataService public function Return Data Service
FrxAPI::documentManager public function Returns the fornea document manager
FrxAPI::error public function Report an error
FrxAPI::getDataContext public function Get the context of a specific id.
FrxAPI::getDocument public function Get the current document
FrxAPI::getReportFileContents public function Load the contents of a file in the report file system.
FrxAPI::innerXML function Enter description here... 1
FrxAPI::popData public function Pop data off of the stack.
FrxAPI::pushData public function Push data onto the Stack
FrxAPI::report public function Run a report with a particular format. 1
FrxAPI::reportFileSystem public function Get the current report file system.
FrxAPI::setDataContext public function Set Data context by id.
FrxAPI::setDocument public function Change to a specific document type.
FrxAPI::skins public function Get list of skins.