-1

在 php 中导出 frm mysql 表时,我需要在 excel spreedsheet 中使列名加粗。请检查下面给出的代码:

<?php

  include '../Scripts/connect.php';    

$sql = "Select u.firstname, u.lastname, q.module, q.title, q.description, a.option, a.correctAnswer, a.userAnswer, a.isCorrect FROM table1 a INNER JOIN table2 u on u.userID = a.user INNER JOIN table3 q on q.questionPageID = a.questionPageID";

    $result = @mysql_query($sql)    or die("Couldn't execute query:
".mysql_error().'
'.mysql_errno());

    header('Content-Type: application/vnd.ms-excel');   //define header info for browser
    header('Content-Disposition: attachment; filename='.$dbTable.'Answers'.date('Ymd').'.xls');
    header('Pragma: no-cache');
    header('Expires: 0');

    for ($i = 0; $i < mysql_num_fields($result); $i++)   // show column names as names of MySQL fields
        echo mysql_field_name($result, $i)."\t";
    print("\n");

    while($row = mysql_fetch_row($result))
    {
        //set_time_limit(60); // you can enable this if you have lot of data
        $output = '';
        for($j=0; $j < mysql_num_fields($result); $j++)
        {
            if(!isset($row[$j]))
                $output .= "NULL\t";
            else
                $output .= "$row[$j]\t";
        }
        $output = preg_replace("/\r\n|\n\r|\n|\r/", ' ', $output);
        print(trim($output))."\t\n";
    }
?>

现在它只是以正常格式导出数据。

4

1 回答 1

1

在这种情况下,您需要创建一个实际的 Excel BIFF (.xls) 或 OfficeOpenXML (.xlsx) 文件,使用为此目的编写的众多 PHP 库之一(例如PHPExcel),而不是制表符分隔值文件

于 2013-06-24T01:01:14.893 回答