使用 php 导入 excel 文件并将其打印在带有代码示例的 html 表中


Import an excel file using php and print it in a html table with example of code

我尝试使用phpoffice/phpexcel来做到这一点,但没有成功。我在 html 中有这个表格。

<body>
    <div align="center">ELENCA TABELLE PRESENTI NEL DB</div>
        <form action="index.php" method="post"
        enctype="multipart/form-data">
<table>
    <tr>
        <td>
            Filename:
        </td>
        <td>
            <input type="file" name="file" id="file">
        </td>
    </tr>
    <tr>
        <td colspan="2" align="right">
            <input type="submit" name="submit" value="Submit">
        </td>
    </tr>
</table>
</form>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
        <script src="/include/js/bootstrap-contextmenu.js"></script>
        <script type="text/javascript" src="/include/js/bootflat.min.js"></script>
        <script type="text/javascript" src="https://cdn.datatables.net/s/bs/pdfmake-0.1.18,dt-1.10.10,af-2.1.0,b-1.1.0,b-colvis-1.1.0,b-html5-1.1.0,b-print-1.1.0,cr-1.3.0,fc-3.2.0,fh-3.1.0,kt-2.1.0,r-2.0.0,rr-1.1.0,sc-1.4.0,se-1.1.0/datatables.min.js"></script>
</body>

我想用它来上传一个 excel 文件和另一个名为"index.php"的文件。我想做一个查询来显示我上传到 html 表中的这个 excel 文件中的数据。我该怎么做?

我刚刚安装了带有作曲家的软件包phpoffice/phpexcel。我可以使用此包的库

现在我这样做,但它不起作用。怎么了?

if(isset($_POST["Import"])){
echo $path=$_FILES["file"]["tmp_name"];
$path = "/home/b2bmomo/www";
$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>';
}

如果你不介意使用JS而不是PHP来做到这一点,你可以使用FileReader和Local Storage来读取你的.xls并解析它。

你可以找到用vanilla js实现的基本解析器:

  • http://oss.sheetjs.com/js-xls/(XLS 文件,您想要的(
  • http://oss.sheetjs.com/js-xlsx/(XLSX/XLSM/XLSB 文件(

参考是这篇文章:https://stackoverflow.com/a/17258652/5661591