Is there a way to detect if an excel file was generated on windows or mac using PHPExcel?

岁酱吖の 提交于 2019-12-10 14:34:27

问题


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;
}

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).

I'd like to know if is there a way to detect it using PHPExcel. If not, I may let the user informs it with a radiobutton, maybe...


回答1:


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];
        }
 }


来源:https://stackoverflow.com/questions/12246116/is-there-a-way-to-detect-if-an-excel-file-was-generated-on-windows-or-mac-using

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!