tags:

views:

66

answers:

5

Hello,

What would be the most simple way to read a csv file using php

Here is the code, the problem here is, it reads everything in one line

$row = 1;
if (($handle = fopen("1.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}

Thanks Jean

[edit]

Apologies, yes I want to read a csv file

A: 

If you use php under windows you can create a com object.

$excel_app = new COM("Excel.application") or Die ("Did not connect");

Victor Marzo
It has to be universal, since it can be ported to linux
Jean
A: 

Try PHP-ExcelReader. I never used it but it claims to be cross platform.

stribika
A: 

You don't indicate which version of Excel; but take a look at PHPExcel (which can read BIFF5-8 formats, and the rare Excel2003 XML format, as well as the more recent OpenXML standard introduced with Excel2007). It's pure PHP, so will run on any platform (no need for .COM).

declaration of vested interest: I am one of the developers.

Mark Baker
I am trying to write the script myself, rather than use one thats available, its helpful in debugging. Looking for something thats universal
Jean
If you seriously want to write a script to read Excel files all by yourself, then good luck to you... it's a **lot** of hard work (PHPExcel has been 3 1/2+ years in development, and is still ongoing) especially if you're starting with a CSV reader that simply requires PHP's built-in functions. But if you really want to embark on writing an Excel reader, why not help an existing project that's available to the whole PHP developer community rather than simply write it for yourself.
Mark Baker
+1  A: 

You might try removing the 'length' parameter, or setting to 0, especially if you have a line longer than 1000 characters.

drowe
+1  A: 

Edit: Seems to me that you want to read CSV files.

alt text

Here is an functional example for the spreadsheet above:

$file_handle = fopen("widgets.csv", "r");

while (!feof($file_handle) ) {

$line_of_text = fgetcsv($file_handle, 1024);

print $line_of_text[0] . $line_of_text[1]. $line_of_text[2] . "<BR>";

}

fclose($file_handle);

But if you really want to read files in Excel format. xlsx, you can use the PHPExcel library. http://phpexcel.codeplex.com/

Reading a spreadsheet:

$objReader = new PHPExcel_Reader_Excel2007();

$objPHPExcel = $objReader->load("05featuredemo.xlsx");

Set properties

echo date('H:i:s') . " Set properties\n";

$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");

$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");

$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");

$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");

$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");

Add some data

echo date('H:i:s') . " Add some data\n";

$objPHPExcel->setActiveSheetIndex(0);

$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');

$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');

$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');

$objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');

Rename sheet

echo date('H:i:s') . " Rename sheet\n";

$objPHPExcel->getActiveSheet()->setTitle('Simple');

Save Excel 2007 file

echo date('H:i:s') . " Write to Excel2007 format\n";

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

$objWriter->save(str_replace('.php', '.xlsx', FILE));

Read specific sheets only

You can set the option setLoadSheetsOnly on the reader, to instruct the reader to only load the sheets with a given name:

$objReader = new PHPExcel_Reader_Excel2007();

$objReader->setLoadSheetsOnly( array("Sheet 1", "My special sheet") );

$objPHPExcel = $objReader->load("05featuredemo.xlsx");

That's it!

More info and docs in:

http://phpexcel.codeplex.com/

Paulocoghi
// You can create new spreadsheets as well.$objPHPExcel = new PHPExcel();
Paulocoghi