亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    用PHP換個思路讀取WORD內(nèi)容

    用PHP換個思路讀取WORD內(nèi)容

    項目:問卷

    需求:WORD 導(dǎo)入問卷

    背景:運營那里有幾百個 WORD 格式問卷,如果去后臺手動錄入,無疑工作量很大,希望能直接導(dǎo)入。

    心情:接到需求之后五味雜陳,因為以前做過 excel 導(dǎo)入,而且有現(xiàn)成的插件,代碼也是一搜索一堆。

    word 導(dǎo)入無疑涉及到了知識盲點,但是需求就在那里,又懟不過產(chǎn)品同學(xué)!只能硬著頭皮上了。

    難點:word 不好讀取內(nèi)容,內(nèi)容讀出來不好結(jié)構(gòu)化。

    解決問題思路:

    先讀取 WORD, 再說怎么結(jié)構(gòu)化。

    讀取 WORD:

    一開始想著用 PHPWORD, 畢竟 PHPOFFICE 這么成熟的插件應(yīng)該可以直接讀取到 WORD 內(nèi)容吧。

    然而現(xiàn)實很骨感,找遍了文檔并沒有找到直接讀取到 WORD 內(nèi)容的方法。PHPWORD 只提供了把 WORD 轉(zhuǎn)換成 HTML,TDF 的方法。

    轉(zhuǎn)換思路:

    既然不能讀取 WORD, 那我可以讀取 HTML, 只需要把 WORD 轉(zhuǎn)換成 HTML 就可以了,然后讀取 HTML 內(nèi)容就行。

    代碼:

    <?php namespace AppConsoleCommands; use IlluminateConsoleCommand; use PhpOfficePhpSpreadsheetReaderHtml; use PhpOfficePhpWordReaderWord2007; class Test extends Command {     /**      * The name and signature of the console command.      *      * @var string      */     protected $signature = 'word';     /**      * The console command description.      *      * @var string      */     protected $description = 'word';     /**      * Create a new command instance.      *      * @return void      */     public function __construct() {         parent::__construct();     }     /**      * Execute the console command.      *      * @return int      */     public function handle(Word2007 $word) {         //WORD轉(zhuǎn)換HTML         $result=$word->load(storage_path('測試.docx'));         $write=new PhpOfficePhpWordWriterHTML($result);         $write->save(storage_path().'/測試.html');         //讀取HTML內(nèi)容         $document=new DOMDocument();         $document->loadHTML(file_get_contents(storage_path('測試.html')));         $html=simplexml_import_dom($document);         dd((array)$html->body);     } }

    開始測試:新建 測試.docx

    測試.docx 內(nèi)容:

    用PHP換個思路讀取WORD內(nèi)容

    執(zhí)行腳本:

    php artisan word

    結(jié)果:

    用PHP換個思路讀取WORD內(nèi)容

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號