使用PHPEXCEL lib将XLS转换为CSV,但不将空行传输到CSV文件中 [英] Convert XLS to CSV using PHPEXCEL lib but without transferring the blank rows into the CSV file

查看:115
本文介绍了使用PHPEXCEL lib将XLS转换为CSV,但不将空行传输到CSV文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码将XLS文件的每个工作表转换为CSV文件。但我找不到办法阻止它将XLS中的空白行包含在CSV中。



请帮助我。



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;
    }
}





我的尝试:



制作一个不使用PHPEXCEL lib的单独脚本,而是使用FGETCSV读取CSV文件,删除空行然后再次保存文件,但这不起作用,这样做会增加服务器上的压力,因为我将使用包含数千行的数千个CSV文件。



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.

推荐答案

infile,


outfile)//转换文件的函数
{
outfile) // the function that converts the file {


fileType = PHPExcel_IOFactory :: identify(
fileType = PHPExcel_IOFactory::identify(


这篇关于使用PHPEXCEL lib将XLS转换为CSV,但不将空行传输到CSV文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆