gpt4 book ai didi

php - 有没有办法使用 PHPExcel 检测 excel 文件是在 windows 还是 mac 上生成的?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:39:17 25 4
gpt4 key购买 nike

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

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

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

我想知道是否有办法使用 PHPExcel 检测它。如果没有,我可能会让用户用单选按钮通知它,也许...

最佳答案

我刚刚解决了这个问题,正如@markBaker 所建议的,使用 PHPExcel 函数转换日期和时间,这样做:

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

关于php - 有没有办法使用 PHPExcel 检测 excel 文件是在 windows 还是 mac 上生成的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12246116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com