根据行数在每个单元格中插入公式

时间:2012-07-14 10:38:57

标签: phpexcel

任何帮助表示赞赏..

我的代码

        /**
 *  Admin Excel Export Function().
 */

function work_record_export_admin(){


require('inc/php-excel.class.php');



    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getActiveSheet()->setTitle('Work Record');

    $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Name')
            ->setCellValue('B1', 'Region')
            ->setCellValue('C1', 'Role')
            ->setCellValue('D1', 'Date')
            ->setCellValue('E1', 'Start Travel')
            ->setCellValue('F1', 'Start Job')
            ->setCellValue('G1', 'Finish Job')
            ->setCellValue('H1', 'Site ID')
            ->setCellValue('I1', 'Site Name')
            ->setCellValue('J1', 'Site Town')
            ->setCellValue('K1', 'Fault Description')
            ->setCellValue('L1', 'Work Summary')
        ->setCellValue('M1', 'Customer Type')
            ->setCellValue('N1', 'Business')
            ->setCellValue('O1', 'Work Code (depot only)')
            ->setCellValue('P1', 'Quantity (depot only)')
        ->setCellValue('Q1', 'Travel Time')
            ->setCellValue('R1', 'Job Time');

 $result = db_query("SELECT name, region, role, date, startt, startj, finj, siteid, sitename, sitetown, faultdesc, worksum, buscode, workcode, quantity FROM work_record_grid ") or die('Query failed!'); 

  // Loop through the result set
    $rowNumber = 2;
    while ($row = db_fetch_array($result)) {
       $col = 'A';
       foreach($row as $cell) {
          $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
          $col++;
       }
       $rowNumber++;
    }

    // Freeze pane so that the heading line won't scroll
    $objPHPExcel->getActiveSheet()->freezePane('A2');


$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(17);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(19);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(17);
$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(17);
$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(14);
$objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(21);
$objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(8);
$objPHPExcel->getActiveSheet()->getColumnDimension('Q')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('R')->setWidth(10);
$objPHPExcel->getActiveSheet()->getStyle('Q'.$row)->getNumberFormat()->setFormatCode('[h]:mm');

$objPHPExcel->getActiveSheet()->getStyle('R'.$row)->getNumberFormat()->setFormatCode('[h]:mm');
//$objPHPExcel->getActiveSheet()->setCellValue('Q'.$row, '=(F'.$row.'-E'.$row.')');
//$objPHPExcel->getActiveSheet()->setCellValue(R'.$row, '=(G'.$row.'-F'.$row')');

    // Save as an Excel BIFF (xls) file
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

   header('Content-Type: application/vnd.ms-excel');
   header('Content-Disposition: attachment;filename="work_record_data' . date('Ymd') . '.xls"');
   header('Cache-Control: max-age=0');
   $objWriter->save('php://output');
   exit();

 }

我正在尝试将公式= F(单元格数字)-E(cellnember)添加到Q col中的所有单元格中。并且= G(单元号)-E(单元号)进入所有R col。对于存在的每一行。

提前致谢

1 个答案:

答案 0 :(得分:1)

在你的行循环中(一旦你填充了所有列):

$objPHPExcel->getActiveSheet()
    ->setCellValue( 'Q'.$rowNumber,
                    '=F'.$rowNumber.'-E'.$rowNumber
                  );

和单元格R. $ rowNumber

的等价物

这样:

while ($row = db_fetch_array($result)) {
   $col = 'A';
   foreach($row as $cell) {
      $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
      $col++;
   }
    $objPHPExcel->getActiveSheet()
        ->setCellValue( 'Q'.$rowNumber,
                        '=F'.$rowNumber.'-E'.$rowNumber
                      );
   $rowNumber++;
}

修改

使用范围为所有适当的单元格设置样式:

$objPHPExcel->getActiveSheet()
    ->getStyle('Q2:Q'.$rowNumber)
    ->getNumberFormat()
    ->setFormatCode('[h]:mm');