phpphpexcel

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

Solution

  • 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);