PHP Classes

Read Excel content for database: Read data from Excel file and insert in database

Recommend this page to a friend!
  All requests RSS feed  >  Read Excel content for database  >  Request new recommendation  >  A request is featured when there is no good recommended package on the site when it is posted. Featured requests  >  No recommendations No recommendations  

Read Excel content for database

Edit

Picture of Eweck by Eweck - 11 months ago (2024-12-07)

Read data from Excel file and insert in database

This request is clear and relevant.
This request is not clear or is not relevant.

+3

Read data from excel file (template) and insert them in database. Should contain error management for required column, data format and control of empty lines

  • 2 Clarification requests
  • 2. Picture of ASCOOS CMS by ASCOOS CMS - 11 months ago (2024-12-09) Reply

    Use the best library available, PhpSpreadsheet.

    Here's a sample of use (one of the basic examples), a little tailored to your own requirements.

    <?php require 'vendor/autoload.php';

    use PhpOffice\PhpSpreadsheet\IOFactory;

    $inputFileName = 'path_to_excel_file.xlsx';

    $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database";

    $conn = new mysqli($servername, $username, $password, $dbname);

    if ($conn->connect_error) {

    die("Connection failed: " . $conn->connect_error);
    

    }

    try {

    $spreadsheet = IOFactory::load($inputFileName);
    

    } catch (Exception $e) {

    die('Error loading file: '.$e->getMessage());
    

    }

    $sheet = $spreadsheet->getActiveSheet(); $highestRow = $sheet->getHighestRow(); $highestColumn = $sheet->getHighestColumn();

    // Read data from Excel, check for errors and import into the database for ($row = 2; $row <= $highestRow; $row++) {

    $data1 = $sheet->getCell('A'.$row)->getValue();
    $data2 = $sheet->getCell('B'.$row)->getValue();
    $data3 = $sheet->getCell('C'.$row)->getValue();
    
    // Check empty lines
    if (empty($data1) && empty($data2) && empty($data3)) {
        continue;
    }
    

    // Check data format (e.g., if a number is required)
    if (!is_numeric($data1)) {
        die("Error: Data in column A, row $row is not numeric.");
    }
    

    // Add to the database
    $sql = "INSERT INTO table_name (column1, column2, column3) VALUES ('$data1', '$data2', '$data3')";
    
    if (!$conn->query($sql)) {
        die("Error inserting data: " . $conn->error);
    }
    

    }

    echo "Data imported successfully.";

    $conn->close(); ?>

    • 1. Picture of Cedric Maenetja by Cedric Maenetja - 11 months ago (2024-12-09) Reply

      I am not sure if a package for this requirement is needed. I think this is a very specific request, for a specific purpose. Instead of requesting a package for this, maybe have a look at PHP-ExcelReader or PHPExcel to read the documents and do the inserts.

      Ask clarification

      1 Recommendation

      Basic Excel: Import and export Excel files to XLS, XLSX and CSV

      This package can import and export Excel files to XLS, XLSX and CSV file formats.

      It provides reader and writer factory classes that can create specific objects of classes depending on the file format that is meant to be imported or exported.

      Currently it provides specific classes for reading or writing data to CSV, XLS or XLSX formats.
      This recommendation solves the problem.
      This recommendation does not solve the problem.

      +1

      Picture of Manuel Lemos by Manuel Lemos Reputation 27250 - 3 months ago (2025-08-15) Comment

      This package can read data from Excel spreadsheet files.

      Then you need to insert the data in a database using a database access class like PDO or another package for that purpose.


      Recommend package
      : 
      :