PHPExcel:将所有值(日期,时间,数字)读取为字符串

Web*_*oft 10 php phpexcel

我正面临着日期和时间的问题.我想让它们像在excel工作簿中出现一样阅读.

所以,我相信如果我使用toArray()获取工作表的所有单元格,那么我应该简单地做一些事情(一些格式转换),将所有单元格映射为字符串,日期,时间等.

或者可能有像load这样的函数,它将工作簿中的所有数据作为字符串加载,而不需要任何格式化或其他复杂的东西.

Mar*_*ker 14

toArray()支持以下参数:

/**
 * @param  mixed    $nullValue          Value returned in the array entry if a cell doesn't 
 *                                      exist
 * @param  boolean  $calculateFormulas  Should formulas be calculated?
 * @param  boolean  $formatData         Should formatting be applied to cell values?
 * @param  boolean  $returnCellRef      False - Return a simple array of rows and 
 *                                      columns indexed by number counting from zero
 *                                      True - Return rows and columns indexed by their 
 *                                      actual row and column IDs
 */
Run Code Online (Sandbox Code Playgroud)

所以

toArray(NULL,TRUE,TRUE);
Run Code Online (Sandbox Code Playgroud)

将返回工作表中的所有单元格值(计算和格式化),就像它们在Excel本身中显示的那样.

所以日期将返回(例如)2011年12月21日07:30而不是47239.318(取决于单元格的格式掩码).其他数字单元格可以返回为21,357.00(取决于该单元格的格式屏蔽),因此它是一把双刃剑.

  • 谢谢你回答这个问题.它工作正常.但我认为toArray(),rangeToArray()(以及其他适用的所有其他文档)的文档应该包括不要使用`$ objReader-> setReadDataOnly(true)'的注意事项.我花了一段时间才弄明白,为什么你的建议不起作用.我必须深入研究代码PHPExel来解决这个问题. (4认同)