Here we are introducing the use of PHP's fgetcsv function to directly read .csv files. It is not a real excel file. If we want to read a real excel file, we need to use a plug-in. I will briefly introduce it below.
excel file (.csv)
The code is as follows |
Copy code |
代码如下 |
复制代码 |
function getCSVdata($filename)
{
$row = 1;//第一行开始
if(($handle = fopen($filename, "r")) !== false)
{
while(($dataSrc = fgetcsv($handle)) !== false)
{
$num = count($dataSrc);
for ($c=0; $c < $num; $c++)//列 column
{
if($row === 1)//第一行作为字段
{
$dataName[] = $dataSrc[$c];//字段名称
}
else
{
foreach ($dataName as $k=>$v)
{
if($k == $c)//对应的字段
{
$data[$v] = $dataSrc[$c];
}
}
}
}
if(!empty($data))
{
$dataRtn[] = $data;
unset($data);
}
$row++;
}
fclose($handle);
return $dataRtn;
}
}
$aData = getCSVdata('all_www.bKjia.c0m.csv');
foreach ($aData as $k=>$v ){
echo "http://".$v['a']." ";
}
?>
|
function getCSVdata($filename)
{
$row = 1; //Start from the first row
If(($handle = fopen($filename, "r")) !== false)
{
$num = count($dataSrc);
for ($c=0; $c < $num; $c++)//column column
If($row === 1)//The first row is used as a field
$dataName[] = $dataSrc[$c];//Field name
foreach ($dataName as $k=>$v)
If($k == $c)//Corresponding field
$data[$v] = $dataSrc[$c];
if(!empty($data))
$dataRtn[] = $data;
unset($data);
$row++;
fclose($handle);
return $dataRtn;
}
}
$aData = getCSVdata('all_www.bKjia.c0m.csv');
foreach ($aData as $k=>$v ){
echo "http://".$v['a']." ";
}
?>
|
Generate excel file (csv)
The code is as follows
代码如下 |
复制代码 |
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=test_data.xls");
//输出内容如下:
echo "姓名"."t";
echo "年龄"."t";
echo "学历"."t";
echo "n";
echo "张三"."t";
echo "25"."t";
echo "本科"."t";
?>
|
|
Copy code
|
header("Content-type:application/vnd.ms-excel"); |
header("Content-Disposition:attachment;filename=test_data.xls");
//The output content is as follows:
echo "Name"."t";
echo "Age"."t";
echo "Education"."t";
echo "n";
echo "Zhang San"."t";
echo "25"."t";
echo "Undergraduate"."t";
?>
The above are just some simple excel file operations. If you want to perform table operations such as editing, modifying, and deleting rows, we can use the phpexcel plug-in to operate the excel file
Recommended reading: Using phpexcel to read excel implementation code
http://www.bkjia.com/PHPjc/632716.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632716.htmlTechArticleHere we introduce the use of php’s fgetcsv function to directly read .csv files, which are not real excel files. , if we want to read the real excel file we need to use the plug-in, below I...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn