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
-
Answer:
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/
Jean at Stack Overflow Visit the source
Other answers
You might try removing the 'length' parameter, or setting to 0, especially if you have a line longer than 1000 characters.
drowe
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
Try http://sourceforge.net/projects/phpexcelreader/. I never used it but it claims to be cross platform.
stribika
You don't indicate which version of Excel; but take a look at http://www.phpexcel.net (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
Related Q & A:
- how delete node in xml file using php?Best solution by Stack Overflow
- How to skip columns empty in CSV file when importing into MySQL table using LOAD DATA INFILE?Best solution by Stack Overflow
- How to upload file using ajax php?Best solution by Stack Overflow
- How to read a PDF file with PHP?Best solution by Stack Overflow
- How to covert csv file to excel and back excel file to csv in python?Best solution by completecampaigns.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.