30 if (!defined(
'PHPEXCEL_ROOT')) {
34 define(
'PHPEXCEL_ROOT', dirname(__FILE__) .
'/../../');
38 require_once PHPEXCEL_ROOT .
'PHPExcel/Writer/IWriter.php';
41 require_once PHPEXCEL_ROOT .
'PHPExcel/Cell.php';
44 require_once PHPEXCEL_ROOT .
'PHPExcel/RichText.php';
47 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/Drawing.php';
50 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/String.php';
53 require_once PHPEXCEL_ROOT .
'PHPExcel/HashTable.php';
133 $this->_phpExcel = $phpExcel;
134 $this->_defaultFontSize = $this->_phpExcel->
getDefaultStyle()->getFont()->getSize();
135 $this->_sheetIndex = 0;
136 $this->_imagesRoot =
'.';
145 public function save($pFilename = null) {
147 $this->_phpExcel->garbageCollect();
153 $this->
buildCSS(!$this->_useInlineCss);
156 $fileHandle = fopen($pFilename,
'w');
157 if ($fileHandle ===
false) {
158 throw new Exception(
"Could not open file $pFilename for writing.");
185 default:
return 'baseline';
201 default:
return false;
209 switch ($borderStyle) {
214 default:
return '1px solid';
234 $this->_sheetIndex = $pValue;
242 $this->_sheetIndex = null;
254 if (is_null($this->_phpExcel)) {
255 throw new Exception(
'Internal PHPExcel object not set to an instance of an object.');
260 $html .=
'<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">' .
"\r\n";
261 $html .=
'<!-- Generated by PHPExcel - http://www.phpexcel.net -->' .
"\r\n";
262 $html .=
'<html>' .
"\r\n";
263 $html .=
' <head>' .
"\r\n";
264 $html .=
' <meta http-equiv="Content-Type" content="text/html; charset=utf-8">' .
"\r\n";
265 $html .=
' <title>' . htmlspecialchars($this->_phpExcel->getProperties()->getTitle()) .
'</title>' .
"\r\n";
266 if ($pIncludeStyles) {
269 $html .=
' </head>' .
"\r\n";
270 $html .=
'' .
"\r\n";
271 $html .=
' <body>' .
"\r\n";
285 if (is_null($this->_phpExcel)) {
286 throw new Exception(
'Internal PHPExcel object not set to an instance of an object.');
291 if (is_null($this->_sheetIndex)) {
292 $sheets = $this->_phpExcel->getAllSheets();
294 $sheets[] = $this->_phpExcel->getSheet($this->_sheetIndex);
302 foreach ($sheets as $sheet) {
304 $cellCollection = $sheet->getCellCollection();
310 $dimension = explode(
':', $sheet->calculateWorksheetDimension());
318 for (
$row = $dimension[0][1];
$row <= $dimension[1][1]; ++
$row) {
323 for ($column = $dimension[0][0]; $column <= $dimension[1][0]; ++$column) {
325 if ($sheet->cellExistsByColumnAndRow($column,
$row)) {
326 $rowData[$column] = $sheet->getCellByColumnAndRow($column,
$row);
328 $rowData[$column] =
'';
342 if (is_null($this->_sheetIndex) && $sheetId + 1 < $this->_phpExcel->getSheetCount()) {
343 $html .=
'<tcpdf method="AddPage" />';
370 if ($drawing->getCoordinates() == $coordinates) {
390 $html .=
' <img style="position: relative; left: ' . $drawing->getOffsetX() .
'px; top: ' . $drawing->getOffsetY() .
'px; width: ' . $drawing->getWidth() .
'px; height: ' . $drawing->getHeight() .
'px;" src="' .
$filename .
'" border="0" width="' . $drawing->getWidth() .
'" height="' . $drawing->getHeight() .
'" />' .
"\r\n";
408 if (is_null($this->_phpExcel)) {
409 throw new Exception(
'Internal PHPExcel object not set to an instance of an object.');
413 $css = $this->
buildCSS($generateSurroundingHTML);
419 if ($generateSurroundingHTML) {
420 $html .=
' <style type="text/css">' .
"\r\n";
421 $html .=
' html { ' . $this->
_assembleCSS($css[
'html']) .
' }' .
"\r\n";
425 foreach ($css as $styleName => $styleDefinition) {
426 if ($styleName !=
'html') {
427 $html .=
' ' . $styleName .
' { ' . $this->
_assembleCSS($styleDefinition) .
' }' .
"\r\n";
432 if ($generateSurroundingHTML) {
433 $html .=
' </style>' .
"\r\n";
447 public function buildCSS($generateSurroundingHTML =
true) {
449 if (is_null($this->_phpExcel)) {
450 throw new Exception(
'Internal PHPExcel object not set to an instance of an object.');
454 if (!is_null($this->_cssStyles)) {
462 if ($generateSurroundingHTML) {
464 $css[
'html'][
'font-family'] =
'Calibri, Arial, Helvetica, sans-serif';
465 $css[
'html'][
'font-size'] =
'11pt';
466 $css[
'html'][
'background-color'] =
'white';
471 $css[
'table'][
'border-collapse'] =
'collapse';
472 $css[
'table'][
'page-break-after'] =
'always';
475 $css[
'.gridlines td'][
'border'] =
'1px dotted black';
478 $css[
'.b'][
'text-align'] =
'center';
481 $css[
'.e'][
'text-align'] =
'center';
484 $css[
'.f'][
'text-align'] =
'right';
487 $css[
'.inlineStr'][
'text-align'] =
'left';
490 $css[
'.n'][
'text-align'] =
'right';
493 $css[
'.s'][
'text-align'] =
'left';
496 foreach ($this->_phpExcel->getCellXfCollection() as $index => $style) {
502 if (is_null($this->_sheetIndex)) {
503 $sheets = $this->_phpExcel->getAllSheets();
505 $sheets[] = $this->_phpExcel->getSheet($this->_sheetIndex);
509 foreach ($sheets as $sheet) {
511 $sheetIndex = $sheet->getParent()->getIndex($sheet);
515 $sheet->calculateColumnWidths();
519 for ($column = 0; $column <= $highestColumnIndex; ++$column) {
520 $this->_columnWidths[$sheetIndex][$column] = 42;
521 $css[
'table.sheet' . $sheetIndex .
' col.col' . $column][
'width'] =
'42pt';
525 foreach ($sheet->getColumnDimensions() as $columnDimension) {
529 $this->_columnWidths[$sheetIndex][$column] = $width;
530 $css[
'table.sheet' . $sheetIndex .
' col.col' . $column][
'width'] = $width .
'pt';
532 if ($columnDimension->getVisible() ===
false) {
533 $css[
'table.sheet' . $sheetIndex .
' col.col' . $column][
'visibility'] =
'collapse';
534 $css[
'table.sheet' . $sheetIndex .
' col.col' . $column][
'*display'] =
'none';
540 $rowDimension = $sheet->getDefaultRowDimension();
543 $css[
'table.sheet' . $sheetIndex .
' tr'] = array();
545 $pt_height = $rowDimension->getRowHeight();
546 $css[
'table.sheet' . $sheetIndex .
' tr'][
'height'] = $pt_height .
'pt';
547 if ($rowDimension->getVisible() ===
false) {
548 $css[
'table.sheet' . $sheetIndex .
' tr'][
'display'] =
'none';
549 $css[
'table.sheet' . $sheetIndex .
' tr'][
'visibility'] =
'hidden';
553 foreach ($sheet->getRowDimensions() as $rowDimension) {
554 $row = $rowDimension->getRowIndex() - 1;
557 $css[
'table.sheet' . $sheetIndex .
' tr.row' .
$row] = array();
559 $pt_height = $rowDimension->getRowHeight();
560 $css[
'table.sheet' . $sheetIndex .
' tr.row' .
$row][
'height'] = $pt_height .
'pt';
561 if ($rowDimension->getVisible() ===
false) {
562 $css[
'table.sheet' . $sheetIndex .
' tr.row' .
$row][
'display'] =
'none';
563 $css[
'table.sheet' . $sheetIndex .
' tr.row' .
$row][
'visibility'] =
'hidden';
569 if (is_null($this->_cssStyles)) {
570 $this->_cssStyles = $css;
612 $css[
'text-align'] = $textAlign;
631 $css[
'font-weight'] =
'bold';
634 $css[
'text-decoration'] =
'underline line-through';
636 $css[
'text-decoration'] =
'underline';
638 $css[
'text-decoration'] =
'line-through';
641 $css[
'font-style'] =
'italic';
644 $css[
'color'] =
'#' . $pStyle->
getColor()->getRGB();
645 $css[
'font-family'] =
'\'' . $pStyle->
getName() .
'\'';
646 $css[
'font-size'] = $pStyle->
getSize() .
'pt';
702 $css[
'background-color'] = $value;
714 $html .=
' </body>' .
"\r\n";
715 $html .=
'</html>' .
"\r\n";
729 $sheetIndex = $pSheet->getParent()->getIndex($pSheet);
734 if (!$this->_useInlineCss) {
735 $gridlines = $pSheet->getShowGridLines() ?
' gridlines' :
'';
736 $html .=
' <table border="0" cellpadding="0" cellspacing="0" class="sheet' . $sheetIndex . $gridlines .
'">' .
"\r\n";
738 $style = isset($this->_cssStyles[
'table']) ?
741 $html .=
' <table border="0" cellpadding="0" cellspacing="0" style="' . $style .
'">' .
"\r\n";
746 for ($i = 0; $i <= $highestColumnIndex; ++$i) {
747 if (!$this->_useInlineCss) {
748 $html .=
' <col class="col' . $i .
'">' .
"\r\n";
750 $style = isset($this->_cssStyles[
'table.sheet' . $sheetIndex .
' col.col' . $i]) ?
751 $this->
_assembleCSS($this->_cssStyles[
'table.sheet' . $sheetIndex .
' col.col' . $i]) :
'';
752 $html .=
' <col style="' . $style .
'">' .
"\r\n";
768 $html .=
' </table>' .
"\r\n";
784 if (is_array($pValues)) {
789 $sheetIndex = $pSheet->
getParent()->getIndex($pSheet);
792 if (!$this->_useInlineCss) {
793 $html .=
' <tr class="row' . $pRow .
'">' .
"\r\n";
795 $style = isset($this->_cssStyles[
'table.sheet' . $sheetIndex .
' tr.row' . $pRow])
796 ? $this->
_assembleCSS($this->_cssStyles[
'table.sheet' . $sheetIndex .
' tr.row' . $pRow]) :
'';
798 $html .=
' <tr style="' . $style .
'">' .
"\r\n";
803 foreach ($pValues as $cell) {
804 if (!$this->_useInlineCss) {
806 $cssClass =
'column' . $colNum;
809 if (isset($this->_cssStyles[
'table.sheet' . $sheetIndex .
' td.column' . $colNum])) {
810 $this->_cssStyles[
'table.sheet' . $sheetIndex .
' td.column' . $colNum];
826 $elements = $cell->getValue()->getRichTextElements();
827 foreach ($elements as $element) {
832 if ($element->getFont()->getSuperScript()) {
833 $cellData .=
'<sup>';
834 }
else if ($element->getFont()->getSubScript()) {
835 $cellData .=
'<sub>';
840 $cellText = $element->getText();
841 $cellData .= htmlspecialchars($cellText);
843 if ($element instanceof PHPExcel_RichText_Run) {
844 if ($element->getFont()->getSuperScript()) {
845 $cellData .=
'</sup>';
846 }
else if ($element->getFont()->getSubScript()) {
847 $cellData .=
'</sub>';
850 $cellData .=
'</span>';
854 if ($this->_preCalculateFormulas) {
856 $cell->getCalculatedValue(),
857 $pSheet->
getParent()->getCellXfByIndex( $cell->getXfIndex() )->getNumberFormat()->getFormatCode()
860 $cellData = PHPExcel_Style_NumberFormat::ToFormattedString(
862 $pSheet->
getParent()->getCellXfByIndex( $cell->getXfIndex() )->getNumberFormat()->getFormatCode()
867 $cellData = htmlspecialchars($cellData);
874 $cellData = str_replace(
"\n",
'<br/>', $cellData);
877 if ($cellData ==
'') {
878 $cellData =
' ';
882 if (!$this->_useInlineCss) {
883 $cssClass .=
' style' . $cell->getXfIndex();
884 $cssClass .=
' ' . $cell->getDataType();
886 if (isset($this->_cssStyles[
'td.style' . $cell->getXfIndex()])) {
887 $cssClass = array_merge($cssClass, $this->_cssStyles[
'td.style' . $cell->getXfIndex()]);
891 $sharedStyle = $pSheet->
getParent()->getCellXfByIndex( $cell->getXfIndex() );
893 && isset($this->_cssStyles[
'.' . $cell->getDataType()][
'text-align']))
895 $cssClass[
'text-align'] = $this->_cssStyles[
'.' . $cell->getDataType()][
'text-align'];
899 $cell =
new PHPExcel_Cell(
909 if ($cell->hasHyperlink() && !$cell->getHyperlink()->isInternal()) {
910 $cellData =
'<a href="' . htmlspecialchars($cell->getHyperlink()->getUrl()) .
'" title="' . htmlspecialchars($cell->getHyperlink()->getTooltip()) .
'">' . $cellData .
'</a>';
915 if ($cell->isInRange($cells)) {
918 if ($first[0] == $cell->getCoordinate()) {
932 if (!$this->_useInlineCss) {
933 $html .=
' class="' . $cssClass .
'"';
940 for ($i = $columnIndex; $i < $columnIndex + $colSpan; ++$i) {
941 if (isset($this->_columnWidths[$sheetIndex][$i])) {
942 $width += $this->_columnWidths[$sheetIndex][$i];
945 $cssClass[
'width'] = $width .
'pt';
949 if (isset($this->_cssStyles[
'table.sheet' . $sheetIndex .
' tr.row' . $pRow][
'height'])) {
950 $height = $this->_cssStyles[
'table.sheet' . $sheetIndex .
' tr.row' . $pRow][
'height'];
951 $cssClass[
'height'] = $height;
955 $html .=
' style="' . $this->
_assembleCSS($cssClass) .
'"';
958 $html .=
' colspan="' . $colSpan .
'"';
961 $html .=
' rowspan="' . $rowSpan .
'"';
972 $html .=
'</td>' .
"\r\n";
980 $html .=
' </tr>' .
"\r\n";
985 throw new Exception(
"Invalid parameters passed.");
998 foreach ($pValue as $property => $value) {
999 $pairs[] = $property .
':' . $value;
1001 $string = implode(
'; ', $pairs);
1022 $this->_preCalculateFormulas = $pValue;
1042 $this->_imagesRoot = $pValue;
1062 $this->_useInlineCss = $pValue;
1075 $explodes = explode(
"\n", $pValue);
1076 foreach ($explodes as $explode) {
1078 if (preg_match(
'/^( )+/', $explode, $matches)) {
1079 $explode = str_repeat(
' ', strlen($matches[0])) . substr($explode, strlen($matches[0]));
1081 $implodes[] = $explode;
1084 $string = implode(
"\n", $implodes);