Salve,
il mio compito è quello di realizzare un programma che mi permatta di acquisire dei dati da fogli excel mandati in input tramite un form
Quanto da me scritto funziona...dovrò solamente sistemare la parte delle celle da acquisire non appena mi verrano fornite altre informazioni ma questo è un altro aspetto.
Volevo chiedere e sapere se, secondo voi, ci sono delle parti che posso essere scritte meglio o eventuali errori da me non notati.
Tipo la gestione degli errori è ben configurata oppure devo fare delle modifiche?
La parte in HTML del form è la seguente:
-------------------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Leggere xls</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="LeggereDaExcel.php">
Seleziona il file .xls da leggere:
<label for="excel"> file </label>
<input type="file" name="excel" id="excel" />
<input type="submit" value="LEGGI" />
</form>
</body>
</html>
-------------------------------------------------------------------------------------
Volevo aggiungere in questa parte un controllo che mi facesse comparire sulla stessa pagina un messagio con scritto "file caricato" nel caso in cui sia correttamente caricato oppure "Errore perchè..." nel caso non venga caricato nulla o nel caso in cui venga caricato un file diverso da .xls o .xlsx e quindi non esegua l'operazione.
Mentre il codice PHP è il seguente:
----------------------------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>LeggereDaExcel</title>
</head>
<body>
<?php
/** PHPExcel */
require_once 'Classes/PHPExcel.php';
/** PHPExcel_IOFactory */
require_once 'Classes/PHPExcel/IOFactory.php';
$inputFileName = $_FILES['excel']['name'];
$sheetname = array('Foglio1' , 'Foglio2');
/** Identify the type of $inputFileName **/
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
/** Create a new Reader of the type that has been identified **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
/** Advise the Reader of which WorkSheets we want to load **/
$objReader->setLoadSheetsOnly($sheetname);
try {
/** Load $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
die('Error loading file: '.$e->getMessage());
}
echo $objPHPExcel->getSheetCount(),' worksheet',(($objPHPExcel->getSheetCount() == 1) ? '' : 's'),' loaded
';
$loadedSheetNames = $objPHPExcel->getSheetNames();
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
echo $sheetIndex,' -> ',$loadedSheetName,'
';
$objPHPExcel->setActiveSheetIndex($sheetIndex);
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColum n);
$hCi = $highestColumnIndex - 1;
echo '<table border="1">' . "\n";
for ($row = 1; $row <= $highestRow; ++$row) {
echo '<tr>' . "\n";
for ($col = 0; $col <= $hCi; ++$col) {
echo '<td>' . $objWorksheet->getCellByColumnAndRow($col, $row)->getValue() . '</td>' . "\n";
}
echo '</tr>' . "\n";
}
echo '</table>' . "\n";
echo '
';
}
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);
?>
</body>
</html>
-----------------------------------------------------------------------------------------
La parte dell'errore è ben gestita così con il try/catch?
Ci sono altri errori?
Avete qualche consiglio da darmi o c'è qualcosa che dovrei sapere?
Grazie a tutti quelli che mi presteranno un minimo di attenzione!