duanpu8830 2012-09-12 18:20
浏览 84

PHPExcel问题,用于从excel文件中读取日期字段

I am facing a problem with the PHPExcel class while i am using that class to read a excel file in my php code.

// Reading the excel data
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load('test.xlsx');
$objPHPExcel->setActiveSheetIndex(0);
$dataArray = $objPHPExcel->getActiveSheet()->toArray();

I am using this code, Now problem is that when I create a excel file and insert a cell value like a date say 01/09/2012

and reads the full excelsheet using the toarray() function of PHPExcel class, then it do not returns the date value. it's returning a numeric value.

What I found that when we create the Excel file using the MS OFfice application and insert a date into any cell the excel converts the cell to a date format. So now I want to read a date format cell using the PHPExcel class.

  • 写回答

1条回答 默认 最新

  • douan1893 2012-09-12 20:02
    关注

    Rule one:

    It's not a good idea to set $objReader->setReadDataOnly(true); when you'll be working with dates. If you do that, then PHPExcel cannot tell you whether a cell contains a date or a number.

    Rule two:

    That number is an Excel date value as stored by Excel. The Excel numberFormat Mask then is used to convert that number to a formatted "human readable" date at display-time. (If you've used $objReader->setReadDataOnly(true); then you haven't loaded the cell's numberFormat mask. If you've loaded the numberFormat masks, then you can use the getFormattedValue() method to return the formatted "human-readable" date as a string.

    Rule three:

    To convert an Excel numeric date value to a unix date/time stamp or to a PHP DateTime object, you can use the built-in PHPExcel_Shared_Date::ExcelToPHP() or PHPExcel_Shared_Date::ExcelToPHPObject() methods.

    Rule four:

    All of this is fully documented. But I guess I'm going to have to write a special manual dedicated to Dates with the word DATES in big red letters in the filename.

    评论

报告相同问题?

悬赏问题

  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真