php PHPExcel - 如何使部分文本加粗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8045056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:50:28  来源:igfitidea点击:

PHPExcel - How to make part of the text bold

phpphpexcel

提问by Ahmad Satiri

How do you create a bold cell value using PHPExcel? I know I can use \n to add a carriage return within the text, but is there some kind of way to bold part of cell value? I also have tried using html formatting such as <b> or <strong> but it did not work.

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

回答by Mark Baker

You can bold part of the text in a cell using rich text formatting, as described in section 4.6.37 of the developer documentation.

您可以使用富文本格式将单元格中的部分文本加粗,如开发人员文档的第 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);

回答by Eric LaForce

Yes you can bold a cell's value with the following code:

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

$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');

Hope this helps.

希望这可以帮助。

Source

来源

回答by Ashwin

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Hello')
            ->setCellValue('B1', 'world!')
            ->setCellValue('C1', 'Hello')
            ->setCellValue('D1', 'world!');

for single cell use:

单细胞使用:

$objPHPExcel->getActiveSheet()->getStyle("A1")->getFont()->setBold(true);

for multiple cells use:

对于多个单元格使用:

$objPHPExcel->getActiveSheet()->getStyle("A1:D1")->getFont()->setBold(true);

回答by Syam

You can use the HTML helper class in PHPExcel to bold text.

您可以使用 PHPExcel 中的 HTML 帮助器类来加粗文本。

$htmlHelper = new \PHPExcel_Helper_HTML();
$html = "<b> Bold Text!! </b>";
$rich_text = $htmlHelper->toRichTextObject($html);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $rich_text);

回答by Nishant

$objPHPExcel->getActiveSheet()->getStyle('1:1')->getFont()->setBold(true);