2

I am reading two excel file, using php-excel-reader (From this)

After reading two files of 1st row, I am comparing it. If they are same then I am appending contain of on file to other. To write the file I am using this

Now for doing this I want to close one file, but that function is not available in php-excel-reader

here is my code

compare file

{

$data = new Spreadsheet_Excel_Reader($filepath);

$data1 = new Spreadsheet_Excel_Reader($destinationfilepath);

}

unset($data);

unset($data1);


if($flag==0)
{

$excel = new ExcelWriter($destinationfilepath); 

// read the source file

 $finalarray= array();

for($m=1;$m<$sourcefilerowcount;$m++)

    { 

           $charvalue='A';

             $temprow=$m+1;

              for($n=0;$n<$destinationcolnum;$n++)
        {

            $data = new Spreadsheet_Excel_Reader($filepath);

                            $finalarray[$n]=$data->val($temprow,$charvalue);

                            $charvalue++;

                    }

             print_r($finalarray)."<br/>";

     $excel->writeLine($finalarray);
  }
Mel
  • 5,837
  • 10
  • 37
  • 42
snehal
  • 429
  • 5
  • 11
  • 25
  • Its in the code i guess `$excel->close();` [http://www.vijayjoshi.org/2009/12/15/how-to-create-excel-files-in-php/](http://www.vijayjoshi.org/2009/12/15/how-to-create-excel-files-in-php/) – swapnesh Jun 18 '13 at 06:31
  • ya.but for php-excel-reader i am not getting. $data1 = new Spreadsheet_Excel_Reader($destinationfilepath); how to close $data1 ? – snehal Jun 18 '13 at 06:47
  • @2492230 you can use `unset($data1);` – swapnesh Jun 18 '13 at 06:54
  • thank you.i tried this one. but my data is not copying in other file.i will upload my code – snehal Jun 18 '13 at 07:05
  • @user2492230 Show us your code, that is not working and explain, what you are trying to achieve there – user4035 Jun 18 '13 at 07:06
  • @user2492230 Remove the unset line. If you want, you can put in the end of the script code, working with $data. There is no need to close the files explicitly - phpExcelReader is doing it for you. Why do you want to close? Isn't anything working correctly? – user4035 Jun 18 '13 at 07:20
  • @ user4035: first time i m opening that file header reading to compare the headers with some other file header suppose if both file hearde are match then i have to copy the data from one file to another. so first time opening the file to read the hearers in read mode and i want to close file in and then i want to open this file in write mode. so i want to close it. because it is not allowing me to write in to file because it is already open so i have to close that first then only i can open the same in write mode. – snehal Jun 18 '13 at 07:30
  • 1
    Rather than using two different and incompatible packages for reading Excel and for writing Excel, why not look for a single package that can both read and write Excel files.... like the PHPExcel package that you've listed in your tags – Mark Baker Jun 18 '13 at 10:53
  • @user2492230 Use 1 package to read and write as Mark Baker said. – user4035 Jun 18 '13 at 11:19

1 Answers1

3

There is no need to explicitly call close() function, because the file is automatically closed in the load() method. If you look at Excel2007.php, where PHPExcel_Reader_Excel2007 is defined, you'll see:

public function load($pFilename)
{
    ...
    $zip = new ZipArchive;

    $zip->open($pFilename);
    ...
    $zip->close();
    return $excel;
}

Just unset your PHPExcel_Reader object, and the data will be removed from memory:

$objReader = PHPExcel_IOFactory::createReader('Excel2003XML');
$objPHPExcel = $objReader->load("Excel2003XMLTest.xml");
...
unset($objPHPExcel);
unset($objReader);
user4035
  • 22,508
  • 11
  • 59
  • 94
  • 2
    OP Isn't actually using PHPExcel, despite the tag; they're using PEAR SEW.... though the same argument holds: PEAR SEW closes the file after loading the file into memory – Mark Baker Jun 18 '13 at 10:49
  • @MarkBaker Ahh, I missed it - was misguided by the tag. Yeah, he should use the same lib everywhere. – user4035 Jun 18 '13 at 11:18