简体   繁体   中英

PHP excel reader cannot read excel file from command prompt

I have an application that need to read excel file. I have already done this using a plugin excel-file-reader from http://code.google.com/p/php-excel-reader/downloads/list . Now i need my application to read an excel file in a background process, and i choose to run the php file from windows command prompt.but when i do this, the result in command prompt is The filename person2.xls is not readable, but when i run the php file from the browser, it can read the excel file. here is my code to help you understand what i mean.

<?php
require_once 'php-excel-reader\excel_reader2.php';
$db=pg_connect('host=localhost dbname=hris user=postgres password=Abcd1234');
$table='lin_people';
$dataExcel = new Spreadsheet_Excel_Reader('person2.xls');
$baris = $dataExcel->rowcount($sheet_index=0);
$model=$table;
$fieldsArray=pg_query('SELECT column_name FROM information_schema.columns WHERE table_name="'.$model.'"');
//$fieldsArray=Set::extract('/0/'.$model,$fieldsArray);
$fieldNames=array_keys($fieldsArray[0][$model]);
$tanda=false;
for($batas=0;$batas<count($fieldNames);$batas++)
{
    if($fieldNames[$batas]!='id')
    {
        if($tanda==false)
        {
            $fields[$batas]=$fieldNames[$batas];
        }
        else
        {
            if($batas!=count($fieldNames)-1)
            {
                $fields[$batas]=$fieldNames[$batas+1];
            }   
        }
    }
    else
    if($fieldNames[$batas]=='id')
    {
        $fields[$batas]=$fieldNames[$batas+1];
        $tanda=true;
    }
}

$sukses = 0;
$gagal = 0;

for($i=2;$i<=$baris;$i++)
{
    if(!empty($dataExcel))
    {
        $this->$model->create();
        for($y=0;$y<count($fields);$y++)
        {
            if($y==0)
            {
                pg_query('INSERT INTO '.$model.' ('.$fields[$y].') VALUES("'.$dataExcel->val($i,$y+1).'")');
            }
            else
            {
                $currID=pg_query('SELECT id FROM '.$model.' ORDER BY id desc LIMIT 1');
                pg_query('INSERT INTO '.$model.' ('.$fields[$y].') VALUES("'.$dataExcel->val($i,$y+1).'") WHERE id='.$currID);
            }
                            //$this->$model->set($fields[$y],$dataExcel->val($i,$y+1));
        }
    }
                    //$this->$model->save();
    if('dataExcel')
    {
        $sukses++;
    }
    else
    {
        $gagal++;
    }

}

?>

How can i made this php file read an excel file when I run the php file from command prompt?please help me...

This may or may not help. I have a feeling it is 1 of two things:

  1. File permissions.
  2. Unrecognized Mime type.

Its probably the second one but here are 2 solutions:

chmod("person2.xls",0777);
$dataExcel = new Spreadsheet_Excel_Reader('person2.xls');
chmod("person2.xls",0644);

If that doesn't work you may need to include a mime type library .

Command prompt uses other character encoding (character format) than Windows. Maybe this is where your problem resides. If you try to save an Excel spreadsheet as CSV or TEXT for example, you will find 2 filetypes: One for windows and one for DOS.

Hope that it helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM