Click here to Skip to main content
15,123,851 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following code converts every sheet of an XLS file to a CSV file. But I cannot find a way to stop it from including the blank rows from the XLS into the CSV.

Please help me.

   function convertXLStoCSV($infile, $outfile) // the function that converts the file
{
    $fileType = PHPExcel_IOFactory::identify($infile);
    $objReader = PHPExcel_IOFactory::createReader($fileType);
    $objReader->setReadDataOnly(true);
    $objPHPExcel = $objReader->load($infile);

    $writer = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
    $writer->setDelimiter(",");
    $writer->setEnclosure("");
    foreach ($objPHPExcel->getWorksheetIterator() as $workSheetIndex => $worksheet) 
    {
        $objPHPExcel->setActiveSheetIndex($workSheetIndex);
        $writer->setSheetIndex($workSheetIndex);
        $writer->save('converted/' . $outfile ."_" . $worksheet->getTitle() . ".csv");


        echo $outfile;
        echo $infile;
    }
}


What I have tried:

Making a separate script that doesn't use the PHPEXCEL lib instead it uses FGETCSV to read the CSV file, remove the blank rows and then save the file again, but that didn't work, also doing this would add a lot of stress on the server as I am going to work with thousands of CSV files which contain thousands of rows.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900