如何在php中从excel文件读取数据
我从一个网站下载了一个excel文件,想用php将其数据导入html文件,我使用PHPEXCEL库,但我的代码不能正常工作,除非我打开excel文件并手动保存,然后php代码可以从中读取数据并导入html文件中的数据。 如何自动执行此过程 我的代码:如何在php中从excel文件读取数据,php,phpexcel,Php,Phpexcel,我从一个网站下载了一个excel文件,想用php将其数据导入html文件,我使用PHPEXCEL库,但我的代码不能正常工作,除非我打开excel文件并手动保存,然后php代码可以从中读取数据并导入html文件中的数据。 如何自动执行此过程 我的代码: <?PHP require_once ('Classes/PHPExcel.php'); $file="sample.xlsx"; $inputFileName = $file; $objPHPExcel =
<?PHP
require_once ('Classes/PHPExcel.php');
$file="sample.xlsx";
$inputFileName = $file;
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
$sheet = $objPHPExcel->getActiveSheet();
$row = $objPHPExcel->getActiveSheet()->getHighestRow();
for($i=1;$i<=$row;$i++){
$cell = $sheet->getCell('A'.$i);
if($cell=='test'){
$find=1;
break;
}
}
if($find==1){
$rownumber=$i;
$row = $objPHPExcel->getActiveSheet()->getRowIterator($rownumber)->current();
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
foreach ($cellIterator as $key => $cell) {
$data[$j]=round($cell->getCalculatedValue(),2);
$j++;
}
$output="<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<table width='95%' class='bourse'>
<tr><td colspan='2' bgcolor='#F2F2F2'>$header</td></tr>";
for($i=2;$i< $j-1 ;$i++){
$output.="<tr><td>".$data[$i]."</font></td></tr>";
}
$output.="</table></div>";
//echo $output;
file_put_contents('sample.html',$output);
}
?>
尝试像这样加载文件:
$inputFileType = PHPExcel_IOFactory::identify($path);
$objReader = PHPExcel_IOFactory::createReader($inputFileType); //Excel5
/** @var PHPExcel $objPHPExcel */
$objPHPExcel = $objReader->load($path);
…我不是一个专业的PHP程序员。但是我已经在很多工具中使用了PHPexcel
主要问题似乎是文件的第一行,其中所有单元格都已合并。我删除了合并,我的脚本能够毫无问题地读取这些值
指向OP原始文件的链接(由于原始帖子被删除,因此在此处重新发布):-
脚本的最低要求:-
将Excel、PHPExcel.php和此脚本保持在同一目录中
将PHPExcel库也保存在同一目录中
请在下面找到我的脚本,它从第4行开始读取第3列中的所有值
<?php
require "PHPExcel.php";
require "PHPExcel/IOFactory.php";
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
set_time_limit ( 0 );
$fileName = "MarketWatchPlus-1393-3-17.xlsx";
$inputFileType = PHPExcel_IOFactory::identify ( $fileName );
$readerObj = PHPExcel_IOFactory::createReader ( $inputFileType );
$excelFile = $readerObj->load ( $fileName );
// Set active sheet to 0(First Sheet)
$excelFile->setActiveSheetIndex ( 0 );
$activeSheet = $excelFile->getActiveSheet();
// Get the highest row in the file( for looping purposes )
$highestRow = $activeSheet->getHighestRow();
$currentRow = 4;
echo "<html><body>";
while ( $currentRow <= 100 ) {
// Column numbers start with 0. A => 0, B => 1 .. and so on.
$value = $activeSheet->getCellByColumnAndRow ( 2, $currentRow );
// Convert to string
$value = $value."";
echo "<p>".$value."</p>";
$currentRow++;
}
echo "</body></html>";
exit();
?>
问题在于,当标准MS Excel文件不使用名称空间时,您正在下载的文件使用名称空间
e、 g(标准MS Excel文件)
到
将第609行从
$xmlWorkbook = simplexml_load_string($this->_getFromZipArchive($zip, "{$rel['Target']}"), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions()); //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");
到
将第652行从
$xmlSheet = simplexml_load_string($this->_getFromZipArchive($zip, "$dir/$fileWorksheet"), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions()); //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");
到
请注意,这是一个黑客,而不是修复。。。它可能会对其他工作簿产生不利影响(尽管我希望不会)它是excel文件还是CSV文件?不确定($cell=='test'){
正在做什么……$cell是phpexcell\u cell对象,而不是字符串……这应该是如果($cell->getValue()='test')){
…但我在链接的电子表格中找不到任何包含文本“test”(由于注释删除了我的回复;)的单元格。我从OP发布的链接下载了该文件,并运行了一个小脚本尝试读取它。出于某种奇怪的原因,getHighestRow()的输出是1!!我想问题就在这里。所以我想我需要弄清楚为什么getHighestRow()应该为这个电子表格返回1,这是非常不寻常的
$xmlStyles = simplexml_load_string($this->_getFromZipArchive($zip, "$dir/$xpath[Target]"), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions()); //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");
$xmlStyles = simplexml_load_string(preg_replace('/\bx:/','',$this->_getFromZipArchive($zip, "$dir/$xpath[Target]")), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions()); //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");
$xmlWorkbook = simplexml_load_string($this->_getFromZipArchive($zip, "{$rel['Target']}"), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions()); //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");
$xmlWorkbook = simplexml_load_string(preg_replace('/\bx:/','',$this->_getFromZipArchive($zip, "{$rel['Target']}")), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions()); //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");
$xmlSheet = simplexml_load_string($this->_getFromZipArchive($zip, "$dir/$fileWorksheet"), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions()); //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");
$xmlSheet = simplexml_load_string(preg_replace('/\bx:/','',$this->_getFromZipArchive($zip, "$dir/$fileWorksheet")), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions()); //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");