简体   繁体   English

phpExcel xls 到 csv 的转换正在将日期更改为原始数据

[英]phpExcel xls to csv conversion is changing the date to raw

my function to convert xls file to csv using phpExcel is changing the value in the date field to raw.我使用 phpExcel 将 xls 文件转换为 csv 的函数正在将日期字段中的值更改为原始值。 How do I avoid this from happening?我如何避免这种情况发生?

The function is as below功能如下

function convertXLStoCSV($infile,$outfile)
{
    $fileType = PHPExcel_IOFactory::identify($infile);
    $objReader = PHPExcel_IOFactory::createReader($fileType);

    $objReader->setReadDataOnly(true);   
    $objPHPExcel = $objReader->load($infile);    

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
    $objWriter->save($outfile);
}

The date in the xls file is of the format: 16.01.2017 00:00:00 xls 文件中的日期格式为:16.01.2017 00:00:00
The output csv file has the date in the format: 42751.0104166667输出 csv 文件的日期格式为:42751.0104166667

I have tried to change in file /Classes/PHPExcel/Style/NumberFormat.php我试图改变文件 /Classes/PHPExcel/Style/NumberFormat.php

        self::$_builtInFormats[22] = 'm/d/yy h:mm'; 
        changed to 
        self::$_builtInFormats[22] = 'mm.dd.yyyy hh:mm:ss';

But this is not doing the magic但这并没有变魔术

Your problem is你的问题是

$objReader->setReadDataOnly(true);  

which is explicitly telling PHPExcel to read the cell data as raw data, without any of the styling information (such as number format masking).这明确告诉 PHPExcel 将单元格数据作为原始数据读取,没有任何样式信息(例如数字格式屏蔽)。

Simply comment out that line只需注释掉该行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM