duan19805 2014-10-01 12:13
浏览 358
已采纳

致命错误:未捕获异常'PHPExcel_Reader_Exception',消息'无法打开阅读! 文件不存在

In my script i´d like to read a .xls file with "browse & input file", then i wan´t PHPExcel to read/load the .xls and insert the cell values into mysql.

I had this script working with static file: $path = 'file.xls'; But i can´t get the upload script to work with my code.

ERROR: Fatal error: Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open for reading! File does not exist

Can someone please tell me what´s wrong? Or is there some code i'm missing?

HTML Select file:

<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&hellip; <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 Read and insert to 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."
";
mysql_query($sql) or die('Invalid query: ' . mysql_error());
}
}
?>
  • 写回答

1条回答 默认 最新

  • duanfengwang9157 2014-10-02 06:49
    关注

    HTML code was missing name & id:

    <input type="file" name="file" id="file" multiple>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决