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
| <?php
// importer le fichier ods dans une table mysql
// --------------------------------------------
//include the following 2 files
require 'PHPExcel/Classes/PHPExcel.php';
require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
// Connexion MYSQL
try
{
$pdo_options[PDO::ATTR_EMULATE_PREPARES] = false;
// mode exception
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$chaine_connexion='mysql:host=localhost;dbname=cemkay;charset=utf8';
$connexion_bdd = new PDO($chaine_connexion, 'root', '', $pdo_options);
}
catch(PDOException $e)
{
$msg = 'ERREUR PDO dans ' . $e->getFile() . ' L.' . $e->getLine() . ' : ' . $e->getMessage();
exit($msg);
}
// objPHPExcel
$objPHPExcel = PHPExcel_IOFactory::load("liste_image.ods");
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
{
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;
}
// requete prepare
$stmt = $connexion_bdd->prepare('INSERT INTO liste_image (id_photo, affichage, disponibilite, taille_type, type) VALUES (?,?,?,?,?)');
// insertion de chaque ligne each row
for ($row=2; $row<=$highestRow; $row++)
{
$val=array();
for ($col = 0; $col < $highestColumnIndex; ++ $col)
{
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val[$col] = $cell->getValue();
}
$stmt->execute($val);
// contrôler precisement ce qui se passe dans l'insertion
$insertion = $stmt->execute($val);
echo 'insertion de <pre>';
print_r($val);
echo '</pre><br/>Résultat : ';
var_dump($insertion);
echo '<br/>';
}
// afficher le tableau depuis le fichier ods
// -----------------------------------------
$sheet = $objPHPExcel->getSheet(0);
echo '<table border="1">';
// On boucle sur les lignes
foreach($sheet->getRowIterator() as $row) {
echo '<tr>';
// On boucle sur les cellule de la ligne
foreach ($row->getCellIterator() as $cell) {
echo '<td>';
print_r($cell->getValue());
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
// afficher les variables du script
// --------------------------------
echo '$worksheetTitle:' . $worksheetTitle . '<br>';
echo '$highestRow:' . $highestRow . '<br>';
echo '$highestColumn:' . $highestColumn . '<br>';
echo '$highestColumnIndex:' . $highestColumnIndex . '<br>';
echo '$nrColumns:' . $nrColumns . '<br>'; |