PHPExcel通过xls文件上传数据

时间:2015-04-30 11:30:12

标签: php phpexcel

你知道我在做错了什么吗?我尝试做的是从选择框中选择一个值,然后在我点击提交时上传一个xls文件,然后将其上传到MySQL。但是当我点击提交时,我收到了无效的帖子请求。

require '../PHPExcel/Classes/PHPExcel.php';
require_once '../PHPExcel/Classes/PHPExcel/IOFactory.php';
$proposal = "select * from proposal";
$rsExport = mysqli_query($DBCONN, $proposal) or die(mysql_error());
echo '<form id="proposal_form" method="post">';
echo "<select id='select_your_proposal' name = 'select_your_proposal'>";
echo "<option value=''>Select Your Proposal</option>";
while ($row = mysqli_fetch_array($rsExport)) {
    echo "<option value='" . $row['id'] . "'>" . $row['enterpriseid'] . " - " . $row['sitename'] . " - " . $row['presaleconsultant'] . " </option>";
}
echo "</select>";
?>
<br>
<form method="post" enctype="multipart/form-data">
1.) Upload User File: <input type="file" name="spreadsheet"/>
<br>
<br>

<?php
if (isset($_POST['select_your_proposal'])) {
    $proposal2 = mysqli_query($DBCONN, "select * from proposal where id = '" . $_POST['select_your_proposal'] . "'");
    $row = $proposal2->fetch_assoc();
    $proposalid = $_POST['select_your_proposal'];
    unset($sessionid);
    $sessionid = uniqid();
    $user = CurrentUserName();
    $date = date('Y-m-d H:i:s');
    ini_set('display_errors', 1);

    // Check valid spreadsheet has been uploaded
    if (isset($_FILES['spreadsheet'])) {
        if ($_FILES['spreadsheet']['tmp_name']) {
            if (!$_FILES['spreadsheet']['error']) {
                $inputFile = $_FILES['spreadsheet']['tmp_name'];
                $extension = strtoupper(pathinfo($inputFile, PATHINFO_EXTENSION));
                if ($extension == 'XLSX' || $extension == 'XLS') {
                    $objPHPExcel = PHPExcel_IOFactory::load($inputFile);
                    foreach($objPHPExcel->getWorksheetIterator() as $worksheet) {
                        $worksheetTitle = $worksheet->getTitle();
                        $highestRow = $worksheet->getHighestRow(); // e.g. 10
                        $highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
                        $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
                        $nrColumns = ord($highestColumn) - 64;
                        echo "<br>The worksheet " . $worksheetTitle . " has ";
                        echo $nrColumns . ' columns (A-' . $highestColumn . ') ';
                        echo ' and ' . $highestRow . ' row.';
                        echo '<br>Data: <table border="1"><tr>';
                        for ($row = 1; $row <= $highestRow; ++$row) {
                            echo '<tr>';
                            for ($col = 0; $col < $highestColumnIndex; ++$col) {
                                $cell = $worksheet->getCellByColumnAndRow($col, $row);
                                $val = $cell->getValue();
                                $dataType = PHPExcel_Cell_DataType::dataTypeForValue($val);
                                echo '<td>' . $val;
                            }
                            echo '</tr>';
                        }
                        echo '</table>';
                    }
                    for ($row = 2; $row <= $highestRow; ++$row) {
                        $val = array();
                        for ($col = 0; $col < $highestColumnIndex; ++$col) {
                            $cell = $worksheet->getCellByColumnAndRow($col, $row);
                            $val[] = $cell->getValue();
                        }
                        $sqlsession = "INSERT IGNORE INTO session (
            xxxx
 ";
                        $sqluser = "INSERT INTO bw_users (
            xxxx
        );";
                        //  echo $sqluser."\n";
                        //  echo $sqlsession."\n";
                        $result = mysqli_query($DBCONN, $sqlsession);
                        $result = mysqli_query($DBCONN, $sqluser);
                        if (!$result) {
                            echo "DB Error, could not query the database\n";
                            echo 'MySQL Error: ' . mysql_error();
                            exit;
                        }
                    }
                }
            }
        }
    }
    else {
    echo "Please upload an XLSX or XLS file";
    }

}
echo "<br>";
echo "</form>";
echo '<input type="submit" name="submit" value="Submit">';
?>

1 个答案:

答案 0 :(得分:0)

您需要在代码看起来像dis的形式中定义操作 <form method="post" enctype="multipart/form-data" action="yournextpage.php"> 1.) Upload User File: <input type="file" name="spreadsheet"/>

或者使用一些javascript来重定向页面