php 如何获取表格数据类型,使用phpword获取doc中的表格数据
* Created by PhpStorm.
* User: parker
* Date: 2020/10/18
* Time: 16:09*/namespace common\services;class WordService extendsBaseService
{public static function importWord($info)
{$word = self::getWord($info['path']);
dd($word);
}/**
* 獲取word文檔內容
* @param string $path
* @return array*/
public static function getWord($path = '')
{//加載word文檔,使用phpword處理
$phpWord = \PhpOffice\PhpWord\IOFactory::load($path);return self::getNodeContent($phpWord);
}/**
* 根據word主節點獲取分節點內容
* @param $word
* @return array*/
public static function getNodeContent($word)
{$return =[];//分解部分
foreach ($word->getSections() as $section)
{if ($sectioninstanceof \PhpOffice\PhpWord\Element\Section) {//分解元素
foreach ($section->getElements() as $element)
{//文本元素
if ($elementinstanceof \PhpOffice\PhpWord\Element\TextRun) {$text = '';foreach ($element->getElements() as $ele) {$text .= self::getTextNode($ele);
}$return[] = $text;
}//表格元素
else if ($elementinstanceof \PhpOffice\PhpWord\Element\Table) {foreach ($element->getRows() as $ele)
{$return[] = self::getTableNode($ele);
}
}
}
}
}return $return;
}/**
* 獲取文檔節點內容
* @param $node
* @return string*/
public static function getTextNode($node)
{$return = '';//處理文本
if ($nodeinstanceof \PhpOffice\PhpWord\Element\Text)
{$return .= $node->getText();
}//處理圖片
else if ($nodeinstanceof \PhpOffice\PhpWord\Element\Image)
{$return .= self::pic2text($node);
}//處理文本元素
else if ($nodeinstanceof \PhpOffice\PhpWord\Element\TextRun) {foreach ($node->getElements() as $ele) {$return .= self::getTextNode($ele);
}
}return $return;
}/**
* 獲取表格節點內容
* @param $node
* @return string*/
public static function getTableNode($node)
{$return = '';//處理行
if ($nodeinstanceof \PhpOffice\PhpWord\Element\Row) {foreach ($node->getCells() as $ele)
{$return .= self::getTableNode($ele);
}
}//處理列
else if ($nodeinstanceof \PhpOffice\PhpWord\Element\Cell) {foreach ($node->getElements() as $ele)
{$return .= self::getTextNode($ele);
}
}return $return;
}/**
* 處理word文檔中base64格式圖片
* @param $node
* @return string*/
public static function pic2text($node)
{//獲取圖片編碼
$imageData = $node->getImageStringData(true);//添加圖片html顯示標頭
$imageData = 'data:' . $node->getImageType() . ';base64,' . $imageData;$return = '';return $return;
}/**
* 處理word文檔中base64格式圖片
* @param $node
* @return string*/
public static function pic2file($node)
{//圖片地址(一般為word文檔地址+在word中的錨點位置)
$imageSrc = 'images/' . md5($node->getSource()) . '.' . $node->getImageExtension();$imageData = $node->getImageStringData(true);//將圖片保存在本地
file_put_contents($imageSrc, base64_decode($imageData));return $imageSrc;
}/**
* 將word轉化為html(轉換存儲html文件后展示)
* @param $path
* @throws \PhpOffice\PhpWord\Exception\Exception*/
public static function word2html($path)
{$phpWord = FileImportService::getOne($path);//轉為html處理
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "HTML");$path = pathinfo($path);$fileName = $path['dirname'] . '/' . $path['filename'] . '.html';$xmlWriter->save($fileName);$html = file_get_contents($fileName);echo $html;die;
}
}
總結
以上是生活随笔為你收集整理的php 如何获取表格数据类型,使用phpword获取doc中的表格数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OSSIM下部署HIDS
- 下一篇: php object添加到数组,PHP