Home > Article > Backend Development > How to convert php xls to csv
php xls to csv method: first create a PHP sample file; then import "PHPExcel.php"; then list all xls files, check and submit to "$_POST"; finally use the getCsv method Just convert xls to csv.
Recommended: "PHP Video Tutorial"
PHPExcle converts xls files to csv files
When importing excel into the database, the data may appear as "= a certain cell" (=A3), so first convert the .xls file to a .csv file, and then import the csv file into the database.
<?php header("Content-Type:text/html;charset=utf-8"); require_once 'PHPExcel/PHPExcel.php'; $d = dir('xls/'); while (($file = $d->read()) !== false){ if(substr(strrchr($file, '.'), 1) == 'xls'){ $arr[] = $file; } } //把所有xls文件列出列表,勾选提交到这边$_POST foreach($_POST as $k => $item){ foreach($item as $v){ //只获取文件名 $file = str_replace('.xls','',$arr[$v]); getCsv($file); } } function getCsv($file){ $filename = "xls/".$file.".xls"; $objReader = new PHPExcel_Reader_Excel5(); //$objReader = PHPExcel_IOFactory::createReader('Excel5'); $objPHPExcel = $objReader->load($filename); //$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV'); $objWriter = new PHPExcel_Writer_CSV($objPHPExcel); $objWriter->save(str_replace('.xls', '.csv',$filename)); $root = "xls/".$file.".csv"; $newRoot='csv/'.$file.'.csv'; //新目录 copy($root,$newRoot); //拷贝到新目录 unlink($root); //删除旧目录下的文件 } ?>
The above is the detailed content of How to convert php xls to csv. For more information, please follow other related articles on the PHP Chinese website!