2

任何帮助表示赞赏..

我的代码

        /**
 *  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(cellnumber)-E(cellnember) 添加到 Q col 中的所有单元格中。和 =G(cellnumber)-E(cellnumber) 进入所有的 R col。对于存在的每一行。

提前致谢

4

1 回答 1

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');
于 2012-07-14T12:14:04.700 回答