0

我正在尝试使用 PHP 以编程方式删除 CSV 文件中的空白行。文件上传到站点并使用 PHPExcel 转换为 CSV。正在生成一种特定类型的 CSV,数据行之间有空行,我正在尝试用 PHP 清理它们,但没有任何运气。这是此 CSV 的示例:https ://gist.github.com/vinmassaro/467ea98151e26a79d556

我需要使用 PHPExcel 或标准 PHP 函数加载 CSV,删除空白行并保存它。提前致谢。

编辑:这是当前如何使用 PHPExcel 转换的片段。这是 Drupal 钩子的一部分,作用于刚刚上传的文件。我无法让 PHPExcelremoveRow方法工作,因为它似乎不适用于空行,只能用于空数据行。

// Load the PHPExcel IOFactory.
require_once(drupal_realpath(drupal_get_path('module', 'custom')) . '/PHPExcel/Classes/PHPExcel/IOFactory.php');

// Load the uploaded file into PHPExcel for normalization.
$loaded_file = PHPExcel_IOFactory::load(drupal_realpath($original_file->uri));
$writer = PHPExcel_IOFactory::createWriter($loaded_file, 'CSV');
$writer->setDelimiter(",");
$writer->setEnclosure("");

// Get path to files directory and build a new filename and filepath.
$files_directory = drupal_realpath(variable_get('file_public_path', conf_path() . '/files'));
$new_filename = pathinfo($original_file->filename, PATHINFO_FILENAME) . '.csv';
$temp_filepath = $files_directory . '/' . $new_filename;

// Save the file with PHPExcel to the temp location. It will be deleted later.
$writer->save($temp_filepath);
4

3 回答 3

2

如果您想使用 phpexcel,请搜索 CSV.php 并编辑此文件:

// Write rows to file
for ($row = 1; $row <= $maxRow; ++$row) {
    // Convert the row to an array...
    $cellsArray = $sheet->rangeToArray('A'.$row.':'.$maxCol.$row, '', $this->preCalculateFormulas);
    // edit by ger
    //  if last row, then no linebreak will added
    if($row == $maxRow){ $ifMaxRow = TRUE; }else{ $ifMaxRow = False; }
    // ... and write to the file
    $this->writeLine($fileHandle, $cellsArray[0], $ifMaxRow);
}

在文件末尾编辑这个

 /**
 *  Write line to CSV file
 *  
 *  edit by ger
 *  
 *  @param    mixed    $pFileHandle    PHP filehandle
 *  @param    array    $pValues        Array containing values in a row
 *  @throws    PHPExcel_Writer_Exception
 */
private function writeLine($pFileHandle = null, $pValues = null, $ifMaxRow = false)
{
...
            // Add enclosed string
            $line .= $this->enclosure . $element . $this->enclosure;
        }

insert this following     

        if($ifMaxRow == false){
           // Add line ending
           $line .= $this->lineEnding;
        }
于 2016-04-21T12:36:30.410 回答
1

在 Mihai Iorga 的评论中使用str_replaceas 将起作用。就像是:

$csv = file_get_contents('path/file.csv');
$no_blanks = str_replace("\r\n\r\n", "\r\n", $csv);
file_put_contents('path/file.csv', $no_blanks);

我从您发布的示例中复制了文本,这很有效,尽管我不得不将“find”参数更改为 to"\r\n \r\n"而不是因为每个看起来空白"\r\n\r\n"上都有一个空格。

于 2013-10-21T15:08:40.877 回答
0

尝试这个:

<?php

$handle = fopen("test.csv", 'r'); //your csv file
$clean = fopen("clean.csv", 'a+'); //new file with no empty rows

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    if($num > 1)
    fputcsv($clean, $data ,";");
}
fclose($handle);
fclose($clean);

?>

在我的本地主机上测试。

输出数据:

初始文件:

col1,col2,col3,col4,col5,col6,col7,col8

0,229.500,7.4,3165.5,62,20.3922,15.1594,0

1,229.600,8.99608,3156.75,62,15.6863,16.882,0

2,229.700,7.2549,3130.25,62,16.8627,15.9633,0

3,229.800,7.1098,3181,62,17.2549,14.1258,0

清理 CSV 文件:

col1    col2    col3    col4    col5    col6    col7    col8
0       229.500 7.4     3165.5    62    203.922 151.594 0
1       229.600 899.608 3156.75   62    156.863 16.882  0
2       229.700 72.549  3130.25   62    168.627 159.633 0
3       229.800 71.098  3181      62    172.549 141.258 0
于 2013-10-21T14:46:07.860 回答