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 $ro开发者_运维问答w: <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
Edit: Seems to me that you want to read CSV files.
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/
You might try removing the 'length' parameter, or setting to 0, especially if you have a line longer than 1000 characters.
If you use php under windows you can create a com object.
$excel_app = new COM("Excel.application") or Die ("Did not connect");
Try PHP-ExcelReader. I never used it but it claims to be cross platform.
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.
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);*/
?>
精彩评论