JustinasT
JustinasT

Reputation: 633

PHP - XLSX to CSV adding spaces

I have problem converting xlsx file to csv - it ads spaces after every symbol.

xlsx file: enter image description here

CSV file result: enter image description here

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?

EDIT inline

Everything in one line

EDIT 2

enter image description here

Upvotes: 3

Views: 573

Answers (1)

JustinasT
JustinasT

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

Related Questions