Merge csv files [closed]

2019-03-06 07:16发布

问题:

How to merge multiple CSV files in to one csv files in PHP or joomla?

Merge all data from the csv files in a folder into a text file with a few small changes you can also use this for txt files. Replace *.csv for *.txt

回答1:

To do this in PHP you would do something in the lines of:

  1. Open a file handle where the merged csv data can be written to
  2. Read all the filenames from the source dir
  3. For every file that ends with ".csv", append its content to the merge file

Ex.

$csvdir = './csvdir';
$result = fopen('./merge.csv', 'w');

if ($handle = opendir($csvdir)) {
    while (false !== ($entry = readdir($handle))) {
        if (substr($entry, -4) === ".csv") {
            $csvcontent = file_get_contents($entry);
            fwrite($result, $csvcontent);
        }
    }

    closedir($handle);
}

fclose($result);


回答2:

Merge all data from the csv files in a folder into a text file

Note: with a few small changes you can also use this for txt files. Replace *.csv for *.txt

1) Windows Start Button | Run 2) Type cmd and hit enter ("command" in Win 98) 3) Go to the folder with the CSV files (for help how to do that enter "help cd") 4) Type copy *.csv all.txt and hit enter to copy all data in the files into all.txt. 5) Type exit and hit enter to close the DOS window

Now we must import the text file all.txt into Excel.

1) Open Excel 2) When you use File Open to open all.txt the Text Import Wizard will help you import the file 3) Choose Delimited 4) Next 5) Check Comma 6) Finish