doxd96148 2015-04-13 20:24
浏览 41
已采纳

PHP值日期xlsx到csv

I have an XLSX file with some cells that has a date value like: 13/04/2015 when I convert my XLSX to CSV the date value in my CSV becomes its value as: 42107

I need that the value be the same in both files like in XLSX (13/04/2015)

Here's my convert script

include ('./PHPExcel/Classes/PHPExcel.php');
$TypeFile="Excel2007";
$FilePath= "./assets/uploads/files/prf.xlsx";
$objReader = PHPExcel_IOFactory::createReader($TypeFile);
$objReader->setReadDataOnly(true);   
$objExcel = $objReader->load($FilePath);
$objCSV = PHPExcel_IOFactory::createWriter($objExcel, 'CSV');
$objCSV->setPreCalculateFormulas(false);
$objCSV->setDelimiter(','); 
$objCSV->setEnclosure('"');    
$objCSV->save('./assets/uploads/files/prf.csv');
  • 写回答

1条回答 默认 最新

  • douduiwei2831 2015-04-13 20:29
    关注

    Yes, it will

    A date in MS Excel is a number value like 42107, that represents a number of days since 1st January 1900, and the only thing that tells MS Excel (or PHPExcel) that it should be treated as a date is the number format mask that's applied to that cell.

    When you use

    $objReader->setReadDataOnly(true); 
    

    you're telling PHPExcel to read the raw data values from the cells, and ignore any number format masks (or indeed any styling) for cells, so that's what you're getting.

    If you want PHPExcel to treat the value as a date, then don't use $objReader->setReadDataOnly(true);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?