X10nD
X10nD

Reputation: 22030

Simplest way to read an csv file using php

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

Upvotes: 2

Views: 5688

Answers (6)

Lokesh Bhardwaj
Lokesh Bhardwaj

Reputation: 1

Following is the best working example:

<?php

ini_set('auto_detect_line_endings', TRUE);

$header = '';
$rows = array_map('str_getcsv', file('ZE5C630852D4DB9AD75E5DE.csv'));
$header = array_shift($rows);
$csv = array();
foreach ($rows as $row) {
  $csv[] =  $row;
}

/*echo "<pre>";
print_r($csv);*/

?>

Upvotes: 0

Paulo Coghi
Paulo Coghi

Reputation: 14949

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/

Upvotes: 4

drowe
drowe

Reputation: 2320

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

Upvotes: 1

Mark Baker
Mark Baker

Reputation: 212402

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.

Upvotes: 0

stribika
stribika

Reputation: 3176

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

Upvotes: 0

Victor Marzo
Victor Marzo

Reputation: 731

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

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

Upvotes: 0

Related Questions