Ecommerce Shopify WordPress Discussion

Why my exported excel file in wordpress has html and css in?

I am trying to export a table from my database to and Excel file with .CSV extension when the user press export button in WordPress. I am able to export the table but when I export, the table also has all the HTML, CSS and JavaScript tags. Can someone please check my code and show me how I can export the file without the html and CSS? I put my code in a short code and then added into the page. [[Screenshot of my exported Excel file] (https://i.stack.imgur.com/QjOhQ.png)] Thanks Here is my code session_start(); $con = new mysqli("","","",""); require '/home/public_html/wp-content/plugins/cbxphpspreadsheet/lib/vendor/autoload.php'; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; use PhpOffice\PhpSpreadsheet\Writer\Xls; use PhpOffice\PhpSpreadsheet\Writer\Csv; if(isset($_POST['export_excel_btn'])) { $file_ext_name = $_POST['export_file_type']; $fileName = "student-sheet"; $student = "SELECT * FROM students"; $query_run = mysqli_query($con, $student); if(mysqli_num_rows($query_run) > 0) { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1', 'ID'); $sheet->setCellValue('B1', 'Full Name'); $sheet->setCellValue('C1', 'Email'); $sheet->setCellValue('D1', 'Phone'); $sheet->setCellValue('E1', 'Course'); $rowCount = 2; foreach($query_run as $data) { $sheet->setCellValue('A'.$rowCount, $data['id']); $sheet->setCellValue('B'.$rowCount, $data['fullname']); $sheet->setCellValue('C'.$rowCount, $data['email']); $sheet->setCellValue('D'.$rowCount, $data['phone']); $sheet->setCellValue('E'.$rowCount, $data['course']); $rowCount++; } if($file_ext_name == 'xlsx') { $writer = new Xlsx($spreadsheet); $final_filename = $fileName.'.xlsx'; } elseif($file_ext_name == 'xls') { $writer = new Xls($spreadsheet); $final_filename = $fileName.'.xls'; } elseif($file_ext_name == 'csv') { $writer = new Csv($spreadsheet); $final_filename = $fileName.'.csv'; } $writer->save($final_filename); header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: inline; filename="'.urlencode($final_filename).'"'); $writer->save('php://output'); } else { $_SESSION['message'] = "No Record Found"; header('Location: index.php'); exit(0); }} session_destroy(); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <div class="row"> <div class="col-md-12 mt-4"> <?php if(isset($_SESSION['message'])) { echo "<h4>".$_SESSION['message']."</h4>"; unset($_SESSION['message']); } ?> <div class="card mt-5"> <div class="card-header"> <h4>Export Clients Data into an Excel File</h4> </div> <div class="card-body"> <form action="" method="POST"> <select name="export_file_type" class="form-control"> <option value="xlsx">XLSX</option> <option value="xls">XLS</option> <option value="csv">CSV</option> </select> <button type="submit" name="export_excel_btn" class="btn btn-primary mt-3">Export</button> </form> </div> </div> </div> </div> </div> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script> </body> </html> <?php session_write_close(); I tried to export a table from database into an excel file in WordPress using short Code. I am able to export the file but when i open the file, the file also has the html, css and JavaScript from the page in it.
Terminiate your php script after the second save() by calling exit(0);. And make sure no output is send before you send the headers do for the generated file. You can try an ob_end_clean(); before sending the headers to php://output. Also see: https://github.com/PHPOffice/PhpSpreadsheet/issues/217 for people having the same issues (although it'snot really a problem of the package).

January 10, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.