PHPExcel 使第一行加粗

2022-08-30 08:03:46

我试图使第一行中的单元格是粗体的。

这是我为此目的创建的方法。

function ExportToExcel($tittles,$excel_name)
 {
  $objPHPExcel = new PHPExcel();
  $objRichText = new PHPExcel_RichText();
  // Set properties
  $objPHPExcel->getProperties()->setCreator("SAMPLE1");
  $objPHPExcel->getProperties()->setLastModifiedBy("SAMPLE1");
  $objPHPExcel->getProperties()->setTitle("SAMPLE1");
  $objPHPExcel->getProperties()->setSubject("SAMPLE1");
  $objPHPExcel->getProperties()->setDescription("SAMPLE1");


  // Add some data
  $objPHPExcel->setActiveSheetIndex(0);

  $letters = range('A','Z');
  $count =0;
  $cell_name="";
  foreach($tittles as $tittle)
  {
   $cell_name = $letters[$count]."1";
   $count++;
   $value = $tittle;
   $objPHPExcel->getActiveSheet()->SetCellValue($cell_name, $value);
   // Make bold cells
   $objPHPExcel->getActiveSheet()->getStyle($cell_name)->getFont()->setBold(true);
  }
  // Save Excel 2007 file
  $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  //$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
  $objWriter->save($excel_name.".xlsx");
 }

问题是在输出excel文件中,单元格不是粗体。


答案 1

尝试对单元格范围进行此操作:

$from = "A1"; // or any value
$to = "B5"; // or any value
$objPHPExcel->getActiveSheet()->getStyle("$from:$to")->getFont()->setBold( true );

或单细胞

$cell_name = "A1";
$objPHPExcel->getActiveSheet()->getStyle( $cell_name )->getFont()->setBold( true );

希望有所帮助


答案 2

试试这个

$objPHPExcel->getActiveSheet()->getStyle('A1:D1')->getFont()->setBold(true);

推荐