What's new

Upload csv and load in table

jay41

Forum Veteran
Joined
Nov 13, 2016
Posts
1,929
Reaction
472
Points
659
Age
24
How to upload excel file and display in table using php and javascript without database
 
To upload an Excel file and display its contents in a table using PHP and JavaScript without a database, you can follow these steps:

1. Create an HTML form that allows users to upload an Excel file.
2. Write a PHP script that handles the file upload and extracts the data from the Excel file using a library like PHPExcel.
3. Convert the Excel data into a JSON object using PHP's json_encode function.
4. Use JavaScript to parse the JSON object and create an HTML table to display the data.

Here's an example code:

HTML form:

Code:
<form action="upload.php" method="post" enctype="multipart/form-data">
  <label for="file">Choose an Excel file to upload:</label>
  <input type="file" name="file" id="file">
  <input type="submit" name="submit" value="Upload">
</form>

<div id="table-container"></div>

upload.php:

PHP:
<?php
require_once 'PHPExcel/PHPExcel.php';

if (isset($_FILES['file'])) {
  $file = $_FILES['file']['tmp_name'];
  $excel = PHPExcel_IOFactory::load($file);
  $sheet = $excel->getActiveSheet();
  $data = array();

  foreach ($sheet->getRowIterator() as $row) {
    $rowData = array();
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false);

    foreach ($cellIterator as $cell) {
      $rowData[] = $cell->getValue();
    }

    $data[] = $rowData;
  }

  echo json_encode($data);
}
?>

JavaScript code:

JavaScript:
$(document).ready(function() {
  $('form').submit(function(event) {
    event.preventDefault();

    var formData = new FormData($('form')[0]);

    $.ajax({
      url: 'upload.php',
      type: 'POST',
      data: formData,
      contentType: false,
      processData: false,
      success: function(data) {
        var jsonData = JSON.parse(data);
        var tableHtml = '<table><thead><tr><th>Column 1</th><th>Column 2</th></tr></thead><tbody>';

        for (var i = 0; i < jsonData.length; i++) {
          tableHtml += '<tr><td>' + jsonData[i][0] + '</td><td>' + jsonData[i][1] + '</td></tr>';
        }

        tableHtml += '</tbody></table>';

        $('#table-container').html(tableHtml);
      }
    });
  });
});

This example code assumes that the Excel file has two columns of data. You can modify the tableHtml variable to match the actual structure of your Excel data.
 
To upload an Excel file and display its contents in a table using PHP and JavaScript without a database, you can follow these steps:

1. Create an HTML form that allows users to upload an Excel file.
2. Write a PHP script that handles the file upload and extracts the data from the Excel file using a library like PHPExcel.
3. Convert the Excel data into a JSON object using PHP's json_encode function.
4. Use JavaScript to parse the JSON object and create an HTML table to display the data.

Here's an example code:

HTML form:

Code:
<form action="upload.php" method="post" enctype="multipart/form-data">
  <label for="file">Choose an Excel file to upload:</label>
  <input type="file" name="file" id="file">
  <input type="submit" name="submit" value="Upload">
</form>

<div id="table-container"></div>

upload.php:

PHP:
<?php
require_once 'PHPExcel/PHPExcel.php';

if (isset($_FILES['file'])) {
  $file = $_FILES['file']['tmp_name'];
  $excel = PHPExcel_IOFactory::load($file);
  $sheet = $excel->getActiveSheet();
  $data = array();

  foreach ($sheet->getRowIterator() as $row) {
    $rowData = array();
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false);

    foreach ($cellIterator as $cell) {
      $rowData[] = $cell->getValue();
    }

    $data[] = $rowData;
  }

  echo json_encode($data);
}
?>

JavaScript code:

JavaScript:
$(document).ready(function() {
  $('form').submit(function(event) {
    event.preventDefault();

    var formData = new FormData($('form')[0]);

    $.ajax({
      url: 'upload.php',
      type: 'POST',
      data: formData,
      contentType: false,
      processData: false,
      success: function(data) {
        var jsonData = JSON.parse(data);
        var tableHtml = '<table><thead><tr><th>Column 1</th><th>Column 2</th></tr></thead><tbody>';

        for (var i = 0; i < jsonData.length; i++) {
          tableHtml += '<tr><td>' + jsonData[i][0] + '</td><td>' + jsonData[i][1] + '</td></tr>';
        }

        tableHtml += '</tbody></table>';

        $('#table-container').html(tableHtml);
      }
    });
  });
});

This example code assumes that the Excel file has two columns of data. You can modify the tableHtml variable to match the actual structure of your Excel data.
can you please complete this code.
 
can you please complete this code.
Sure, here's the complete code:

HTML form:

Code:
<form action="upload.php" method="post" enctype="multipart/form-data">
  <label for="file">Choose an Excel file to upload:</label>
  <input type="file" name="file" id="file">
  <input type="submit" name="submit" value="Upload">
</form>

<div id="table-container"></div>

upload.php:

PHP:
<?php
require_once 'PHPExcel/PHPExcel.php';

if (isset($_FILES['file'])) {
  $file = $_FILES['file']['tmp_name'];
  $excel = PHPExcel_IOFactory::load($file);
  $sheet = $excel->getActiveSheet();
  $data = array();

  foreach ($sheet->getRowIterator() as $row) {
    $rowData = array();
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false);

    foreach ($cellIterator as $cell) {
      $rowData[] = $cell->getValue();
    }

    $data[] = $rowData;
  }

  echo json_encode($data);
}
?>

JavaScript code:

JavaScript:
$(document).ready(function() {
  $('form').submit(function(event) {
    event.preventDefault();

    var formData = new FormData($('form')[0]);

    $.ajax({
      url: 'upload.php',
      type: 'POST',
      data: formData,
      contentType: false,
      processData: false,
      success: function(data) {
        var jsonData = JSON.parse(data);
        var tableHtml = '<table><thead><tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr></thead><tbody>';

        for (var i = 0; i < jsonData.length; i++) {
          tableHtml += '<tr><td>' + jsonData[i][0] + '</td><td>' + jsonData[i][1] + '</td><td>' + jsonData[i][2] + '</td></tr>';
        }

        tableHtml += '</tbody></table>';

        $('#table-container').html(tableHtml);
      }
    });
  });
});

Note that you should modify the tableHtml variable to match the actual structure of your Excel data, based on the number of columns and their contents.
 
Sure, here's the complete code:

HTML form:

Code:
<form action="upload.php" method="post" enctype="multipart/form-data">
  <label for="file">Choose an Excel file to upload:</label>
  <input type="file" name="file" id="file">
  <input type="submit" name="submit" value="Upload">
</form>

<div id="table-container"></div>

upload.php:

PHP:
<?php
require_once 'PHPExcel/PHPExcel.php';

if (isset($_FILES['file'])) {
  $file = $_FILES['file']['tmp_name'];
  $excel = PHPExcel_IOFactory::load($file);
  $sheet = $excel->getActiveSheet();
  $data = array();

  foreach ($sheet->getRowIterator() as $row) {
    $rowData = array();
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false);

    foreach ($cellIterator as $cell) {
      $rowData[] = $cell->getValue();
    }

    $data[] = $rowData;
  }

  echo json_encode($data);
}
?>

JavaScript code:

JavaScript:
$(document).ready(function() {
  $('form').submit(function(event) {
    event.preventDefault();

    var formData = new FormData($('form')[0]);

    $.ajax({
      url: 'upload.php',
      type: 'POST',
      data: formData,
      contentType: false,
      processData: false,
      success: function(data) {
        var jsonData = JSON.parse(data);
        var tableHtml = '<table><thead><tr><th>Column 1</th><th>Column 2</th><th>Column 3</th></tr></thead><tbody>';

        for (var i = 0; i < jsonData.length; i++) {
          tableHtml += '<tr><td>' + jsonData[i][0] + '</td><td>' + jsonData[i][1] + '</td><td>' + jsonData[i][2] + '</td></tr>';
        }

        tableHtml += '</tbody></table>';

        $('#table-container').html(tableHtml);
      }
    });
  });
});

Note that you should modify the tableHtml variable to match the actual structure of your Excel data, based on the number of columns and their contents.
can you provide the code inside PHPExcel.php
 
can you provide the code inside PHPExcel.php
I apologize for the confusion earlier. There is no code inside PHPExcel.php as it is a library file and contains the classes and functions necessary to read, write and manipulate Excel files. You can download it from the official PHPExcel GitHub repository and include it in your PHP project.

Here's an example code of how to use PHPExcel to read an Excel file and output its contents:

PHP:
<?php
require_once 'PHPExcel/PHPExcel.php';

$inputFileName = 'example.xlsx';

// Load the Excel file
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);

// Get the active worksheet
$worksheet = $objPHPExcel->getActiveSheet();

// Get the highest row and column numbers
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();

// Convert the highest column letter to a number
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

// Loop through each row of the worksheet
$data = array();
for ($row = 1; $row <= $highestRow; ++$row) {
  // Loop through each column of the row
  $rowData = array();
  for ($col = 0; $col < $highestColumnIndex; ++$col) {
    // Get the cell value
    $value = $worksheet->getCellByColumnAndRow($col, $row)->getValue();

    // Add the cell value to the row data
    $rowData[] = $value;
  }

  // Add the row data to the data array
  $data[] = $rowData;
}

// Output the data as a JSON object
echo json_encode($data);
?>

This code reads an Excel file named "example.xlsx", loops through each row and column of the worksheet, and outputs the cell values as a JSON array. You can modify this code to match your specific needs, such as changing the input file name or outputting the data in a different format.
 
Back
Top