2011-11-15 21 views
1

如何使用setBold工作aith動態範圍的單元格?有時可能有10列,有時可能是50列。我不能使用A1:A40這樣的範圍,因爲我不知道確切的數量。動態範圍中的PPHExcel粗體文本

感謝

+0

你需要跟蹤你正在使用某種方式的單元格區域的 –

回答

0
$outputFileType = 'Excel2007'; 
$outputFileName = 'testWrite.xlsx'; 

/** Include path **/ 
set_include_path(get_include_path() . PATH_SEPARATOR . './Classes/'); 

/** PHPExcel_IOFactory */ 
include 'PHPExcel/IOFactory.php'; 

// Instantiate a new PHPExcel object and set some data 
$objPHPExcel = new PHPExcel(); 
$cellData = magicSquare(4); 
$objPHPExcel->getActiveSheet()->fromArray($cellData); 

// Define a style to set 
$styleArray = array('font' => array('bold' => true, 
            ) 
        ); 
// And a range of cells to set it for 
$fromCol = 'A'; 
$toCol = 'C'; 
$fromRow = 1; 
$toRow = 3; 
$cellRange = $fromCol . $fromRow . ':' . $toCol . $toRow; 

$objPHPExcel->getActiveSheet()->getStyle($cellRange)->applyFromArray($styleArray); 

// Save the workbook 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $outputFileType); 
$objWriter->save($outputFileName); 


function magicSquare($size) { 
    $row = 1; 
    $column = ceil($size/2); 
    $result = array(); 
    $valueCount = $size*$size; 

    for ($i = 1; $i <= $valueCount; $i++) { 
     $result[$row][$column] = $i; 

     if (($i % $size) == 0) { 
      $row++; 
     } else { 
      $row--; 
      $column++; 
     } 

     if ($row <= 0) { 
      $row = $size; 
     } elseif($row > $size) { 
      $row = 1; 
     } 
     if ($column <= 0) { 
      $column = $size; 
     } elseif ($column > $size) { 
      $column = 1; 
     } 
    } 

    ksort($result); 
    foreach($result as &$row) { 
     ksort($row); 
    } 

    return $result; 
}