phpexcel将长数字读取为布尔值
我使用的是Symfony2.3.4、PHP5.6.3和phpexcel1.8.0 当我尝试读取excel文件时,几乎所有单元格都能正常工作 如果单元格包含非常大的数字,当我读取它并在html视图中显示值时,它会输出false 我试着使用马克·贝克(MarkBaker)指导的自定义值绑定器,但我无法让它工作,它从一开始就是一个布尔值 重要: 我试图在html中加载的Excel是从另一个站点下载(生成)的,我注意到,当您尝试使用Microsoft Excel打开它们时,它首先会提示您一个警告窗口,告诉用户文件扩展名和文件格式不匹配,尽管如果您选择无论如何打开它,它仍然可以正常打开 我想这就是问题的原因,我几乎可以肯定(我无法联系实现其他网站下载功能的人)他们做了如下事情:phpexcel将长数字读取为布尔值,php,symfony,phpexcel,Php,Symfony,Phpexcel,我使用的是Symfony2.3.4、PHP5.6.3和phpexcel1.8.0 当我尝试读取excel文件时,几乎所有单元格都能正常工作 如果单元格包含非常大的数字,当我读取它并在html视图中显示值时,它会输出false 我试着使用马克·贝克(MarkBaker)指导的自定义值绑定器,但我无法让它工作,它从一开始就是一个布尔值 重要: 我试图在html中加载的Excel是从另一个站点下载(生成)的,我注意到,当您尝试使用Microsoft Excel打开它们时,它首先会提示您一个警告窗口,告
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, $ext == 'xlsx' ?
'Excel5' : 'Excel2007');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, $ext == 'xls' ?
'Excel5' : 'Excel2007');
当他们应该这样做的时候:
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, $ext == 'xlsx' ?
'Excel5' : 'Excel2007');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, $ext == 'xls' ?
'Excel5' : 'Excel2007');
按照PHPExcel文档中的说明,使扩展名和格式匹配
如果您需要任何具体的澄清,请询问
将文件加载到html中的我的代码:
public function uploadAction() {
$request = $this->getRequest();
$form = $this->createFormBuilder()
->add('file', 'file')
->getForm();
if ($request->getMethod() == 'POST'){
$form->submit($request);
$file = $form['file'];
$file->getData()->move(
'uploads', $form['file']->getData()->getClientOriginalName());
$ext = pathinfo($file->getData()->getClientOriginalName(), PATHINFO_EXTENSION);
$name = pathinfo($file->getData()->getClientOriginalName(), PATHINFO_BASENAME);
//$objReader = \PHPExcel_IOFactory::createReader('xlsx' == $ext ? 'Excel2007' : 'Excel5');
$objReader = \PHPExcel_IOFactory::createReaderForFile('uploads/' . $name);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load('uploads/' . $name);
$activeSheet = $objPHPExcel->getActiveSheet();
$rowIter = $activeSheet->getRowIterator();
foreach ($rowIter as $key => $row) {
$columns = array();
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
foreach ($cellIterator as $cell)
$columns[] = $cell->getCalculatedValue();
}
}
}
注意:我真的不知道以下两者之间的区别:
$objReader = \PHPExcel_IOFactory::createReader('xlsx' == $ext ? 'Excel2007' : 'Excel5');
$objReader = \PHPExcel_IOFactory::createReader('xlsx' == $ext ? 'Excel2007' : 'Excel5');
及
我知道我不能使用第一个,因为我在上面描述的文件生成错误等问题。如果我尝试使用它,浏览器会显示:
The filename uploads/<name>.xls is not recognised as an OLE file.
我猜如果它有帮助的话,你会比我更好地使用它。再次感谢
public function read($sFileName) {
// Check if file exists and is readable
if (!is_readable($sFileName)) {
throw new PHPExcel_Reader_Exception("Could not open " . $sFileName . " for reading! File does not exist, or it is not readable.");
}
// Get the file identifier
// Don't bother reading the whole file until we know it's a valid OLE file
$this->data = file_get_contents($sFileName, FALSE, NULL, 0, 8);
////VAR_DUMPSSSSSSSSSSSS
var_dump($this->data);
var_dump(self::IDENTIFIER_OLE);
die();
// Check OLE identifier
if ($this->data != self::IDENTIFIER_OLE) {
throw new PHPExcel_Reader_Exception('The filename ' . $sFileName . ' is not recognised as an OLE file');
}
// Get the file data
$this->data = file_get_contents($sFileName);
// Total number of sectors used for the SAT
$this->numBigBlockDepotBlocks = self::_GetInt4d($this->data, self::NUM_BIG_BLOCK_DEPOT_BLOCKS_POS);
// SecID of the first sector of the directory stream
$this->rootStartBlock = self::_GetInt4d($this->data, self::ROOT_START_BLOCK_POS);
// SecID of the first sector of the SSAT (or -2 if not extant)
$this->sbdStartBlock = self::_GetInt4d($this->data, self::SMALL_BLOCK_DEPOT_BLOCK_POS);
// SecID of the first sector of the MSAT (or -2 if no additional sectors are used)
$this->extensionBlock = self::_GetInt4d($this->data, self::EXTENSION_BLOCK_POS);
// Total number of sectors used by MSAT
$this->numExtensionBlocks = self::_GetInt4d($this->data, self::NUM_EXTENSION_BLOCK_POS);
$bigBlockDepotBlocks = array();
$pos = self::BIG_BLOCK_DEPOT_BLOCKS_POS;
$bbdBlocks = $this->numBigBlockDepotBlocks;
if ($this->numExtensionBlocks != 0) {
$bbdBlocks = (self::BIG_BLOCK_SIZE - self::BIG_BLOCK_DEPOT_BLOCKS_POS) / 4;
}
for ($i = 0; $i < $bbdBlocks; ++$i) {
$bigBlockDepotBlocks[$i] = self::_GetInt4d($this->data, $pos);
$pos += 4;
}
for ($j = 0; $j < $this->numExtensionBlocks; ++$j) {
$pos = ($this->extensionBlock + 1) * self::BIG_BLOCK_SIZE;
$blocksToRead = min($this->numBigBlockDepotBlocks - $bbdBlocks, self::BIG_BLOCK_SIZE / 4 - 1);
for ($i = $bbdBlocks; $i < $bbdBlocks + $blocksToRead; ++$i) {
$bigBlockDepotBlocks[$i] = self::_GetInt4d($this->data, $pos);
$pos += 4;
}
$bbdBlocks += $blocksToRead;
if ($bbdBlocks < $this->numBigBlockDepotBlocks) {
$this->extensionBlock = self::_GetInt4d($this->data, $pos);
}
}
$pos = 0;
$this->bigBlockChain = '';
$bbs = self::BIG_BLOCK_SIZE / 4;
for ($i = 0; $i < $this->numBigBlockDepotBlocks; ++$i) {
$pos = ($bigBlockDepotBlocks[$i] + 1) * self::BIG_BLOCK_SIZE;
$this->bigBlockChain .= substr($this->data, $pos, 4 * $bbs);
$pos += 4 * $bbs;
}
$pos = 0;
$sbdBlock = $this->sbdStartBlock;
$this->smallBlockChain = '';
while ($sbdBlock != -2) {
$pos = ($sbdBlock + 1) * self::BIG_BLOCK_SIZE;
$this->smallBlockChain .= substr($this->data, $pos, 4 * $bbs);
$pos += 4 * $bbs;
$sbdBlock = self::_GetInt4d($this->bigBlockChain, $sbdBlock * 4);
}
// read the directory stream
$block = $this->rootStartBlock;
$this->entry = $this->_readData($block);
$this->_readPropertySets();
}
公共函数读取($sFileName){
//检查文件是否存在且可读
如果(!可读($sFileName)){
抛出新的PHPExcel_Reader_异常(“无法打开用于读取的“$sFileName.”!文件不存在,或不可读。”);
}
//获取文件标识符
//在我们知道它是一个有效的OLE文件之前,不要阅读整个文件
$this->data=file\u get\u contents($sFileName,FALSE,NULL,0,8);
////VAR_dumpssss
变量转储($this->data);
变量转储(self::IDENTIFIER\u OLE);
模具();
//检查OLE标识符
如果($this->data!=self::IDENTIFIER\u OLE){
抛出新的PHPExcel_Reader_异常(“文件名“$sFileName.”未识别为OLE文件);
}
//获取文件数据
$this->data=file\u get\u contents($sFileName);
//SAT使用的扇区总数
$this->numBigBlockDepotBlocks=self::_GetInt4d($this->data,self::NUM\u BIG\u BLOCK\u deptblocks\u POS);
//目录流的第一个扇区的SecID
$this->rootStartBlock=self::GetInt4d($this->data,self::ROOT\u START\u BLOCK\u POS);
//SSAT第一个扇区的SecID(如果不存在,则为-2)
$this->sbdStartBlock=self::GetInt4d($this->data,self::SMALL_BLOCK_DEPOT_BLOCK_POS);
//MSAT第一个扇区的SecID(如果未使用其他扇区,则为-2)
$this->extensionBlock=self::\u GetInt4d($this->data,self::EXTENSION\u BLOCK\u POS);
//MSAT使用的扇区总数
$this->numExtensionBlocks=self::\u GetInt4d($this->data,self::NUM\u EXTENSION\u BLOCK\u POS);
$bigBlockDepotBlocks=array();
$pos=self::大街区\停车场\街区\ pos;
$bbdBlocks=$this->numbigblockDepotblock;
如果($this->numExtensionBlocks!=0){
$bbdBlocks=(self::BIG_BLOCK_SIZE-self::BIG_BLOCK_depos_BLOCK_POS)/4;
}
对于($i=0;$i<$bbdBlocks;++$i){
$bigBlockDepotBlocks[$i]=self::_GetInt4d($this->data,$pos);
$pos+=4;
}
对于($j=0;$j<$this->numExtensionBlocks;++$j){
$pos=($this->extensionBlock+1)*self::BIG_BLOCK_SIZE;
$blocksToRead=min($this->numbigblockdepoteblocks-$bbdBlocks,self::BIG_BLOCK_SIZE/4-1);
对于($i=$bbdBlocks;$i<$bbdBlocks+$blocksToRead;++$i){
$bigBlockDepotBlocks[$i]=self::_GetInt4d($this->data,$pos);
$pos+=4;
}
$bbdBlocks+=$blocksToRead;
如果($bbdBlocks<$this->numbigblockDepotblock){
$this->extensionBlock=self:_GetInt4d($this->data,$pos);
}
}
$pos=0;
$this->bigblockschain='';
$bbs=self::大块大小/4;
对于($i=0;$i<$this->numBigBlockDepotBlocks;++$i){
$pos=($bigBlockDepotBlocks[$i]+1)*self::BIG_BLOCK_SIZE;
$this->bigblockschain.=substr($this->data,$pos,4*$bbs);
$pos+=4*$bbs;
}
$pos=0;
$sbdBlock=$this->sbdStartBlock;
$this->smallBlockChain='';
而($sbdBlock!=-2){
$pos=($sbdBlock+1)*自我::大块大小;
$this->smallBlockChain.=substr($this->data,$pos,4*$bbs);
$pos+=4*$bbs;
$sbdBlock=self::_GetInt4d($this->bigblockschain,$sbdBlock*4);
}
//读取目录流
$block=$this->rootStartBlock;
$this->entry=$this->\u readData($block);
$this->_readPropertySets();
}
两者之间的区别
及
第一个是相信扩展名对于文件的实际格式是正确的,扩展名为.xlsx
的文件实际上是OfficeOpenXML格式的文件,或者扩展名为.xls
的文件实际上是BIFF格式的文件,然后告诉PHPExcel使用合适的读取器
这通常不是一个问题,除非(例如)它不只是扩展名为.xls
或.xlsx
的文件中的HTML标记。。。。然后,您为文件的实际格式选择了错误的读取器;这就是MS Excel本身告诉您的信息“文件扩展名和文件格式不匹配”
第二是使用
$objReader = \PHPExcel_IOFactory::createReader('xlsx' == $ext ? 'Excel2007' : 'Excel5');
$objReader = \PHPExcel_IOFactory::createReaderForFile('uploads/' . $name);