PHP / HTML Excel导出按钮

时间:2017-12-21 10:49:53

标签: php html excel forms button

我需要创建一个按钮,当单击该按钮时,会将页面上HTML表单中的所有数据导出到excel文件。任何人都可以帮助我,我是PHP新手,不知道从哪里开始。

        <div class="left col-md-12 form-group" >


            <form method="post" id="triallesson_form">
                <div class="table">

                <table class="table table-header-rotated table-condensed table-striped table-hover"  style="width: 100%;">
                    <th></th>
                    <th class="col-xs-1 not-rotate"><div><span><?= text::$string['person_name']            ;?></span></div>  </th>

                    <th class="col-xs-1 not-rotate"><div><span><?= text::$string['person_telphone']        ;?></span></div>  </th>
                    <th class="col-xs-1 not-rotate"><div><span><?= text::$string['location_name']          ;?></span></div>  </th>
                    <th class="col-xs-3 not-rotate"><div><span><?= text::$string['class_date']           ;?></span></div>  </th>

                    <th class="col-xs-2 not-rotate"><div><span><?= text::$string['triallesson_type']     ;?></span></div>  </th>
                    <th class="col-xs-2 not-rotate"><div><span><?= text::$string['triallesson_email']     ;?></span></div>  </th>
                    <th class="col-xs-2 not-rotate"><div><span>via</span></div>  </th>

                    <th class="rotate-45"><div><span><?= text::$string['triallesson_called']    ;?></span></div>  </th>
                    <th class="rotate-45"><div><span><?= text::$string['triallesson_confirmd']  ;?></span></div>  </th>
                    <th class="rotate-45"><div><span><?= text::$string['triallesson_show']      ;?></span></div>  </th>
                    <th class="rotate-45"><div><span><?= text::$string['triallesson_sign_up']   ;?></span></div>  </th>
                    <th class="rotate-45"><div><span><?= text::$string['triallesson_cancelled'] ;?></span></div>  </th>
                    <th class="rotate-45" title="<?= Text::$string['triallesson_sms_info'];?>"><div><span><?= Text::$string['triallesson_sms']       ;?></span></div>  </th>
                    <th></th>
                    <?= $content[1]; ?>
                </table>
                    <input type="hidden" name="date1" value="<?=$con->date1?>">
                <input type="hidden" name="date2" value="<?=$con->date2?>"></form>
        </div>
    </div>

上次我没有添加代码抱歉。 这是我的数据显示的表格。 我的目的是当我单击同一页面上的按钮时,它将从表中获取所有数据并将其导出到XLS或CSV文件中。

如果需要任何进一步的信息,我会提前添加它。

1 个答案:

答案 0 :(得分:0)

您正在使用PHP创建表格,但使用JavaScript从生成的HTML表格创建Excel文件可能更简单,因为有多种工具可用于执行此操作并不太难以使用,例如:https://github.com/clarketm/TableExport

e.g。以此为例:您的代码为https://tableexport.v3.travismclarke.com/examples/position.html,您的代码可能如下所示:

<div class="left col-md-12 form-group" >
    <form method="post" id="triallesson_form">
        <div class="table">
            <table id="myDataTable" class="table table-header-rotated table-condensed table-striped table-hover" style="width: 100%;">
                <thead>
                    <th></th>
                    <th class="col-xs-1 not-rotate"><div><span><?= text::$string['person_name']            ;?></span></div>  </th>
                    <th class="col-xs-1 not-rotate"><div><span><?= text::$string['person_telphone']        ;?></span></div>  </th>
                    <th class="col-xs-1 not-rotate"><div><span><?= text::$string['location_name']          ;?></span></div>  </th>
                    <th class="col-xs-3 not-rotate"><div><span><?= text::$string['class_date']           ;?></span></div>  </th>
                    <th class="col-xs-2 not-rotate"><div><span><?= text::$string['triallesson_type']     ;?></span></div>  </th>
                    <th class="col-xs-2 not-rotate"><div><span><?= text::$string['triallesson_email']     ;?></span></div>  </th>
                    <th class="col-xs-2 not-rotate"><div><span>via</span></div>  </th>
                    <th class="rotate-45"><div><span><?= text::$string['triallesson_called']    ;?></span></div>  </th>
                    <th class="rotate-45"><div><span><?= text::$string['triallesson_confirmd']  ;?></span></div>  </th>
                    <th class="rotate-45"><div><span><?= text::$string['triallesson_show']      ;?></span></div>  </th>
                    <th class="rotate-45"><div><span><?= text::$string['triallesson_sign_up']   ;?></span></div>  </th>
                    <th class="rotate-45"><div><span><?= text::$string['triallesson_cancelled'] ;?></span></div>  </th>
                    <th class="rotate-45" title="<?= Text::$string['triallesson_sms_info'];?>"><div><span><?= Text::$string['triallesson_sms']       ;?></span></div>  </th>
                    <th></th>
                </thead>
                <tbody>
                    <?= $content[1]; ?>
                </tbody>
            </table>
            <input type="hidden" name="date1" value="<?=$con->date1?>">
            <input type="hidden" name="date2" value="<?=$con->date2?>">
        </div>
    </form>
</div>

<script type="text/javascript" src="../bower_components/jquery/dist/jquery.min.js"></script>
<script type="text/javascript" src="../bower_components/js-xlsx/dist/xlsx.core.min.js"></script>
<script type="text/javascript" src="../bower_components/blobjs/Blob.min.js"></script>
<script type="text/javascript" src="../bower_components/file-saverjs/FileSaver.min.js"></script>
<script type="text/javascript" src="../dist/js/tableexport.min.js"></script>
<script>
    var PositionTable = document.getElementById('myDataTable');
    new TableExport(PositionTable, {
        position: 'top'
    });
</script>

对HTML进行的唯一必要更改是向表中添加ID属性:<table id="myDataTable" ...

然后在JavaScript中再次引用var PositionTable = document.getElementById('myDataTable');

您需要更新所包含文件的位置以适应您获取它们的位置(xlsx.core.min.js,Blob.min.js,FileSaver.min.js,tableexport。 min.js等)。如果不需要,删除jQuery include。

然后,您将看到Excel下载按钮显示在您的表格下方,就像示例中一样。