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

时间:2018-02-13 09:40:59

标签: php excel time phpexcel phpexcelreader

我想使用PHPExcel在Excel中绘制总小时数,这不仅仅是24小时格式,即可能有大于24小时的小时数,例如。 49:20。

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

enter image description here

但是我无法绘制时间超过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');
  

注意:const FORMAT_DATE_TIMEMK =' hhh:mm:ss&#39 ;;

提前谢谢。

2 个答案:

答案 0 :(得分:0)

  1. 屏幕截图看起来就像你在Ububntu上使用OpenOffice而不是excel。我想你可以查看原来的办公室套餐 - 可能会略有不同。

  2. 为什么不在php中格式化/计算然后只是将文本传递给单元格?

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

答案 1 :(得分:0)

我能够通过在脚本逻辑中进行一些更改来生成所需的报告。在理解用于计算超过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功能逻辑链接:https://theexceltrainer.co.uk/excels-10000-hour-limitation/