I have an excel document with many columns.I want to parse that data.Please help in php.Provide simple means to guide
Thanks
look at this class
http://phpexcel.codeplex.com/
you can find an example of read here
if it saves as CSV this is how to read it:
<?php
$row = 1;
if (($handle = fopen("test.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);
}
?>
I've used this in one of my projects...works like a charm :P
http://sourceforge.net/projects/phpexcelreader/
Related
I hava a csv file with data like this:
enter image description here
My code to read this csv file is:
<?php
$row = 1;
if (($handle = fopen("test.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);
}
It outputs:
1 fields in line 1:
1.19617E+11
1 fields in line 2:
1.13953E+11
1 fields in line 3:
1.19501E+11
But what I expect is actual value in csv(value display in formula area):
119616896266
113953402648
119501389455
Can anyone help to solve this? Thank you!
(I came across this problem when I using phpspreadsheet, and I found it use fgetcsv to read file.)
When I open the csv file
$str = file_get_contents($filename);
$str = mb_convert_encoding($str,"utf-8","sjis");
var_dump($str);
It shows like this
\0000\000.\0000\0000\0008\0000\0005\0007\000,\0000\000.\0000\0001\0000 ....
At first I think , this is the problem of encoding, so I added mb_convert_encoding but in vain.
What cause this improper encoding?
Better you can do like this:
$row = 1;
if (($handle = fopen("test.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);
}
Code copied from :http://php.net/manual/en/function.fgetcsv.php
alternatively : http://php.net/manual/en/function.str-getcsv.php
I have the following csv file:
9 carat gold,11.87
18 carat gold,23.73
Silver,0.49
Platinum,27.52
I would like to put this in a Multidimensional Array but i have no idea how to.
Thanks for any help.
Try this code
$csv = file_get_contents($file);
$data = explode(PHP_EOL, $csv);
$arr = array();
foreach ($data as $entry) {
$arr[] = str_getcsv($entry);
}
print_r($arr);
You can go through this link for more details about str_getcsv function
You can use fgetcsv to parse a CSV file without having to worry about parsing it yourself.
Example from PHP Manual:
$row = 1;
if (($handle = fopen("test.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);
}
You can use fgetcsv to read a csv file open with fopen.
In tried importing a csv into a table using phpMyAdmin and I'm getting permission errors so I'm looking for any php scripts available around that I can use.
What scripts are available? Can ane point to any free one's?
fgetcsv is the php function you should use.
For details see here.
Sample code here..
$row = 1;
if (($handle = fopen("test.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++) {
// write insert query here
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
Thanks.
I want to display the contents of a CSV file by assigning it to a variable and then display it using echo statement.
This code is not working could someone point the error in it
$fh = fopen('db.csv', 'r');
$now = time();
$data=fgetcsv($fh);
$data[0]=$name;
echo $name;
Am a newbie to coding and scripting.
Thanks
This is the CSV line that I want to be printed
katz,26-11-2011,http//www.google.com
Why you first valorize the $data with an array of your CSV and then you overwrite the first position with the $name variable (which apparently is null)?
$data[0] = $name;
should be
$name = $data[0];
You could use the handy fgetcvs function for this.
Might as well post an example for want of a complete answer, shamelessly ripped from that same
manpage:
<?php
$row = 1;
if (($handle = fopen("test.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);
}
?>
Hope that fits your needs. Happy coding.