我正在尝试创建一个mysql查询循环,以在xlsxwriter php excel exporter中实现它,但我很难让它工作,它生成excel表,但它说它已损坏,无法工作。
这是原始文件源代码:
<?php
include_once("xlsxwriter.class.php");
ini_set('display_errors', 0);
ini_set('log_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);
$filename = "example.xlsx";
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$rows = array(
array('test','1','-50.5','2010-01-01 23:00:00','2012-12-31 23:00:00'),
array('2003','=B1', '23.5','2010-01-01 00:00:00','2012-12-31 00:00:00'),
);
$writer = new XLSXWriter();
$writer->setAuthor('Some Author');
foreach($rows as $row)
$writer->writeSheetRow('Sheet1', $row);
$writer->writeToStdOut();
//$writer->writeToFile('example.xlsx');
//echo $writer->writeToString();
exit(0);
这是我创建的代码,您会注意到在“writesheetrow”行中,我创建了一些伪数据,只是为了检查它是否工作,而不是
<?php
include "session.php";
include_once("files/excel/xlsxwriter.class.php");
ini_set('display_errors', 0);
ini_set('log_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);
$filename = "example.xlsx";
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$writer = new XLSXWriter();
//define column headers
$headers = array('Product Id' => 'integer', 'Price' => 'price', 'Sale Price' => 'price', 'Sales Count' => 'integer', 'Sale Date' => 'string');
//write headers
$writer->writeSheetHeader('Sheet1', $headers);
$sql = "SELECT * FROM customeracct WHERE tourid=1005 AND status=1";
$result = $db->query($sql);
if ($result->num_rows > 0) { while($customeracct = $result->fetch_assoc()) {
$contactid = $customeracct["contactid"];
$contactsql = "SELECT * FROM contacts WHERE id=$contactid";
$contactresult = $db->query($contactsql);
$contactcustomer = $contactresult->fetch_assoc();
//write rows to sheet1
$writer->writeSheetRow('Sheet1',array('test','1','-50.5','2010-01-01 23:00:00','2012-12-31 23:00:00'));
}}
$writer->setAuthor('Good Shepherd Travels');
exit(0);
你能帮我检查一下它是否正确,或者有没有更好的方法吗?我尝试在数组中创建循环,但是在数组中使用php条件代码是行不通的。
谢谢您。
2条答案
按热度按时间zaqlnxep1#
此链接可能有用。有很多方法可以做到这一点。只需搜索谷歌。
https://www.sitepoint.com/generate-excel-files-charts-phpexcel/
vwoqyblh2#
在使用xlsxwriter尝试了几种方法之后,通过尝试调整这个问题的答案:如何使用phpxlsxwriter从mysql导出xlsx
我找到了解决问题的方法,这是针对像我这样面临类似问题的人的代码,希望它能让他们受益: