PHPExcel - 如何使部分文本加粗

2022-08-30 16:18:48

如何使用PHPExcel创建粗体单元格值?我知道我可以使用\n在文本中添加回车符,但是有没有办法加粗单元格值的一部分?我也尝试过使用html格式,如<b>或<strong>但它不起作用。


答案 1

您可以使用格式文字格式将单元格中的部分文本加粗,如开发人员文档的 4.6.37 节中所述。

$objRichText = new PHPExcel_RichText();
$objRichText->createText('This text is ');

$objBold = $objRichText->createTextRun('bold');
$objBold->getFont()->setBold(true);

$objRichText->createText(' within the cell.');

$objPHPExcel->getActiveSheet()->getCell('A18')->setValue($objRichText);

答案 2

是的,您可以使用以下代码加粗单元格的值:

$workbook = new PHPExcel;
$sheet = $workbook->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World');
$styleArray = array(
    'font' => array(
        'bold' => true
    )
);
$sheet->getStyle('A1')->applyFromArray($styleArray);
$writer = new PHPExcel_Writer_Excel5($workbook);
header('Content-type: application/vnd.ms-excel');
$writer->save('php://output');

希望这有帮助。


推荐