使用php生成Excel报告

时间:2014-04-11 04:53:34

标签: php sql excel

我希望使用PHP下载Excel工作表。

在Excel报告中,生成的数字无法在单元格中正确显示。

我想在单元格中正确显示数字。

我的代码:

 <?php

    include("connection.php");

    if(isset($_POST['submit']) && $_POST['submit']=="Download")
        {

        $payor_code = $_POST['payor_code'];
        $corp_code= $_POST['corp_code'];
        $pro_type = isset($_POST['product_type']);
        $submit_date = $_POST['sub_date'];

     $sql= oci_parse($conn, "select * from members");

    oci_execute($sql);
    $dat = date("d/m/Y"); 
    //echo $dat;
        $filename = "Report_$dat";

           $table = "";
            $table .= '<table border="1" cellpadding="2" cellspacing="0" width="900">';
            $table .= '<tr>';
            $table .= '<td colspan="20" style="height:30px;font-weight:bold; font-size:20px" align="center">' .$filename. '</td>';
            $table .= '</tr>';
            $table .= '<tr>'; 
            $table .= '<td style="height:20px;font-weight:bold;width=100px" align="center" valign="middle">ReceivedDate</td>'; 
            $table .= '<td style="height:20px;font-weight:bold;width=50px" align="center" valign="middle">ProductType</td>';
            $table .= '<td style="height:20px;font-weight:bold;width=100px" align="center" valign="middle">Name</td>';
             $table .= '<td style="height:20px;font-weight:bold;width=100px" align="center" valign="middle">SubmissionDate</td>';
            $table .= '<td style="height:20px;font-weight:bold;width=100px" align="center" valign="middle">ClaimNo</td>';
             $table .= '<td style="height:20px;font-weight:bold;width=100px" align="center" valign="middle">PatientName</td>';

              $table .= '<td style="height:20px;font-weight:bold;width=100px" align="center" valign="middle">PatientIC</td>';
               $table .= '<td style="height:20px;font-weight:bold;width=100px" align="center" valign="middle">PrincipalName</td>';
                $table .= '<td style="height:20px;font-weight:bold;width=100px" align="center" valign="middle">PrincipalIC</td>';

            $table .= '</tr>';

            $i = 1;

        while($row = oci_fetch_array($sql))
        {

        $cli_id = $row['CLAIMS_ID']. "<br>\n";
        $pat_name = $row['PATIENT_NAME']. "<br>\n";
        $sub_date = $row['SUBMISSION_DATE']. "<br>\n";
        $remarks= $row['REMARKS']. "<br>\n";
        $doc_rece_date= $row['RECEIVED_DATE']. "<br>\n";
        $group = "Group";       
            $test = "TEST";
            $board_type = "TEST";
            $board_no = "123";
            $prin_name = "Test";
            $table .= '<tr>';
            $table .= '<td  align="left">'.$doc_rece_date.'</td>';
            $table .= '<td  align="left">'.$group.'</td>';
            $table .= '<td  align="left">'.$test.'</td>';       
             $table .= '<td  align="left">'.$sub_date.'</td>';
             $table .= '<td  align="left">'.$cli_id.'</td>';
             $table .= '<td  align="left">'.$pat_name.'</td>';   
             $pat_ic = $row['NRIC_ID'];
             $table .= '<td  align="left">'.$pat_ic.'</td>'; 
             $pric_name = $row['EMP_NAME'];
             $table .= '<td  align="left">'.$pric_name.'</td>';
             $pric_ic = $row['PRINCIPAL_NRIC'];
             $table .= '<td  align="left">'.$pric_ic.'</td>';           
        }
        $table .= '</table>';

    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");;
    header("Content-Disposition: attachment; filename=$filename.xls");
    header("Content-Transfer-Encoding: binary ");

    echo $table;
    }
    ?>

2 个答案:

答案 0 :(得分:1)

您无需将table tags添加到Excel中。您可以使用fputcsv()。检查示例 -

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=export.csv");
header("Pragma: no-cache");
header("Expires: 0");

$header = array(
    "Sn.",
    "Name",
    "Email",
    "Mobile",
    "Address",
    "City",
    "State",
    "Pincode"
       );

$fp = fopen('php://output', 'w');
fputcsv($fp, $header);

$incr = 0;
while($row = mysql_fetch_assoc($execute1)) {

    $name = $row['name'];
    $email = $row['email'];
    $mobile = $row['mobile'];
    $address = $row['address'];
    $city = $row['city'];
    $state = $row['state'];
    $pincode = $row['pincode'];

        $fields = array (
        $incr++,
        $name,
        $email,
        $mobile,
        $address,
        $city,
        $state,
        $pincode
        );

       fputcsv($fp, $fields);
  }

答案 1 :(得分:0)

你应该看PHPExcel,它比你期望的要多得多。

检查PHPExcel here的功能。