Browse Source

word导出测试

visuddhinanda 4 years ago
parent
commit
3c86cdd034
1 changed files with 100 additions and 0 deletions
  1. 100 0
      app/article/export.php

+ 100 - 0
app/article/export.php

@@ -0,0 +1,100 @@
+<?php
+require_once '../../vendor/autoload.php';
+require_once '../path.php';
+require_once '../public/function.php';
+
+
+// Creating the new document...
+$phpWord = new \PhpOffice\PhpWord\PhpWord();
+
+
+
+$phpWord->addTitleStyle(1, array('size' => 16), array('numStyle' => 'hNum', 'numLevel' => 0));
+$phpWord->addTitleStyle(2, array('size' => 14), array('numStyle' => 'hNum', 'numLevel' => 1));
+$phpWord->addTitleStyle(3, array('size' => 12), array('numStyle' => 'hNum', 'numLevel' => 2));
+
+/* Note: any element you append to a document must reside inside of a Section. */
+
+// Adding an empty Section to the document...
+$section = $phpWord->addSection();
+
+$section->addTitle('Article Title', 1);
+
+// Adding Text element to the Section having font styled by default...
+$section->addText(
+    '"Learn from yesterday, live for today, hope for tomorrow. '
+        . 'The important thing is not to stop questioning." '
+        . '(Albert Einstein)'
+);
+
+/*
+ * Note: it's possible to customize font style of the Text element you add in three ways:
+ * - inline;
+ * - using named font style (new font style object will be implicitly created);
+ * - using explicitly created font style object.
+ */
+
+// Adding Text element with font customized inline...
+$section->addText(
+    '"Great achievement is usually born of great sacrifice, '
+        . 'and is never the result of selfishness." '
+        . '(Napoleon Hill)',
+    array('name' => 'Tahoma', 'size' => 10)
+);
+
+$textrun = $section->addTextRun();
+$textrun->addText('Lead text.');
+$footnote = $textrun->addFootnote();
+$footnote->addText('Footnote text can have ');
+$footnote->addLink('http://test.com', 'links');
+$footnote->addText('.');
+$footnote->addTextBreak();
+$footnote->addText('And text break.');
+$textrun->addText('Trailing text.');
+
+// Adding Text element with font customized using named font style...
+$fontStyleName = 'oneUserDefinedStyle';
+$phpWord->addFontStyle(
+    $fontStyleName,
+    array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
+);
+$section->addText(
+    '"The greatest accomplishment is not in never falling, '
+        . 'but in rising again after you fall." '
+        . '(Vince Lombardi)',
+    $fontStyleName
+);
+
+// Adding Text element with font customized using explicitly created font style object...
+$fontStyle = new \PhpOffice\PhpWord\Style\Font();
+$fontStyle->setBold(true);
+$fontStyle->setName('Tahoma');
+$fontStyle->setSize(13);
+$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
+$myTextElement->setFontStyle($fontStyle);
+
+// Saving the document as OOXML file...
+$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
+$tmpFileName = _DIR_TMP_EXPORT.'/'.UUID::v4().'.docx';
+$objWriter->save($tmpFileName);
+
+//4.从浏览器下载
+ob_clean();
+ob_start();
+$fp = fopen($tmpFileName,"r");
+$file_size = filesize($tmpFileName);
+Header("Content-type:application/octet-stream");
+Header("Accept-Ranges:bytes");
+Header("Accept-Length:".$file_size);
+Header("Content-Disposition:attchment; filename=".'wikipali.docx');
+$buffer = 1024;
+$file_count = 0;
+while (!feof($fp) && $file_count < $file_size){
+    $file_con = fread($fp,$buffer);
+    $file_count += $buffer;
+    echo $file_con;
+}
+fclose($fp);
+ob_end_flush();
+
+unlink($tmpFileName);