How to center the text in PHPExcel merged cell

前端 未结 5 1085
花落未央
花落未央 2020-12-23 13:16

How to center text \"test\"?

This is my code:



        
相关标签:
5条回答
  • 2020-12-23 13:51
    <?php
        /** Error reporting */
        error_reporting(E_ALL);
        ini_set('display_errors', TRUE);
        ini_set('display_startup_errors', TRUE);
        date_default_timezone_set('Europe/London');
    
        /** Include PHPExcel */
        require_once '../Classes/PHPExcel.php';
    
        $objPHPExcel = new PHPExcel();
        $sheet = $objPHPExcel->getActiveSheet();
        $sheet->setCellValueByColumnAndRow(0, 1, "test");
        $sheet->mergeCells('A1:B1');
        $sheet->getActiveSheet()->getStyle('A1:B1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save("test.xlsx");
    ?>
    
    0 讨论(0)
  • 2020-12-23 13:58

    if you want to align only this cells, you can do something like this:

        $style = array(
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
            )
        );
    
        $sheet->getStyle("A1:B1")->applyFromArray($style);
    

    But, if you want to apply this style to all cells, try this:

        $style = array(
            'alignment' => array(
                'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
            )
        );
    
        $sheet->getDefaultStyle()->applyFromArray($style);
    
    0 讨论(0)
  • 2020-12-23 13:59

    We can also set the vertical alignment with using this way

    $style_cell = array(
       'alignment' => array(
           'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
           'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
       ) 
    ); 
    

    with this cell set the vertically aligned into the middle.

    0 讨论(0)
  • 2020-12-23 14:03

    The solution is to set the cell style through this function:

    $sheet->getStyle('A1')->getAlignment()->applyFromArray(
        array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,)
    );
    

    Full code

    <?php
    /** Error reporting */
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    date_default_timezone_set('Europe/London');
    
    /** Include PHPExcel */
    require_once '../Classes/PHPExcel.php';
    
    $objPHPExcel = new PHPExcel();
    $sheet = $objPHPExcel->getActiveSheet();
    $sheet->setCellValueByColumnAndRow(0, 1, "test");
    $sheet->mergeCells('A1:B1');
    $sheet->getStyle('A1')->getAlignment()->applyFromArray(
        array('horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,)
    );
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save("test.xlsx");
    

    enter image description here

    0 讨论(0)
  • 2020-12-23 14:11

    When using merged columns, I got it centered by using PHPExcel_Style_Alignment::HORIZONTAL_CENTER_CONTINUOUS instead of PHPExcel_Style_Alignment::HORIZONTAL_CENTER

    0 讨论(0)
提交回复
热议问题