예제 #1
0
 /**
  * Create excel from document
  *
  * @return \PHPExcel
  */
 protected function createExcel()
 {
     $this->phpexcel = new \PHPExcel();
     $tableNumber = 0;
     // Loop over all tables in document
     foreach ($this->document->getTables() as $table) {
         // Handle worksheets
         if ($tableNumber > 0) {
             $this->phpexcel->createSheet();
         }
         $excelWorksheet = $this->phpexcel->setActiveSheetIndex($tableNumber);
         if ($sheetTitle = $table->getAttribute('_excel-name')) {
             $excelWorksheet->setTitle($sheetTitle);
         }
         // Loop over all rows
         $rowNumber = 1;
         foreach ($table->getRows() as $row) {
             $excelWorksheet->getStyle($rowNumber . ':' . $rowNumber)->applyFromArray($this->getRowStylesArray($row));
             $this->setDimensions($excelWorksheet, $excelWorksheet->getRowIterator($rowNumber)->current(), $row);
             // Loop over all cells in row
             $cellNumber = 0;
             foreach ($row->getCells() as $cell) {
                 $excelCellIndex = \PHPExcel_Cell::stringFromColumnIndex($cellNumber) . $rowNumber;
                 // Set value
                 if ($explicitCellType = $cell->getAttribute('_excel-explicit') || ($explicitCellType = $row->getAttribute('_excel-explicit'))) {
                     $excelWorksheet->setCellValueExplicit($excelCellIndex, $this->changeValueEncoding($cell->getValue()), $this->convertStaticPhpExcelConstantsFromStringsToConstants($explicitCellType), true);
                 } else {
                     $excelWorksheet->setCellValue($excelCellIndex, $this->changeValueEncoding($cell->getValue()), true);
                 }
                 // Merge cells
                 $colspan = $cell->getAttribute('colspan');
                 $rowspan = $cell->getAttribute('rowspan');
                 if ($colspan || $rowspan) {
                     if ($colspan) {
                         $colspan = $colspan - 1;
                     }
                     if ($rowspan) {
                         $rowspan = $rowspan - 1;
                     }
                     $mergeCellsTargetCellIndex = \PHPExcel_Cell::stringFromColumnIndex($cellNumber + $colspan) . ($rowNumber + $rowspan);
                     $excelWorksheet->mergeCells($excelCellIndex . ':' . $mergeCellsTargetCellIndex);
                 }
                 // Set styles
                 $excelWorksheet->getStyle($excelCellIndex)->applyFromArray($this->getCellStylesArray($cell));
                 $this->setDimensions($excelWorksheet, $excelWorksheet->getCell($excelCellIndex), $cell);
                 $cellNumber++;
             }
             $rowNumber++;
         }
         $tableNumber++;
     }
     return $this->phpexcel;
 }
예제 #2
0
 /**
  * Parses the html code
  *
  * @return \Ticketpark\HtmlPhpExcel\Elements\Document
  */
 public function parse()
 {
     if (null === $this->html) {
         throw new HtmlPhpExcelException('You must provide html content first. Use setHtml() or setHtmlFile().');
     }
     $dom = new \DOMDocument();
     $dom->loadHTML($this->html);
     $xpath = new \DOMXPath($dom);
     $htmlTables = $xpath->query('.//table[contains(concat(" ", normalize-space(@class), " "), "' . $this->tableClass . '")]');
     $document = new Document();
     foreach ($htmlTables as $htmlTable) {
         $table = new Table();
         $htmlRows = $xpath->query('.//tr[contains(concat(" ", normalize-space(@class), " "), "' . $this->rowClass . '")]', $htmlTable);
         foreach ($htmlRows as $htmlRow) {
             $row = new Row();
             $htmlCells = $xpath->query('.//td[contains(concat(" ", normalize-space(@class), " "), "' . $this->cellClass . '")]
                 | .//th[contains(concat(" ", normalize-space(@class), " "), "' . $this->cellClass . '")]', $htmlRow);
             foreach ($htmlCells as $htmlCell) {
                 $cell = new Cell();
                 $cell->setValue($htmlCell->nodeValue);
                 foreach ($htmlCell->attributes as $attribute) {
                     $cell->addAttribute($attribute->name, $attribute->value);
                 }
                 if ('th' == $htmlCell->nodeName) {
                     $cell->setIsHeader(true);
                 }
                 $row->addCell($cell);
             }
             foreach ($htmlRow->attributes as $attribute) {
                 $row->addAttribute($attribute->name, $attribute->value);
             }
             $table->addRow($row);
         }
         foreach ($htmlTable->attributes as $attribute) {
             $table->addAttribute($attribute->name, $attribute->value);
         }
         $document->addTable($table);
     }
     return $document;
 }