Exporting data from database in php,and the file format for excel export should be as windows 97-2003 workbook

こ雲淡風輕ζ 提交于 2019-12-07 01:31:26

You can just tell your MySQL database to dump it as CSV:

$file = 'D:/path/to/website/dump.sql';

// The SELECT...INTO query is unable to overwrite if the
// file already exists, so delete it beforehand:
if (is_file($file))
    unlink($file);

$query = "
    SELECT id, title, created   -- The fields to export
    INTO OUTFILE '{$file}'      -- The file
    FIELDS TERMINATED BY ','    -- Delimiter
    OPTIONALLY ENCLOSED BY '\"' -- Quote if necessary
    LINES TERMINATED BY '\n'    -- End line with LF newline
    FROM tbl_employee           -- Table name
";

$db = new MySQLi('localhost', 'root', '', 'your_database');
$db->query($query);

// Show any errors...
if ($db->error) {
    var_dump($db->error);
}

Using EasyXLS Excel library you can save real xls files from a mysql query, not csv files:

// Query the database
$query_result = mysql_query( "SELECT * FROM table", $db_conn ) or die( "<strong>ERROR: Query failed</strong>" );

// Create the list used to store the values
$lstRows = new COM("EasyXLS.Util.List");

// Add the header row to the list
$lstHeaderRow = new COM("EasyXLS.Util.List");
$lstHeaderRow->addElement("Column 1");
$lstHeaderRow->addElement("Column 2");
$lstHeaderRow->addElement("Column 2");
$lstRows->addElement($lstHeaderRow);

// Add the values from the database to the list
while ($row=mssql_fetch_array($query_result))
{
     $RowList = new COM("EasyXLS.Util.List");
     $RowList->addElement("" . $row["Column 1"]);
     $RowList->addElement("" . $row["Column 2"]);
     $RowList->addElement("" . $row["Column 3"]);
     $lstRows->addElement($RowList);
}

// Create an instance of the object used to format the cells
$xlsAutoFormat = new COM("EasyXLS.ExcelAutoFormat");
$xlsAutoFormat->InitAs($AUTOFORMAT_EASYXLS1);

// Export list to Excel file
$xls->easy_WriteXLSFile_FromList_2("Query to Excel.xls", $lstRows, $xlsAutoFormat, "MySQL Query values");

See here more details about exporting data from database:

http://www.easyxls.com/manual/basics/export-list-to-excel.html

and here about exporting data to Excel 97-2003 xls workbook:

http://www.easyxls.com/manual/basics/export-to-xls-file-format.html

1-Use phpExcel library. It has several nice features for generating excel/csv in native format using php.

2- Use this code to download either excel or csv based on which submit button was clicked

 <input type="submit" name="Download"  value="CSV" title="Export" class="frmDownloadButton" /> 
<input type="submit" name="Download"  value="EXECL" title="Export" class="frmDownloadButton" /> 


    <?php

    if($_POST["download"]){ 
            $output="";
            $line_termineted="\n";

            if( $_POST["download"] =="CSV") $field_termineted=","; else $field_termineted="\t"; 
                $enclosed='"';
                $escaped="\\";

                $export_schema="SR No".$field_termineted."Student ID".$field_termineted."First Name".$field_termineted."Middle Name".$field_termineted."Last Name";
                $dataQuery=mysql_query("select * from sof_student ");
                $output.=$export_schema;
                $p=0;
                while($data=mysql_fetch_array($dataQuery)) {
                 $p++;
                    $output.= $line_termineted.$p.$field_termineted;
                    $output.=$enclosed.$data["id"].$enclosed.$field_termineted;
                    $output.=$enclosed.$data["first_name"].$enclosed.$field_termineted;
                    $output.=$enclosed.$data["middle_name"].$enclosed.$field_termineted;
                    $output.=$enclosed.$data["last_name"].$enclosed.$field_termineted;
                  }

        header("Content-Description: File Transfer");
       if( $_POST["download"] =="CSV"){
            header("Content-Type: application/csv");
            header("Content-Disposition: attachment; filename=report".date("d_m_Y_H_i_s").".csv");
        } else {
            header("Content-Type: application/vnd.ms-excel");
            header("Content-disposition: attachment; filename=report".date("d_m_Y_H_i_s").".xls");
        }

        header("Content-Transfer-Encoding: binary");
        header("Expires: 0");
        header("Cache-Control: must-revalidate");
        header("Pragma: public");
        header("Content-Length: ".strlen($output));
        ob_clean();
        flush();
        echo $output;
        exit;
    }
     ?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!