Convert existing files .xls -> .csv (Resolved)

Topics: Developer Forum, User Forum
Oct 6, 2010 at 10:25 AM

Hello,

I get the file .xls with the form and I would like to convert it to .csv and save it. Then I would like to save datas of .csv file in a MySQL database.
I looked at the documentation and I make this code change that does not work.

<?php
$path = './';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

function __autoload($class){
    $file= str_replace(
        '_',
        DIRECTORY_SEPARATOR,
        $class
    ) . '.php';
   
    require_once($file);
}

$objet = new PHPExcel_Reader_Excel5();
$xls = $objet->load('Providers.xls');
$writer = new PHPExcel_Writer_CSV($xls);
$writer->save('Providers.csv');
?>
Thank you =)

Coordinator
Oct 6, 2010 at 6:29 PM

Your autoloader may well be conflicting with the PHPExcel autoloader

Are you getting any errors.

Oct 7, 2010 at 8:46 AM

Not error, but  notices.

Notice: Undefined property: PHPExcel_Shared_OLERead::$wrkbook in C:\wamp\www\Classes\PHPExcel\Shared\OLERead.php on line 174

Notice: Undefined index: in C:\wamp\www\Classes\PHPExcel\Shared\OLERead.php on line 174

Notice: Undefined property: PHPExcel_Shared_OLERead::$wrkbook in C:\wamp\www\Classes\PHPExcel\Shared\OLERead.php on line 178

Notice: Undefined index: in C:\wamp\www\Classes\PHPExcel\Shared\OLERead.php on line 178

Notice: Undefined index: in C:\wamp\www\Classes\PHPExcel\Shared\OLERead.php on line 185

Have any idea please ?

 

Coordinator
Oct 7, 2010 at 9:06 AM

From the messages, it looks like the file you're loading isn't actually an Excel workbook file, just a file with an extension of xls. Try opening it in a text editor, and post the first hundred or so characters of the file.

Oct 7, 2010 at 9:39 AM

It seems you were right, thank you Mark =)

I opened the file with a text editor, but I could not copy/paste here, because of special characters not understood by the charset of this forum.

So I reopened the file with open office and saved it with the right extention (.xls).

The code in my first post works =) I got the output in .csv

Thanks again Mark =)


Coordinator
Oct 7, 2010 at 9:52 AM

Instead of creating a specific writer and then loading like

$objet = new PHPExcel_Reader_Excel5();
$xls = $objet->load('Providers.xls');

you use

$objPHPExcel = PHPExcel_IOFactory::load('Providers.xls');

then PHPExcel will attempt to identify the file type (irrespective of the file extension) and use the appropriate reader for that file.

There is also an identify() method

$objPHPExcel = PHPExcel_IOFactory::identify('Providers.xls');

that uses the same identification logic as PHPExcel_IOFactory::load() and returns the name of the reader that should be instantiated to load the named file. The automatic identification logic isn't perfect, but it can help to avoid problems with misnamed files in future.

Oct 7, 2010 at 10:29 AM
Edited Oct 7, 2010 at 10:34 AM

If I understand well, i can do this too with your code:

$objPHPExcel = PHPExcel_IOFactory::load('Providers.xls');


adding:

$writer = PHPExcel_IOFactory::createWriter($objPHPExcel); 
$writer->save('Providers.csv');

 

Finally, my code could appear like that:

<?php
$path = './';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

function __autoload($class){
    $file= str_replace(
        '_',
        DIRECTORY_SEPARATOR,
        $class
    ) . '.php';
   
    require_once($file);
}

$objPHPExcel = PHPExcel_IOFactory::load('Providers.xls');
$writer = PHPExcel_IOFactory::createWriter($objPHPExcel);
$writer->save('Providers.csv');

 

 

?>