如何在xlsxwriter php中使用mysql loop来导出到PHP中的Excel工作表

时间:2018-10-22 09:51:38

标签: php mysql excel xlsxwriter

我正在尝试创建一个mysql查询循环以在xlsxwriter php excel导出器中实现它,但是我很难使它工作,它会生成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 个答案:

答案 0 :(得分:1)

尝试使用xlsxwriter的几种方法并尝试调整此问题的答案:how to export xlsx from mysql with php xlsxwriter

我能够找到解决问题的方法,这是面向像我这样面临类似问题的人们的代码,希望它会对他们有所帮助:

<?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');   
    $query="SELECT * FROM customeracct WHERE tourid=1005 AND status=1";
    $result = $db->query($query); 
    //$rows = $result->fetch_assoc(); 
    $header = array(
      'ID'=>'integer',
      'Subject'=>'string',
      'Content'=>'string',
    );
    $writer = new XLSXWriter();
    $writer->writeSheetHeader('Sheet1', $header);
    $array = array();
    while ($row=$result->fetch_assoc())
    {
        $array[1] = $row['contactid'];
        $array[2] = $row['tourid'];
        $array[3] = $row['status'];
        $writer->writeSheetRow('Sheet1', $array);
    };

    //$writer->writeSheet($array,'Sheet1', $header);//or write the whole sheet in 1 call    

    $writer->writeToStdOut();
    //$writer->writeToFile('example.xlsx');
    //echo $writer->writeToString();
    exit(0);

答案 1 :(得分:0)

此链接可能有用。有很多方法可以做到这一点。只需搜索Google。

https://www.sitepoint.com/generate-excel-files-charts-phpexcel/