Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
777 views
in Technique[技术] by (71.8m points)

php - PHPExcel set border and format for all sheets in spreadsheet

I'm currently trying to set all borders for my spreadsheet, also formatting such as autosize.

My code below is working, for sheet 1. All other sheets inside the spreadsheet are completely untouched. I've been trying to get it to work with all other sheets inside this spreadsheet but with no luck.

Any ideas on how i can global set the formatting so that all sheets have borders and autosize? The layout of all sheets in this spreadsheet are all the same. I'm exporting to XLSX file.

Cheers,

/**autosize*/
for ($col = 'A'; $col != 'P'; $col++) {
    $objPHPExcel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);
}


/** Borders for all data */
   $objPHPExcel->getActiveSheet()->getStyle(
    'A2:' . 
    $objPHPExcel->getActiveSheet()->getHighestColumn() . 
    $objPHPExcel->getActiveSheet()->getHighestRow()
)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);



/** Borders for heading */
   $objPHPExcel->getActiveSheet()->getStyle(
    'A1:O1'
)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);

EXTRA QUESTION: I've currently set headings. The headings do appear on sheet 1, but do not appear in any other sheets.. is it possible to show the headings in all sheets? The headings are set in Row 1. and the results are from Row 2 down.

$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Asset_id');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'Asset_name');
$objPHPExcel->getActiveSheet()->setCellValue('C1', 'Asset_type');
$objPHPExcel->getActiveSheet()->setCellValue('D1', 'Asset_make');
$objPHPExcel->getActiveSheet()->setCellValue('E1', 'Asset_model');
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can set a default style for the entire workbook (all worksheets):

$objPHPExcel->getDefaultStyle()
    ->getBorders()
    ->getTop()
        ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getDefaultStyle()
    ->getBorders()
    ->getBottom()
        ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getDefaultStyle()
    ->getBorders()
    ->getLeft()
        ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getDefaultStyle()
    ->getBorders()
    ->getRight()
        ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

or

  $styleArray = array(
      'borders' => array(
          'allborders' => array(
              'style' => PHPExcel_Style_Border::BORDER_THIN
          )
      )
  );
$objPHPExcel->getDefaultStyle()->applyFromArray($styleArray);

And this can be used for all style properties, not just borders.

But column autosizing is structural rather than stylistic, and has to be set for each column on each worksheet individually.

EDIT

Note that default workbook style only applies to Excel5 Writer


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...