将表导出为ex​​cel / csv / pdf

时间:2014-07-03 09:01:51

标签: php html export-to-excel export-to-csv

我有一个html页面,它显示了一个使用PHP从数据库中获取的表。我想在页面上创建一个链接,以便在csv / pdf / excel中导出表格。这有可能创造,如果是,请帮助我。

2 个答案:

答案 0 :(得分:1)

您可以使用 PHPExcel https://phpexcel.codeplex.com/

require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', 'Hello')
        ->setCellValue('B2', 'world!')
        ->setCellValue('C1', 'Hello')
        ->setCellValue('D2', 'world!');
$objPHPExcel->setActiveSheetIndex(0);

// Save Excel
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', __FILE__));

echo date('H:i:s') , " File written to " , str_replace('.php', '.xls', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;

答案 1 :(得分:0)

    <script src="https://code.jquery.com/jquery-1.12.3.js"></script>
    <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.2.2/js/dataTables.buttons.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.2.2/js/buttons.flash.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js"></script>
    <script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js"></script>
    <script src="https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.2.2/js/buttons.html5.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.2.2/js/buttons.print.min.js"></script>
    <link href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" type="text/css" rel="stylesheet"></script>
    <link href="https://cdn.datatables.net/buttons/1.2.2/css/buttons.dataTables.min.css" type="text/css" rel="stylesheet"></script>
    <script>
    $(document).ready(function() {
        $('#tableid').DataTable( {
            dom: 'Bfrtip',
            buttons: [
                'copy', 'csv', 'excel', 'pdf', 'print'
            ]
        } );
    } );
    </script>


<script>

$(document).ready(function() {
    $('#tableid').DataTable( {
        dom: 'Bfrtip',
        buttons: [
             {
                extend: 'excelHtml5',
                exportOptions: {
                     columns: [ 0,1,2,3,4,5 ]
                }
            }
        ]
    } );
} );
</script>