What's new

PHP import table data to excel

J P

Journeyman
Joined
Jun 1, 2023
Posts
32
Reaction
0
Points
21
I just need a button for extract data swhown in the table.
Thank you for your help.



PHP:
<?php
    session_start();
     $connection= mysqli_connect('localhost','root','','vehicle management');

    $select_query="SELECT * FROM `booking`";
    $result= mysqli_query($connection,$select_query);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>trip Details</title>
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">   
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <link rel="stylesheet" href="http://cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css">
    <link rel="stylesheet" href="sweetalert2/sweetalert2.css">
    <script src="sweetalert2/sweetalert2.min.js"></script>
    <script type="text/javascript" src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <!-- <link rel="stylesheet" href="animate.css"> -->
    <link rel="stylesheet" href="style.css">

</head>
<body>
    <?php include 'navbar_admin.php'; ?>
    <br><br>
    <div class="container">
        <div class="row">
            
            <div class="col-md-12">
                <div class="page-header">
                    <h1 style="text-align: center;">Trip Details</h1>
                
                </div>
                
                <table id="myTable" class="table table-bordered animated rubberBand">
                    <thead>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Department</th>
                        <th>Vehicle</th>
                        <th>Plate Number</th>
                        <th>Driver ID</th>
                        <th>Destination</th>
                        <th>Trip Date</th>
                        <th>Time</th>
                    </thead>
                    <tbody>
                        <?php while($row=mysqli_fetch_assoc($result)){ ?>
                        <tr>
                            <td><?php echo $row['booking_id']; ?></td>
                            <td><?php echo $row['name']; ?></td>
                            <td><?php echo $row['department']; ?></td>
                            <td><?php echo $row['type']; ?></td>
                            <td><?php echo $row['veh_reg']; ?></td>
                            <td><?php echo $row['driverid']; ?></td>
                            <td><?php echo $row['destination']; ?></td>
                            <td><?php echo $row['req_date']; ?></td>
                            <td><?php echo $row['req_time']; ?></td>
                        </tr>
                        <?php } ?>
                    </tbody>
                </table>
                
                
            </div>
    
        </div>
    </div>
</body>
<script>
$(document).ready(function(){
    $('#myTable').dataTable();
});
</script>
</html>
1702350362319.png
 

Attachments

To add a button to extract the data shown in the table, you can make use of the PHPExcel library. Here's an example of how you can modify your code to achieve that:

First, you need to download and include the PHPExcel library in your project. You can download it from the official GitHub repository: You do not have permission to view the full content of this post. Log in or register now.

After downloading and extracting the PHPExcel library, you can include the necessary files in your code:

PHP:
<?php
    session_start();
    require_once 'PHPExcel/PHPExcel.php';
    $connection = mysqli_connect('localhost', 'root', '', 'vehicle management');

    $select_query = "SELECT * FROM `booking`";
    $result = mysqli_query($connection, $select_query);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <!-- ... Your existing code ... -->
</head>
<body>
    <!-- ... Your existing code ... -->

    <button id="exportButton" class="btn btn-primary">Export Data</button>

    <!-- ... Your existing code ... -->

    <script>
        $(document).ready(function(){
            $('#myTable').dataTable();

            // Add click event listener to the export button
            $('#exportButton').click(function(){
                // Create a new PHPExcel object
                $objPHPExcel = new PHPExcel();

                // Set default properties for the workbook
                $objPHPExcel->getProperties()
                    ->setCreator('Your Name')
                    ->setLastModifiedBy('Your Name')
                    ->setTitle('Trip Details')
                    ->setSubject('Trip Details')
                    ->setDescription('Trip Details');

                // Add data to the worksheet
                $objPHPExcel->setActiveSheetIndex(0);
                $objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID');
                $objPHPExcel->getActiveSheet()->setCellValue('B1', 'Name');
                $objPHPExcel->getActiveSheet()->setCellValue('C1', 'Department');
                $objPHPExcel->getActiveSheet()->setCellValue('D1', 'Vehicle');
                $objPHPExcel->getActiveSheet()->setCellValue('E1', 'Plate Number');
                $objPHPExcel->getActiveSheet()->setCellValue('F1', 'Driver ID');
                $objPHPExcel->getActiveSheet()->setCellValue('G1', 'Destination');
                $objPHPExcel->getActiveSheet()->setCellValue('H1', 'Trip Date');
                $objPHPExcel->getActiveSheet()->setCellValue('I1', 'Time');

                <?php
                $rowNumber = 2;
                while($row = mysqli_fetch_assoc($result)){
                    echo '$objPHPExcel->getActiveSheet()->setCellValue(\'A' . $rowNumber . '\', \'' . $row['booking_id'] . '\');' . PHP_EOL;
                    echo '$objPHPExcel->getActiveSheet()->setCellValue(\'B' . $rowNumber . '\', \'' . $row['name'] . '\');' . PHP_EOL;
                    echo '$objPHPExcel->getActiveSheet()->setCellValue(\'C' . $rowNumber . '\', \'' . $row['department'] . '\');' . PHP_EOL;
                    echo '$objPHPExcel->getActiveSheet()->setCellValue(\'D' . $rowNumber . '\', \'' . $row['type'] . '\');' . PHP_EOL;
                    echo '$objPHPExcel->getActiveSheet()->setCellValue(\'E' . $rowNumber . '\', \'' . $row['veh_reg'] . '\');' . PHP_EOL;
                    echo '$objPHPExcel->getActiveSheet()->setCellValue(\'F' . $rowNumber . '\', \'' . $row['driverid'] . '\');' . PHP_EOL;
                    echo '$objPHPExcel->getActiveSheet()->setCellValue(\'G' . $rowNumber . '\', \'' . $row['destination'] . '\');' . PHP_EOL;
                    echo '$objPHPExcel->getActiveSheet()->setCellValue(\'H' . $rowNumber . '\', \'' . $row['req_date'] . '\');' . PHP_EOL;
                    echo '$objPHPExcel->getActiveSheet()->setCellValue(\'I' . $rowNumber . '\', \'' . $row['req_time'] . '\');' . PHP_EOL;

                    $rowNumber++;
                }
                ?>

                // Set the filename for the download
                $filename = 'trip_details.xlsx';

                // Prepare the response headers
                header('Content-Type: application/vnd.ms-excel');
                header('Content-Disposition: attachment;filename="' . $filename . '"');
                header('Cache-Control: max-age=0');

                // Create a writer to output the workbook to the browser
                $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
                $objWriter->save('php://output');
            });
        });
    </script>
</body>
</html>

In this modified code, a button with the ID "exportButton" is added to the page. When the button is clicked, a new PHPExcel object is created and populated with the data from the table. The data is then saved as an Excel file and sent to the browser for download.

Please make sure to adjust the paths to the PHPExcel library files according to your project structure.
 
You do not have permission to view the full content of this post. Log in or register now.


<link rel="stylesheet" type="text/css" href="DataTables-1.10.24/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="TableTools-2.2.4/css/dataTables.tableTools.min.css">

<script src="jQuery-3.5.1/jquery-3.5.1.min.js"></script>
<script src="DataTables-1.10.24/js/jquery.dataTables.min.js"></script>
<script src="TableTools-2.2.4/js/dataTables.tableTools.min.js"></script>


change mo yung nasa script mo ng declaration ng datatable nito

$(document).ready(function() {
var table = $('#myTable').DataTable( {
dom: 'Bfrtip',
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
]
} );
} );

auutomatic meron yang mga button to export
 
You do not have permission to view the full content of this post. Log in or register now.


<link rel="stylesheet" type="text/css" href="DataTables-1.10.24/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="TableTools-2.2.4/css/dataTables.tableTools.min.css">

<script src="jQuery-3.5.1/jquery-3.5.1.min.js"></script>
<script src="DataTables-1.10.24/js/jquery.dataTables.min.js"></script>
<script src="TableTools-2.2.4/js/dataTables.tableTools.min.js"></script>


change mo yung nasa script mo ng declaration ng datatable nito

$(document).ready(function() {
var table = $('#myTable').DataTable( {
dom: 'Bfrtip',
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
]
} );
} );

auutomatic meron yang mga button to export
Thank you!

Ano gagawin ko dito?
<link rel="stylesheet" type="text/css" href="DataTables-1.10.24/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="TableTools-2.2.4/css/dataTables.tableTools.min.css">

<script src="jQuery-3.5.1/jquery-3.5.1.min.js"></script>
<script src="DataTables-1.10.24/js/jquery.dataTables.min.js"></script>
<script src="TableTools-2.2.4/js/dataTables.tableTools.min.js"></script>
 
Thank you!

Ano gagawin ko dito?
<link rel="stylesheet" type="text/css" href="DataTables-1.10.24/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="TableTools-2.2.4/css/dataTables.tableTools.min.css">

<script src="jQuery-3.5.1/jquery-3.5.1.min.js"></script>
<script src="DataTables-1.10.24/js/jquery.dataTables.min.js"></script>
<script src="TableTools-2.2.4/js/dataTables.tableTools.min.js"></script>
importing ng datatable na nadownload mo. pero kung meron kana. wag mo nang pansinin
 
importing ng datatable na nadownload mo. pero kung meron kana. wag mo nang pansinin
Napalitan ko na po script pero wala pa din po
Na dowload ko na din po ito
1702358736694.png



PHP:
<?php
    session_start();
     $connection= mysqli_connect('localhost','root','','vehicle management');

    $select_query="SELECT * FROM `booking`";
    $result= mysqli_query($connection,$select_query);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>trip Details</title>
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">   
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <link rel="stylesheet" href="http://cdn.datatables.net/1.10.2/css/jquery.dataTables.min.css">
    <link rel="stylesheet" href="sweetalert2/sweetalert2.css">
    <script src="sweetalert2/sweetalert2.min.js"></script>
    <script type="text/javascript" src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <!-- <link rel="stylesheet" href="animate.css"> -->
    <link rel="stylesheet" href="style.css">

</head>
<body>
    <?php include 'navbar_admin.php'; ?>
    <br><br>
    <div class="container">
        <div class="row">
            
            <div class="col-md-12">
                <div class="page-header">
                    <h1 style="text-align: center;">Trip Details</h1>
                
                </div>
                
                <table id="myTable" class="table table-bordered animated rubberBand">
                    <thead>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Department</th>
                        <th>Vehicle</th>
                        <th>Plate Number</th>
                        <th>Driver ID</th>
                        <th>Destination</th>
                        <th>Trip Date</th>
                        <th>Time</th>
                    </thead>
                    <tbody>
                        <?php while($row=mysqli_fetch_assoc($result)){ ?>
                        <tr>
                            <td><?php echo $row['booking_id']; ?></td>
                            <td><?php echo $row['name']; ?></td>
                            <td><?php echo $row['department']; ?></td>
                            <td><?php echo $row['type']; ?></td>
                            <td><?php echo $row['veh_reg']; ?></td>
                            <td><?php echo $row['driverid']; ?></td>
                            <td><?php echo $row['destination']; ?></td>
                            <td><?php echo $row['req_date']; ?></td>
                            <td><?php echo $row['req_time']; ?></td>
                        </tr>
                        <?php } ?>
                    </tbody>
                </table>
                
                
            </div>
    
        </div>
    </div>
</body>
<script>
$(document).ready(function() {
var table = $('#myTable').DataTable( {
dom: 'Bfrtip',
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
]
} );
} );
</script>
</html>
 

Attachments

make sure tamaa yung path sa pag import mo paps

<head>
<link rel="stylesheet" href="You do not have permission to view the full content of this post. Log in or register now.">
<link rel="stylesheet" href="sweetalert2/sweetalert2.css">
<script src="sweetalert2/sweetalert2.min.js"></script>
<script type="text/javascript" src="You do not have permission to view the full content of this post. Log in or register now."></script>
<script type="text/javascript" src="You do not have permission to view the full content of this post. Log in or register now."></script>
<!-- <link rel="stylesheet" href="animate.css"> -->
<link rel="stylesheet" href="style.css">

</head>

yung ginamit mo kase paps ONLINE e. much better yung locally nalang. kase kung wlang internet hindi yan gagana
 

Similar threads

Back
Top