PHP - XLSX 到 CSV 添加空格
PHP - XLSX to CSV adding spaces
我在将 xlsx 文件转换为 csv 文件时遇到问题 - 它会在每个符号后添加空格。
xlsx 文件:
CSV 文件结果:
代码:
if (file_exists('temp.xlsx')) {
require_once('Classes\PHPExcel.php');
$TypeFile="Excel2007";
$FilePath= "temp.xlsx";
$objReader = PHPExcel_IOFactory::createReader($TypeFile);
$objReader->setReadDataOnly(true);
$objExcel = $objReader->load($FilePath);
$objCSV = PHPExcel_IOFactory::createWriter($objExcel, 'CSV');
$objCSV->setPreCalculateFormulas(false);
$objCSV->setDelimiter(',');
$objCSV->setEnclosure('"');
$objCSV->save('prf.csv');
}
如何防止所有这些随机空格出现?
编辑
一切尽在一行
编辑 2
$fi = fopen('myFile.csv', 'r');
$fo = fopen('myFilenew.csv', 'w');
while (($data = fgetcsv($fi, 0, "\t")) !== false) {
array_walk($data, function(&$value) {$value = preg_replace('/[^\P{C}\n]+/u', '', $value);});
fputcsv($fo, $data);
}
fclose($fi);
fclose($fo);
这解决了我的问题。感谢马克·贝克
我在将 xlsx 文件转换为 csv 文件时遇到问题 - 它会在每个符号后添加空格。
xlsx 文件:
CSV 文件结果:
代码:
if (file_exists('temp.xlsx')) {
require_once('Classes\PHPExcel.php');
$TypeFile="Excel2007";
$FilePath= "temp.xlsx";
$objReader = PHPExcel_IOFactory::createReader($TypeFile);
$objReader->setReadDataOnly(true);
$objExcel = $objReader->load($FilePath);
$objCSV = PHPExcel_IOFactory::createWriter($objExcel, 'CSV');
$objCSV->setPreCalculateFormulas(false);
$objCSV->setDelimiter(',');
$objCSV->setEnclosure('"');
$objCSV->save('prf.csv');
}
如何防止所有这些随机空格出现?
编辑
一切尽在一行
编辑 2
$fi = fopen('myFile.csv', 'r');
$fo = fopen('myFilenew.csv', 'w');
while (($data = fgetcsv($fi, 0, "\t")) !== false) {
array_walk($data, function(&$value) {$value = preg_replace('/[^\P{C}\n]+/u', '', $value);});
fputcsv($fo, $data);
}
fclose($fi);
fclose($fo);
这解决了我的问题。感谢马克·贝克