PHPExcel - How to make part of the text bold

前端 未结 5 883
梦毁少年i
梦毁少年i 2020-12-13 19:43

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?

相关标签:
5条回答
  • 2020-12-13 20:13
    $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);
    
    0 讨论(0)
  • 2020-12-13 20:18

    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.

    $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);
    
    0 讨论(0)
  • 2020-12-13 20:22

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

    $htmlHelper = new \PHPExcel_Helper_HTML();
    $html = "<b> Bold Text!! </b>";
    $rich_text = $htmlHelper->toRichTextObject($html);
    $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $rich_text);
    
    0 讨论(0)
  • 2020-12-13 20:32

    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

    0 讨论(0)
  • 2020-12-13 20:32
    $objPHPExcel->getActiveSheet()->getStyle('1:1')->getFont()->setBold(true);
    
    0 讨论(0)
提交回复
热议问题