1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
| <form method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="display" value="Afficher">
<input type="submit" name="insert" value="Inserer">
</form>
<?php
require '..\PhpSpreadsheet\vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
// Connexion à la base de données Oracle
$username = 'user';
$password = 'pwd';
$host = 'hostname';
$connname = 'dbname';
$conn = oci_connect(...............................);
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
// Récupérer la feuille active (la première)
if (isset($_FILES['file'])) {
$file = $_FILES['file']['tmp_name'];
try {
$spreadsheet = IOFactory::load($file);
$worksheet = $spreadsheet->getActiveSheet();
echo $file;
if(isset($_FILES['file'])){
echo "Le fichier est chargé";
}
if (isset($_POST['display'])) {
echo $file;
if(!isset($worksheet)){
echo "Veuillez importer le fichier avant";
exit;
}
// Récupérer la valeur de la cellule A7
$cellA7 = $worksheet->getCell('A7')->getValue();
// Récupérer la valeur de la cellule B7
$cellB7 = $worksheet->getCell('B7')->getValue();
// Afficher les valeurs des cellules A7 et B7 sur la page
echo "La valeur de la cellule A7 est : " . $cellA7 . "<br>";
echo "La valeur de la cellule B7 est : " . $cellB7;
}
if (isset($_POST['insert'])) {
if(!isset($worksheet)){
echo "You need to import the excel file first.";
exit;
}
// Récupérer toutes les données du tableau
$rows = $worksheet->toArray();
// Boucle pour insérer les données dans la première table
foreach ($rows as $row) {
$sql = "INSERT INTO table1 (col1, col2, col3) VALUES (:col1, :col2, :col3)";
$stmt = oci_parse($conn, $sql);
oci_bind_by_name($stmt, ":col1", $row[0]);
oci_bind_by_name($stmt, ":col2", $row[1]);
oci_bind_by_name($stmt, ":col3", $row[2]);
oci_execute($stmt);
}
// Boucle pour insérer les données dans la deuxième table
foreach ($rows as $row) {
$sql = "INSERT INTO table2 (col1, col2, col3) VALUES (:col1, :col2, :col3)";
$stmt = oci_parse($conn, $sql);
oci_bind_by_name($stmt, ":col1", $row[0]);
oci_bind_by_name($stmt, ":col2", $row[1]);
oci_bind_by_name($stmt, ":col3", $row[2]);
oci_execute($stmt);
}
}
} catch (Exception $e) {
echo 'Veuillez choisir un fichier: ', $e->getMessage(), "\n";
}
} |
Partager