2014-02-07 38 views
0

如何在我的PHP代碼中將數組加粗並使用PHPExcel類將其保存在Excel文件中?我想也知道如何在保存文件的背景有顏色:我想要什麼在PHP代碼中使用PHPExcel加粗數組

例子:

enter image description here

我當前的代碼:

<?php 
if (!isset($_POST['send'])) { ?> 
    <!DOCTYPE html> 
    <html> 
    <head> 
    <title>test</title> 
    </head> 
    <body> 
<?php } else { 
    require_once 'C:\xampp\htdocs\test\Classes\PHPExcel\IOFactory.php'; 
    $filename = 'file.xlsx'; 
    $title = $_POST['title']; 
    mysql_connect("localhost","root","") or die ("cant connect!"); 
    mysql_select_db("test") or die ("cant find database!"); 

    $objReader = PHPExcel_IOFactory::createReader('Excel2007'); 
    $objReader->setReadDataOnly(true); 

    $objPHPExcel = $objReader->load($filename); 
    $objWorksheet = $objPHPExcel->getActiveSheet(); 
    $objWorksheet = $objPHPExcel->setActiveSheetIndex(0); 

    $result = mysql_query("SELECT * FROM score"); 
    if(isset($_POST['send'])){ 

     $headings = array(
      'ID', 
      'NAME', 
      'SCORE 1', 
      'SCORE 2', 
      'OTHER QUALITIES', 
      'INTERVIEW', 
      'TOTAL', 
      'AIC', 
      'BATCHCODE', 
     ); 
     $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true); 
     $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true); 
     $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true); 
     $objPHPExcel->getActiveSheet() 
      ->getStyle('A1:I5') 
      ->getAlignment() 
      ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
     $objPHPExcel->getActiveSheet()->fromArray($headings, null, 'A1'); 
     $row = 2; 
     while($rows = mysql_fetch_row($result)){ 
      $objPHPExcel->getActiveSheet()->fromArray($rows, null, 'A' . $row); 
      $row++; 
     } 
    } 

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); 
    header('Content-Disposition: attachment;filename="'.$title.'.xlsx"'); 
    header('Cache-Control: max-age=0'); 

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); 
    $objWriter->save('php://output'); 
} 
if (!isset($_POST['send'])) { ?> 

    <form id="form1" name="form1" method="post" action="" > 
    <input name="title" type="text" id="title" value="title" /> 
    <input type="submit" name="send" value="send to excel" id="send" /> 
    </form> 
    </body> 
    </html> 
<?php } 
+3

我真的開始懷疑我爲什麼懶得寫文檔,或提供示例代碼PHPExcel –

+0

@馬克·貝克[RüPHPexcel的創造者? + _ +如果'是'真棒:D – xplody

回答

2

設置如果從數組可能是最簡單的方法:

$objPHPExcel->getActiveSheet()->getStyle('A1:I1')->applyFromArray(
      array(
       'font' => array(
        'bold' => true 
       ), 
       'alignment' => array(
        'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER, 
       ), 
       'borders' => array(
        'top' => array(
         'style' => PHPExcel_Style_Border::BORDER_THIN 
        ) 
       ), 
       'fill' => array(
        'type' => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR, 
        'rotation' => 90, 
        'startcolor' => array(
         'argb' => 'FFA0A0A0' 
        ), 
        'endcolor' => array(
         'argb' => 'FFFFFFFF' 
        ) 
       ) 
      ) 
    ); 
1

只需更改單元格樣式即可獲取字體。

$objPHPExcel->getActiveSheet() 
      ->getStyle("$cell:$cell") 
      ->getFont() 
      ->setBold(true); 

或使用該功能的背景色

function cellColor($cells, $bgcolor, $color){ 
    global $objPHPExcel; 
    $objPHPExcel->getActiveSheet()->getStyle($cells)->getFill() 
    ->applyFromArray(array('type' => PHPExcel_Style_Fill::FILL_SOLID, 
    'startcolor' => array('rgb' => $bgcolor), 
    'endcolor' => array('rgb' => $color) 
    )); 
} 
0

試試吧!

<?php 
if (!isset($_POST['send'])) { ?> 
    <!DOCTYPE html> 
    <html> 
    <head> 
    <title>test</title> 
    </head> 
    <body> 
<?php } else { 
    require_once 'C:\xampp\htdocs\test\Classes\PHPExcel\IOFactory.php'; 
    $filename = 'file.xlsx'; 
    $title = $_POST['title']; 
    mysql_connect("localhost","root","") or die ("cant connect!"); 
    mysql_select_db("test") or die ("cant find database!"); 

    $objReader = PHPExcel_IOFactory::createReader('Excel2007'); 
    $objReader->setReadDataOnly(true); 

    $objPHPExcel = $objReader->load($filename); 
    $objWorksheet = $objPHPExcel->getActiveSheet(); 
    $objWorksheet = $objPHPExcel->setActiveSheetIndex(0); 

    $result = mysql_query("SELECT * FROM score"); 
    if(isset($_POST['send'])){ 

     $headings = array(
      'ID', 
      'NAME', 
      'SCORE 1', 
      'SCORE 2', 
      'OTHER QUALITIES', 
      'INTERVIEW', 
      'TOTAL', 
      'AIC', 
      'BATCHCODE', 
     ); 
     $objPHPExcel->getActiveSheet()->getStyle('A1:I5')->getFont()->setBold(true); 
     $objPHPExcel->getActiveSheet()->getStyle('A1:I5')->applyFromArray(array('fill' => array('type' => PHPExcel_Style_Fill::FILL_SOLID, 'color' => array('rgb' => 'CCCCCC')),)); 
     $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true); 
     $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true); 
     $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true); 
     $objPHPExcel->getActiveSheet() 
      ->getStyle('A1:I5') 
      ->getAlignment() 
      ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
     $objPHPExcel->getActiveSheet()->fromArray($headings, null, 'A1'); 
     $row = 2; 
     while($rows = mysql_fetch_row($result)){ 
      $objPHPExcel->getActiveSheet()->fromArray($rows, null, 'A' . $row); 
      $row++; 
     } 
    } 

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); 
    header('Content-Disposition: attachment;filename="'.$title.'.xlsx"'); 
    header('Cache-Control: max-age=0'); 

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); 
    $objWriter->save('php://output'); 
} 
if (!isset($_POST['send'])) { ?> 

    <form id="form1" name="form1" method="post" action="" > 
    <input name="title" type="text" id="title" value="title" /> 
    <input type="submit" name="send" value="send to excel" id="send" /> 
    </form> 
    </body> 
    </html> 
<?php }