使用PHPExcel在Windows或Mac上是否生成了exce

使用PHPExcel在Windows或Mac上是否生成了exce

本文介绍了有没有一种方法可以检测使用PHPExcel在Windows或Mac上是否生成了excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHPExcel生成一个xls模板,用户可以下载该模板并用他想要的数据填充它.众所周知,excel以数字格式保存日期.我正在使用此函数来转换数据并返回时间戳:

I'm using PHPExcel to generate a xls template that the user can download and fill it with the data he wants.As we know, excel saves the date in a numeric format. I'm using this function to convert the data and return the timestamp:

public static function excelToTimestamp($excelDateTime, $isMacExcel=false) {
    $myExcelBaseDate = $isMacExcel ? 24107 : 25569; // 1st jan 1904 or 1st jan 1900
    if (!$isMacExcel && $excelDateTime < 60) {
        //  Adjust for the spurious 29-Feb-1900 (Day 60)
        --$myExcelBaseDate;
    }
    // Perform conversion
    if ($excelDateTime >= 1) {
        $timestampDays = $excelDateTime - $myExcelBaseDate;
        $timestamp = round($timestampDays * 86400);
        if (($timestamp <= PHP_INT_MAX) && ($timestamp >= -PHP_INT_MAX)) {
            $timestamp = intval($timestamp);
        }
    } else {
        $hours = round($excelDateTime * 24);
        $mins = round($excelDateTime * 1440) - round($hours * 60);
        $secs = round($excelDateTime * 86400) - round($hours * 3600) - round($mins * 60);
        $timestamp = (integer) gmmktime($hours, $mins, $secs);
    }
    return $timestamp;
}

问题是我必须检测用户导入到系统的文件是否是使用Mac或Windows的excel填充的,以便我可以正确设置日期(mac使用1904年日历,而Windows使用1900年).

The problem is that I have to detect if the file that the user imported to the system was filled using excel for mac or windows, so that I can set the date correctly (mac uses 1904 calendar, while windows uses 1900).

我想知道是否可以使用PHPExcel检测到它.如果不是,我可以让用户通过单选按钮通知它,也许...

推荐答案

正如@markBaker所建议的,我只是使用PHPExcel函数来转换日期和时间来解决此问题,

I just solved this problem using, as @markBaker suggested, the PHPExcel function to convert the date and time, doing this:

 foreach ($rowLine as $header => $col) {
        if ($header == self::COLUMN_DATE) {
            //transform the excel date value into a datetime object
            $date = PHPExcel_Shared_Date::ExcelToPHPObject($sheetData[$row][$col]);
            $rowLine[$header] = $date->format('m/d/Y');
        }else if ($header == self::COLUMN_HOUR) {
            //transform the excel time value into a datetime object
            $time = PHPExcel_Shared_Date::ExcelToPHPObject($sheetData[$row][$col]);
            $rowLine[$header] = $time->format('H:i');
        }else{
            $rowLine[$header] = $sheetData[$row][$col];
        }
 }

这篇关于有没有一种方法可以检测使用PHPExcel在Windows或Mac上是否生成了excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 18:42