PHP Classes

PHP Merge Excel Files Merge Excel Files To One Per Column: Create an Excel file from other spreadsheet values

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 74 All time: 10,213 This week: 488Up
Version License PHP version Categories
mergeexcelfilestoone 1.0.0GNU General Publi...5Files and Folders
Description 

Author

This package can create an Excel file from other spreadsheet values.

It provides a script that uses Excel reader classes to parse multiple Excel spreadsheets in XLSX format to extract the values of their cells.

The script can combine the values of all the parsed spreadsheets and generates a single spreadsheet file in Excel XLSX format that merges the importance of all spreadsheets into one with a sheet with the values of other spreadsheets in multiple columns.

Innovation Award
PHP Programming Innovation award winner
June 2023
Winner
Excel is a popular tool for creating spreadsheets. It saves spreadsheet data in XML-based format, which usually is called XLSX.

This XML format makes it easy for PHP developers to process and create spreadsheet files in Excel XSLX format.

This package provides a script that uses an Excel parser and generator classes to show how to parse several Excel spreadsheet files and combine their values to generate a new spreadsheet that merges the importance of all spreadsheets in a single spreadsheet.

This way, other developers can learn to process multiple spreadsheets and combine them in a way that is convenient for their application purposes.

Manuel Lemos
Picture of Max Base
  Performance   Level  
Name: Max Base <contact>
Classes: 5 packages by
Country: United States United States
Age: ???
All time rank: 2602358 in United States United States
Week rank: 192 Up22 in United States United States Up
Innovation award
Innovation award
Nominee: 3x

Winner: 1x

Example

<?php
// Max Base
// https://github.com/BaseMax/MergeExcelFilesToOnePerColumns
// 2021-04-13, 2021-04-22

require "excel.php";
require
"excel-create.php";

$authors = [];
$counts = [];
$data = [];

$prefix = "input-first/";
$prefix = "input-second/";

$files = glob($prefix . "*.xlsx");
$files_count = 0;

$count = 65;
$count = 39;

// foreach($files as $file) {
for($i=1;$i<=$count;$i++) {
   
$file = $prefix . $i.".xlsx";
    if(
$file === "" || $file === "." || $file === "..") {
        continue;
    }
   
// if($files_count > 5) {
    // // break;
    // }
   
$files_count++;
   
// print $file."\n";
   
if($xlsx = SimpleXLSX::parse($file) ) {
       
$rows = $xlsx->rows();
       
// print_r($rows);
       
$values = [];
        foreach(
$rows as $row) {
            if(isset(
$row[1])) {
               
$values[] = $row[1];
            }
            else {
               
$values[] = "";
            }
        }
       
// print_r($values);
       
$counts[] = (int) count($values);
       
$authors[] = $values;
       
// exit();
        // $values = [];
        // foreach($rows as $row) {
        // print_r($row);
        // if(isset($row[0], $row[1]) && $row[0] !== "" && $row[1] !== "") {
        // $row[1] = $row[1];
        // $values[] = $row[1];
        // }
        // }
        // // print_r($values);
        // $counts[] = (int) count($values);
        // $authors[] = $values;
        // exit();
   
} else {
        echo
SimpleXLSX::parseError();
    }
}

// print_r($authors);

$m = max($counts);
// print $m."\n";
// exit();
$c = count($authors);
// print count($authors[0])."\n";
// exit();

for($i=1;$i<=$m;$i++) {
   
$data[$i-1] = [];
}

for(
$i=1;$i<=$m;$i++) {
    for(
$j=1;$j<=$c;$j++) {
       
$data[$i-1][$j-1] = "";
        if(isset(
$authors[$j-1][$i-1])) {
           
$data[$i-1][$j-1] = $authors[$j-1][$i-1];
        }
    }
}
// print_r($data);
// exit();

// for($i=1;$i<=$c;$i++) {
// for($j=1;$j<=$m;$j++) {
// if(isset($authors[$i-1][$j])) {
// print $authors[$i-1][$j];
// }
// print "\t";
// }
// print "\n";
// }

// exit();

// for($j=0;$j<500;$j++) {
// $has = false;
// for($i=0;$i<$files_count;$i++) {
// print $i."\t";
// if(isset($authors[$i][$j])) {
// $authors[$i][$j] = trim($authors[$i][$j]);
// if($authors[$i][$j] !== "") {
// $has = true;
// print $authors[$i][$j];
// print "\t";
// }
// }
// }
// if($has === true) {
// print "\n";
// }
// }

$xlsx = SimpleXLSXGen::fromArray($data);
$xlsx->saveAs('res.xlsx');



Details

Merge Excel Files to single excel file per columns

Using

Set your directory name at https://github.com/BaseMax/MergeExcelFilesToOnePerColumns/blob/main/parser.php#L14, and number of files at https://github.com/BaseMax/MergeExcelFilesToOnePerColumns/blob/main/parser.php#L20.

Your directory should look like: 1.xlsx, 2.xlsx, 3.xlsx, 4.xlsx, etc....

Now convert all excel files to a single file by running:

php parser.php

Next open res.xlsx file in your excel or libreoffice...

© Copyright 2021 Max Base


  Files folder image Files (5)  
File Role Description
Plain text file excel-create.php Class Class source
Plain text file excel.php Class Class source
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file parser.php Example Example script
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:74
This week:0
All time:10,213
This week:488Up