在我的脚本中,我想用“浏览和输入文件”读取.xls文件,然后我不想PHPExcel读取/加载.xls并将单元格值插入到mysql中。
我让这个脚本使用静态文件:$ path ='file.xls'; 但是我无法使用上传脚本来处理我的代码。
错误: 致命错误:未捕获异常'PHPExcel_Reader_Exception',消息'无法打开阅读!文件不存在
有人可以告诉我哪里错了? 或者是否有一些我缺少的代码?
HTML选择文件:
<form class="form-horizontal well" action="php/import_excel.php" method="post" name="import_excel" enctype="multipart/form-data">
<h4>Ladda upp tabell</h4>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-primary btn-file">
Bläddra… <input type="file" multiple>
</span>
</span>
<input type="text" class="form-control" readonly>
</div>
<span class="help-block">
Välj en excel fil
</span>
<div class="pull-right">
<button type="submit" id="submit" name="Import" class="btn btn-primary button-loading" data-loading-text="Loading...">Ladda upp</button>
</div>
<br />
</form>
PHP读取并插入mysql:
//Load phpexcel includes
require '../Classes/PHPExcel.php';
require_once '../Classes/PHPExcel/IOFactory.php';
//Establish connection to mysql
$conn=mysql_connect($host,$username,$password) or die("Could not connect");
mysql_select_db($dbname,$conn) or die("could not connect database");
//Load file
if(isset($_POST["Import"])){
echo $path=$_FILES["file"]["tmp_name"];
//$path = "atbl.xls";
$objPHPExcel = PHPExcel_IOFactory::load($path);
//Loop threw file to get data
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheetTitle = $worksheet->getTitle();
$highestRow = 20; //$worksheet->getHighestRow(); // e.g. 10
$highestColumn = 'G'; //$worksheet->getHighestColumn(''); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;
//Echo file info
echo "<br>The worksheet ".$worksheetTitle." has ";
echo $nrColumns . ' columns (A-' . $highestColumn . ') ';
echo ' and ' . $highestRow . ' row.';
echo '<br>Data: <table border="1"><tr>';
//Loop threw colum, rows and cells
for ($row = 11; $row <= $highestRow; ++ $row) {
echo '<tr>';
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val = $cell->getCalculatedValue();
//$dataType = PHPExcel_Cell_DataType::dataTypeForValue($val);
echo '<td>' . $val . '<br></td>';
}
echo '</tr>';
}
echo '</table>';
}
for ($row = 11; $row <= $highestRow; ++ $row) {
$val=array();
for ($col = 0; $col < $highestColumnIndex; ++ $col) {
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val[] = $cell->getCalculatedValue();
}
//Insert data from file to mysql
$sql="INSERT INTO phpexcel(objekt_nr, objekt_rev, element_nr, element_langd, element_bredd, element_hojd)
VALUES ('".$val[0] . "','" . $val[1] . "','" . $val[2]. "','" . $val[4]. "','" . $val[5]. "','" . $val[6]. "')";
//echo $sql."\n";
mysql_query($sql) or die('Invalid query: ' . mysql_error());
}
}
?>
答案 0 :(得分:0)
HTML代码缺少名称&amp; ID:
<input type="file" name="file" id="file" multiple>