0

i download an excel file from one site and want to import it`s data in html file with php,i use PHPEXCEL library,but my code does not work correctly unless i open my excel file and save it manually,and after that php code can read data from it and import data in html file. how can i do this process automatically?

my code:

<?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);
    }

    ?>
8
  • is it an excel file or CSV file?
    – jeorfevre
    Commented Jun 7, 2014 at 12:55
  • Unsure what if($cell == 'test'){ is doing.... $cell is a PHPExcel_Cell object, not a string... should this be if($cell->getValue() == 'test'){... but I can't find any cells in your linked spreadsheet that contain the text 'test'
    – Mark Baker
    Commented Jun 7, 2014 at 13:29
  • (deleted my response due to comment ;)
    – jeorfevre
    Commented Jun 7, 2014 at 13:35
  • I downloaded the file from the link posted by the OP, and ran a small script to try and read it. For some weird reason, the output of getHighestRow() is 1 !! I think the problem lies there. Commented Jun 7, 2014 at 13:41
  • So guess I need to figure out why getHighestRow() should return a 1 for this spreadsheet, it's highly unusual
    – Mark Baker
    Commented Jun 7, 2014 at 14:12

3 Answers 3

2

The problem is that the file you are downloading uses namespacing when a standard MS Excel file doesn't.

e.g (standard MS Excel file)

<?xml version="1.0" encoding="UTF-8" standalone="true"?>
<workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xml:space="preserve">
    <fileVersion rupBuild="4505" lowestEdited="4" lastEdited="4" appName="xl"/>
    <workbookPr codeName="ThisWorkbook"/>
    <bookViews>
        <workbookView visibility="visible" tabRatio="600" showVerticalScroll="1" showSheetTabs="1" showHorizontalScroll="1" minimized="0" firstSheet="0" autoFilterDateGrouping="1" activeTab="0"/>
    </bookViews>
    <sheets>
        <sheet r:id="rId4" sheetId="1" name="Simple"/>
    </sheets>
    <definedNames/>
    <calcPr fullCalcOnLoad="0" calcMode="auto" calcId="124519"/>
</workbook>

Your file:

<?xml version="1.0" encoding="UTF-8"?>
<x:workbook xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
    <x:bookViews>
        <x:workbookView/>
    </x:bookViews>
    <x:sheets>
        <x:sheet xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="R87019d29e03f45b0" sheetId="1" name="دیده بان بازار"/>
    </x:sheets>
</x:workbook>

Note the x: prefix for each element; the line xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main" defines a namespace of x for http://schemas.openxmlformats.org/spreadsheetml/2006/main while in the standard MS Excel file this is simply xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" to set http://schemas.openxmlformats.org/spreadsheetml/2006/main as the default namespace for the file

There is no clean and easy solution to this

A dirty solution is to modify the Excel2007 Reader. Assuming that you're using PHPExcel 1.8.0

Change line 505 from

$xmlStyles = simplexml_load_string($this->_getFromZipArchive($zip, "$dir/$xpath[Target]"), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions()); //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");

to

$xmlStyles = simplexml_load_string(preg_replace('/\bx:/','',$this->_getFromZipArchive($zip, "$dir/$xpath[Target]")), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions()); //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");

Change line 609 from

$xmlWorkbook = simplexml_load_string($this->_getFromZipArchive($zip, "{$rel['Target']}"), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions());  //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");

to

$xmlWorkbook = simplexml_load_string(preg_replace('/\bx:/','',$this->_getFromZipArchive($zip, "{$rel['Target']}")), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions());  //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");

Change line 652 from

$xmlSheet = simplexml_load_string($this->_getFromZipArchive($zip, "$dir/$fileWorksheet"), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions());  //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");

to

$xmlSheet = simplexml_load_string(preg_replace('/\bx:/','',$this->_getFromZipArchive($zip, "$dir/$fileWorksheet")), 'SimpleXMLElement', PHPExcel_Settings::getLibXmlLoaderOptions());  //~ http://schemas.openxmlformats.org/spreadsheetml/2006/main");

Note that this is a hack, not a fix... it may have adverse effects on other workbooks (though I hope that it won't)

0
0

Try to load file like this:

    $inputFileType = PHPExcel_IOFactory::identify($path);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType); //Excel5

    /** @var PHPExcel $objPHPExcel */
    $objPHPExcel = $objReader->load($path);

.....

0
0

I am no expert PHP programmer. But I have used PHPexcel for quite a few tools.

The main problem appears to be the first row of your file, in which all the cells have been merged. I removed the merge, and my script was able to read the values with no problem.

Link to OP's original file(re-posting here due to the original post being deleted):- http://members.tsetmc.com/tsev2/excel/MarketWatchPlus.aspx?d=0

Minimum requirements for the script:-

  1. Keep the Excel, PHPExcel.php, and this script in the same directory

  2. Keep the PHPExcel library in the same directory as well.

Please find below my script, which reads all the values in the 3rd Column, from the 4th row onwards.

<?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();

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.