如何通过电子邮件发送oracle查询结果。我更喜欢作为excel文件附加到电子邮件。

时间:2015-03-27 11:40:29

标签: php sql excel oracle email

我需要使用PhpExcel和PhpMailer通过电子邮件将此表作为excel附件发送。该表是查询我的oracle DB的结果。我需要知道如何使用PhpExcel将此表写入excel,然后使用phpMailer将其附加到电子邮件地址并最终发送。 提前谢谢。

<?php

$username = "xxx";
$passwd = "yyy";
$db="(DESCRIPTION =
   (ADDRESS_LIST =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 123.43.56.789)(PORT = 1234))

   )
   (CONNECT_DATA =

     (SERVICE_NAME = xxx)


   )
  )";

$c = OCILogon($username,$passwd,$db);
if (!$c)
{
    echo "Connection failed";

}
else
{
    echo "We are connected to KYC.Querying DB now...Please wait... ";
}

$s = oci_parse($c, "
select MSISDN SUBSCRIBER_NUMBER, OUTLET_NUMBER,time_stamp, Company,NAMES SUBSCRIBER_NAME,GENDER, DOB DATE_OF_BIRTH,Dealer_name Dealer_name,  Agent_name 
from subscribers  where time_stamp between trunc(sysdate)-1 And trunc(sysdate)-1/86400 order by time_stamp");

if (!$s) {
$e = oci_error($c);
trigger_error('Could not parse statement: '. $e['message'], E_USER_ERROR);
}
$r = oci_execute($s);
if (!$r) {
$e = oci_error($s);
trigger_error('Could not execute statement: '. $e['message'], E_USER_ERROR);
    }


$html .= '<table width="720" border="0" cellspacing="0" cellpadding="0">
 <tr>
 <th class="header" colspan="7"> KYC Details</th>
 <tr>

 <tr>
  <th>SUBSCRIBER_NUMBER</th>
  <th>OUTLET_NUMBER</th>
  <th>TIME_STAMP</th>
  <th>COMPANY</th>
  <th>SUBSCRIBER_NAME</th>
  <th>GENDER</th>
  <th>DATE_OF_BIRTH</th>
<tr>';


while($rows=oci_fetch_array($s, OCI_ASSOC+OCI_RETURN_NULLS)){
        $sub = $rows['SUBSCRIBER_NUMBER'];
        $agt = $rows['OUTLET_NUMBER'];
        $tim = $rows['TIME_STAMP'];
        $com = $rows['COMPANY'];
        $nam = $rows['SUBSCRIBER_NAME'];
        $sex = $rows['GENDER'];
        $dob = $rows['DATE_OF_BIRTH'];

$html .= '<tr>
        <td>'.$sub.'</td>
        <td>'.$agt.'</td>
        <td>'.$tim.'</td>
        <td>'.$com.'</td>
        <td>'.$nam.'</td>
        <td>'.$sex.'</td>
        <td>'.$dob.'</td>';
$html .= '<tr>';

    }
$html .='</table><hr>';

echo $html;

?>

1 个答案:

答案 0 :(得分:1)

我会忽略HTML的内容,只需将其写为逗号分隔值文件,并在每条记录后添加换行符,然后在Excel中将其打开并保存为XLSX。