如何使用setBold来处理动态范围的单元格?有时候可能有10列,有时甚至是50.我不能使用像A1:A40这样的范围,因为我从来不知道确切的数量。
由于
答案 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;
}