Reputation: 633
I have problem converting xlsx file to csv - it ads spaces after every symbol.
CODE:
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');
}
How to prevent all these random spaces from appearing?
Everything in one line
EDIT 2
Upvotes: 3
Views: 573
Reputation: 633
$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);
This solved my problem. Thanks to Mark Baker
Upvotes: 1