我正在尝试在 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,但它不起作用。我的桌子没有边框
它的事件无法通过添加样式来工作。请帮忙
尝试以最小的更改和所有工作来运行您的代码)
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');
在这种情况下使用内联 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');
添加 PHPWord/Shared/Html.php, 函数parseInlineStyle, 在案例部分添加
case 'border':
$styles['borderSize'] = (int) $val;
$styles['borderStyle'] = 'solid';
$styles['borderColor'] = '#000000';
break;