Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want (and i have but dont work!) to do this

1. user can upload excel file
2. check if the file is excel
3. of the file is excel this be visible on html page
4. the user can edit the excel file
5. all edits i want to saved in real time and on excel file

i have create on xampp\\htdocs a file with name index
on xampp\\htdocs\\index i have create

3 php files:
index.php
save_excel.php
upload_excel.php

1 json file:
composer.json

1 file with name upload

and i have download the https://github.com/PHPOffice/PhpSpreadsheet and i have the the file "src"

i have download the Composer-Setup.exe and i have run on cmd two order composer init and composer require phpoffice/phpspreadsheet.

My problem is that this i want to do, dont did it.
Down i give you and my code to tell me if i have something wrong.

What I have tried:

index.php:
<!DOCTYPE html>
<html>
<head>
   <meta charset="UTF-8">
   <title>Edit Excel File</title>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
   <script>
   $(document).ready(function(){
       $('td').attr('contenteditable', true); // επιτρέπει την επεξεργασία των κελιών του πίνακα
       $('td').on('focus', function(){
           $(this).data('before', $(this).html());
       });
       $('td').on('blur', function(){
           if ($(this).data('before') !== $(this).html()) {
               var row = $(this).parent().index() + 1;
               var col = $(this).index() + 1;
               var value = $(this).text();
               $.ajax({
                   url: 'save_excel.php', // ορίζει τη διαδρομή για την αποθήκευση των δεδομένων στον server
                   type: 'post',
                   data: {row: row, col: col, value: value}, // στέλνει τα δεδομένα στον server ως POST request
                   success: function(response){
                       console.log(response);
                   },
                   error: function(xhr){
                       console.log(xhr.responseText);
                   }
               });
           }
       });
   });
   </script>
</head>
<body>
   <h1>Edit Excel File</h1>
   <form method="post" action="upload_excel.php" enctype="multipart/form-data">
       <input type="file" name="file">
       <input type="submit" name="submit" value="Upload">
   </form>
   <?php
   if(isset($_SESSION['filename'])) { // ελέγχει αν έχει μεταφορτωθεί αρχείο
       echo '<table>';
       $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($_SESSION['filename']);
       $worksheet = $spreadsheet->getActiveSheet();
       foreach($worksheet->getRowIterator() as $row) {
           echo '<tr>';
           $cellIterator = $row->getCellIterator();
           $cellIterator->setIterateOnlyExistingCells(false);
           foreach($cellIterator as $cell) {
               echo '<td>' . $cell->getValue() . '</td>'; // εμφανίζει τα δεδομένα του πίνακα
           }
           echo '</tr>';
       }
       echo '</table>';
   }
   ?>
</body>
</html>



save_excel.php:

<?php
// Συμπερίληψη της βιβλιοθήκης PhpSpreadsheet
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;

// Λήψη του αριθμού γραμμής, αριθμού στήλης και της νέας τιμής κελιού από το αίτημα AJAX
$row = $_POST['row'];
$col = $_POST['col'];
$value = $_POST['value'];

// Φόρτωση του αρχείου Excel
$filename = 'example.xlsx';
$spreadsheet = IOFactory::load($filename);

// Λήψη του ενεργού φύλλου εργασίας
$worksheet = $spreadsheet->getActiveSheet();

// Ορισμός της τιμής του επεξεργασμένου κελιού
$worksheet->setCellValueByColumnAndRow($col, $row, $value);

// Αποθήκευση του ενημερωμένου αρχείου Excel
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($filename);

// Επιστροφή μηνύματος επιτυχίας στο αίτημα AJAX
echo 'Cell updated successfully!';
?>




upload_excel.php:

<?php
// Έλεγχος εάν ανέβηκε ένα αρχείο
if(isset($_FILES['file'])) {
    // Ορισμός επιτρεπόμενων τύπων αρχείων
    $allowed_extensions = array('xls', 'xlsx');
    // Λήψη της επέκτασης του αρχείου
    $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
    // Έλεγχος εάν ο τύπος αρχείου είναι επιτρεπόμενος
    if(in_array($extension, $allowed_extensions)) {
        // Αποθήκευση του ανεβασμένου αρχείου σε προσωρινή τοποθεσία
        $tmp_name = $_FILES['file']['tmp_name'];
        $filename = basename($_FILES['file']['name']);
        $file_path = 'uploads/' . $filename;
        move_uploaded_file($tmp_name, $file_path);
        // Έναρξη συνόδου και αποθήκευση ονόματος αρχείου
        session_start();
        $_SESSION['filename'] = $file_path;
        // Ανακατεύθυνση πίσω στο index.php
        header('Location: index.php');
        exit;
    } else {
        // Ο τύπος αρχείου δεν επιτρέπεται, ανακατεύθυνση πίσω στο index.php με μήνυμα σφάλματος
        header('Location: index.php?error=invalid_file_type');
        exit;
    }
}
?>




composer.json:

{
    "require": {
        "phpoffice/phpspreadsheet": "^1.18.0"
    }
}
Posted
Comments
Andre Oosthuizen 28-Feb-23 7:03am    
Please specify exactly what you want your code to do, this is a lot of code to read through to try and find if it is correct. Also tell us on which line did your error happen, it will make it easier for us to assist.

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