簡體   English   中英

使用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”的文件。 我想查詢顯示在excel表格中的excel表中的數據。 我怎樣才能做到這一點?

我剛剛在composer上安裝了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來執行此操作,則可以使用FileReaderLocal Storage來讀取.xls並進行解析。

您可以找到在vanilla js中實現的基本解析器:

參考是這篇文章: https : //stackoverflow.com/a/17258652/5661591

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM