PHPExcel为电子表格中的所有工作表设置边框和格式 [英] PHPExcel set border and format for all sheets in spreadsheet

查看:795
本文介绍了PHPExcel为电子表格中的所有工作表设置边框和格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试为电子表格设置所有边框,还设置诸如自动调整大小之类的格式.

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

我的以下代码在工作表1上正常工作.电子表格中的所有其他工作表都未触及.我一直在尝试使其能够与该电子表格中的所有其他工作表一起使用,但是没有运气.

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.

关于如何全局设置格式以使所有工作表都有边框和自动调整大小的任何想法吗?该电子表格中所有工作表的布局均相同.我正在导出到XLSX文件.

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.

干杯

/**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);

其他问题: 我目前已设定标题.标题确实出现在工作表1上,但没有出现在其他任何工作表上..是否可以在所有工作表中显示标题? 标题设置在第1行中,结果从第2行开始向下.

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

推荐答案

您可以为整个工作簿(所有工作表)设置默认样式:

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

  $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.

但是列自动调整大小是structural而不是stylistic,必须为每个工作表上的每个列分别设置.

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

编辑

请注意,默认工作簿样式仅适用于Excel5 Writer

Note that default workbook style only applies to Excel5 Writer

这篇关于PHPExcel为电子表格中的所有工作表设置边框和格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆