Excel 使用 PHPExcel 格式的 HH:MM 总时数计算,HH 大于 24 小时

Excel Total Hours Calculation using PHPExcel in HH:MM format with HH greater than 24 hours

我想使用 PHPExcel 绘制 Excel 中的总小时数,它不仅仅受 24 小时格式的影响,即可以有大于 24 小时的小时数,例如。 49:20.

目前我能够以低于 24 小时制的格式绘制和显示小时数。以及显示 excel 中所有小时的总和。即参考下面的屏幕截图。

但我无法绘制大于 24 小时的时间。在上面的屏幕截图中,我使用了下面的脚本来绘制数据。

A 列和 B 列绘制了所需数据,但它们是字符串格式,无法在对 Excel 中的单元格值求和时使用。

下面的 A 列是示例代码:

$objPHPExcel->getActiveSheet()->setCellValue('A'.$row_count, $time);
$objPHPExcel->getActiveSheet()->getStyle('A1:A'.$row_count)->getNumberFormat()->setFormatCode("[h]:mm:ss");

下面的 B 列是示例代码:

$objPHPExcel->getActiveSheet()->setCellValue('B'.$row_count,$time);
$objPHPExcel->getActiveSheet()->getStyle('B1:B'.$row_count)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIMEMK);

C 列在 24:00 下方绘制时间,我什至可以对 Excel 中的值求和。但是我无法绘制时间大于 24:00.

的时间

C列示例代码:

$objPHPExcel->setActiveSheetIndex()->setCellValue('C'.$row_count, PHPExcel_Calculation_DateTime::TIMEVALUE($time));
$objPHPExcel->getActiveSheet()->getStyle('C1:C'.$row_count)->getNumberFormat()->setFormatCode('[HH]:MM');

下面的代码生成了上面提到的屏幕截图的报告。

require('PHPExcel.php');
//ini_set('memory_limit', '1024M');
require_once 'PHPExcel/Cell/AdvancedValueBinder.php';
$filename ="test.xlsx";
include 'Classes/PHPExcel/IOFactory.php';
include 'PHPExcel/Cell.php';
include 'PHPExcel/Style/NumberFormat.php';
$inputFileType = 'Excel2007';



ob_end_clean();
// create new PHPExcel object
$objPHPExcel = new PHPExcel;

$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
                 ->setLastModifiedBy("Maarten Balliauw")
                 ->setTitle("Office 2007 XLSX Test Document")
                 ->setSubject("Office 2007 XLSX Test Document")
                 ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                 ->setKeywords("office 2007 openxml php")
                 ->setCategory("Test result file");

$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);

$time_array = Array('20:40','22:40','24:00','24:60','50:60','256:60');
    $row_count=1;
    foreach( $time_array as $time )
    {
        $objPHPExcel->getActiveSheet()->setCellValue('A'.$row_count, $time);
        $objPHPExcel->getActiveSheet()->setCellValue('B'.$row_count,$time);
        $objPHPExcel->setActiveSheetIndex()->setCellValue('C'.$row_count, PHPExcel_Calculation_DateTime::TIMEVALUE($time));
        $row_count++;
    }
    $objPHPExcel->getActiveSheet()->getStyle('A1:A'.$row_count)->getNumberFormat()->setFormatCode("[h]:mm:ss");
    $objPHPExcel->getActiveSheet()->getStyle('B1:B'.$row_count)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_TIMEMK);
    $objPHPExcel->getActiveSheet()->getStyle('C1:C'.$row_count)->getNumberFormat()->setFormatCode('[HH]:MM');


ob_end_clean();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");

$objPHPExcel->getSheet(0)->setTitle('Sheet');
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=".$filename.""); 
header("Content-Transfer-Encoding: binary ");
$objWriter->save('php://output');

Note : const FORMAT_DATE_TIMEMK = 'hhh:mm:ss';

提前致谢。

  1. 屏幕截图看起来像是您在 Ububntu 上使用 OpenOffice 而不是 excel。我想您可以查看原始办公包 - 它可能略有不同。

  2. 为什么不在 php 中 formatting/calculating 然后将文本传递到单元格?

  3. 所有 3 列都有不同的格式。这是什么原因?

我能够通过对脚本逻辑进行一些更改来生成所需的报告。在了解了计算超过 24 小时的时间的 Excel 函数之后,我们需要使用 Excel 公式来计算时间,即 =(HOUR/24+TIME(0,MINUTES,0 ).

下面是示例脚本

require('PHPExcel.php');
//ini_set('memory_limit', '1024M');
require_once 'PHPExcel/Cell/AdvancedValueBinder.php';
$filename ="test.xls";
include 'Classes/PHPExcel/IOFactory.php';
include 'PHPExcel/Cell.php';
include 'PHPExcel/Style/NumberFormat.php';
$inputFileType = 'Excel5';



ob_end_clean();
// create new PHPExcel object
$objPHPExcel = new PHPExcel;

$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
                 ->setLastModifiedBy("Maarten Balliauw")
                 ->setTitle("Office 2007 XLSX Test Document")
                 ->setSubject("Office 2007 XLSX Test Document")
                 ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                 ->setKeywords("office 2007 openxml php")
                 ->setCategory("Test result file");

$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$row_count=1;
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row_count, "Hours");
$objPHPExcel->getActiveSheet()->setCellValue('B'.$row_count,"Minutes");
$objPHPExcel->getActiveSheet()->setCellValue('C'.$row_count,"Time");
$row_count++;
$time_array = Array('20:40','22:40','24:00','24:60','50:60','256:60');

    foreach( $time_array as $time )
    {
        $time_slice = Array();
        $time_slice = explode(":",$time);
        $objPHPExcel->getActiveSheet()->setCellValue('A'.$row_count, $time_slice[0]);
        $objPHPExcel->getActiveSheet()->setCellValue('B'.$row_count,$time_slice[1]);
        // $objPHPExcel->getActiveSheet()->setCellValue('C'.$row_count,$time_slice[1]);
        $objPHPExcel->getActiveSheet()->setCellValue('C'.$row_count, '=(A'.$row_count.'/24+TIME(0,B'.$row_count.',0))');//Fixed 
        $objPHPExcel->getActiveSheet()->setCellValue('D'.$row_count, '=('.$time_slice[0].'/24+TIME(0,'.$time_slice[1].',0))');//Fixed 
        $row_count++;
    }
    // $objPHPExcel->getActiveSheet()->setCellValue('C2', "=(A2/24+TIME(,B2,0))");//Fixed

    $objPHPExcel->getActiveSheet()->getStyle('C2:C'.$row_count)->getNumberFormat()->setFormatCode("[h]:mm:ss");
    $objPHPExcel->getActiveSheet()->getStyle('D2:D'.$row_count)->getNumberFormat()->setFormatCode("[h]:mm:ss");


ob_end_clean();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel5");

$objPHPExcel->getSheet(0)->setTitle('Sheet');
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=".$filename.""); 
header("Content-Transfer-Encoding: binary ");
$objWriter -> setPreCalculateFormulas(true);
$objWriter->save('php://output');

Excel 功能逻辑 link : https://theexceltrainer.co.uk/excels-10000-hour-limitation/