Project: Questionnaire
Requirement: WORD import questionnaire
Background: There are hundreds of WORD format questionnaires in the operation. If you go to the backend to enter them manually, There is no doubt a lot of work, and I hope it can be imported directly.
Mood: After receiving the request, I had mixed feelings, because I have done excel import before, and there are ready-made plug-ins, and I have to search a lot of codes.
Word import undoubtedly involves knowledge blind spots, but the demand is there, and you can’t beat the product classmates! I just had to bite the bullet.
Difficulty: Word is difficult to read the content, and the content is not structured well when read.
Ideas to solve the problem:
Read the WORD first, and then talk about how to structure it.
Read WORD:
At first I thought about using PHPWORD. After all, a mature plug-in like PHPOFFICE should be able to read WORD content directly.
However, the reality is very ugly. I searched all the documents and could not find a way to directly read the WORD content. PHPWORD only provides methods to convert WORD into HTML and TDF.
Conversion idea:
Since I can’t read WORD, then I can read HTML. I just need to convert WORD into HTML, and then read the HTML content. That’s it.
Code:
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use PhpOffice\PhpSpreadsheet\Reader\Html; use PhpOffice\PhpWord\Reader\Word2007; 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转换HTML $result=$word->load(storage_path('测试.docx')); $write=new \PhpOffice\PhpWord\Writer\HTML($result); $write->save(storage_path().'/测试.html'); //读取HTML内容 $document=new \DOMDocument(); $document->loadHTML(file_get_contents(storage_path('测试.html'))); $html=simplexml_import_dom($document); dd((array)$html->body); } }
Start test: New test.docx
Test.docx Content:
Execute script:
php artisan word
Result:
The above is the detailed content of Use PHP to change the way of reading WORD content. For more information, please follow other related articles on the PHP Chinese website!