imperium2335
imperium2335

Reputation: 24112

How to Apply Borders to All Cells, Not a Range!

I have got my Excel reader to work but there is an annoying problem where I can only find information on how to apply a border to a range of cells, which is useless for my application.

Users upload Excel sheets that can be viewed later on. So the cell range method won't work since the range changes.

Is there not a default parameter to set all cell styles etc?

Here is what I have:

require_once ROOT . '/libs/PHPExcel/IOFactory.php';
      $excel = PHPExcel_IOFactory::load('../public_html/uploads/' . $filename);
      PHPExcel_Shared_Font::setAutoSizeMethod(PHPExcel_Shared_Font::AUTOSIZE_METHOD_EXACT);
      $writer = PHPExcel_IOFactory::createWriter($excel, 'HTML');

      $writer->setUseInlineCSS(true);
      $styleArray = array(
          'borders' => array(
              'allborders' => array(
                  'style' => PHPExcel_Style_Border::BORDER_THIN
              )
          )
      );

      $excel->getActiveSheet()->getStyle('A1:B1')->applyFromArray($styleArray);
      unset($styleArray);
      $writer->save('uploads/excel-sheet.html');
      header('Location: ' . WROOT . 'uploads/excel-sheet.html');

Upvotes: 7

Views: 36549

Answers (2)

ilham76c
ilham76c

Reputation: 71

To apply border style to entire cell in sheet, you can do with this code

$objPHPExcel = new \PHPExcel();

$objPHPExcel->getActiveSheet()->getDefaultStyle()->applyFromArray(
    array(
        'borders' => array(
            'allborders' => array(
                'style' => \PHPExcel_Style_Border::BORDER_THIN,
                'color' => array('rgb' => 'FFFFFF')
            )
        )
    )
);

or use this code

$objPHPExcel->getActiveSheet()->getDefaultStyle()
    ->getBorders()
    ->getTop()
    ->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN)
    ->setColor(new \PHPExcel_Style_Color('FFFFFF'));
$objPHPExcel->getActiveSheet()->getDefaultStyle()
    ->getBorders()
    ->getBottom()
    ->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN)
    ->setColor(new \PHPExcel_Style_Color('FFFFFF'));
$objPHPExcel->getActiveSheet()->getDefaultStyle()
    ->getBorders()
    ->getLeft()
    ->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN)
    ->setColor(new \PHPExcel_Style_Color('FFFFFF'));
$objPHPExcel->getActiveSheet()->getDefaultStyle()
    ->getBorders()
    ->getRight()
    ->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN)
    ->setColor(new \PHPExcel_Style_Color('FFFFFF'));

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212402

Set a default style that will apply to the whole workbook

$excel->getDefaultStyle()->applyFromArray($styleArray);

Though you should be able to read the range for any worksheet that has been loaded, and so set the style for that range

$excel->getActiveSheet()->getStyle(
    'A1:' . 
    $excel->getActiveSheet()->getHighestColumn() . 
    $excel->getActiveSheet()->getHighestRow()
)->applyFromArray($styleArray);

Upvotes: 27

Related Questions