无法在表格中添加边框样式(phpword)

问题描述 投票:0回答:3

我正在尝试在 html 内的表格中添加边框。下面是我的代码

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$html = "<table border='1'>
<tr>
<th>name</th>
</tr>
<tr><td>John</td></tr>
</table>";
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html );
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;filename="test.docx"');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('php://output');

我已指定边框为 1,但它不起作用。我的桌子没有边框

它的事件无法通过添加样式来工作。请帮忙

php css phpword
3个回答
0
投票

尝试以最小的更改和所有工作来运行您的代码)

require_once __DIR__ . '/vendor/autoload.php';

use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\Html;

$phpWord = new PhpWord();
$section = $phpWord->addSection();
$html = "<table border='1'>
<tr>
<th>name</th>
</tr>
<tr><td>John</td></tr>
</table>";
Html::addHtml($section, $html);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;filename="test.docx"');
$objWriter = IOFactory::createWriter($phpWord);
$objWriter->save('php://output');

结果截图


0
投票

在这种情况下使用内联 CSS。你应该这样做:

require_once __DIR__ . '/vendor/autoload.php';

use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\Html;

$phpWord = new PhpWord();
$section = $phpWord->addSection();
$html = "<table style="border: 1px solid black;">
<tr>
<th>name</th>
</tr>
<tr><td>John</td></tr>
</table>";
Html::addHtml($section, $html);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;filename="test.docx"');
$objWriter = IOFactory::createWriter($phpWord);
$objWriter->save('php://output');

0
投票

添加 PHPWord/Shared/Html.php, 函数parseInlineStyle, 在案例部分添加

case 'border':
    $styles['borderSize'] = (int) $val;
    $styles['borderStyle'] = 'solid';
    $styles['borderColor'] = '#000000';
 break;
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.