2017-01-28 138 views
0

我希望你能幫助我把它,我有這樣的代碼:創建XLS,並通過電子郵件

<?php 
session_start(); 
include('includes/connect.php'); 
header("Content-Type: application/vnd.ms-excel charset=utf-8"); 
header("Content-Disposition: attachment; filename=consultores.xls"); //File name extension was wrong 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: private",false); 
$query = $_SESSION['query']; 
$qry = $mysqli->query($query); 
echo $mysqli->error; 
?> 
<body style="border: 1px solid #ccc"> 
<?php 
echo '<table class="table table-striped">'; 
?> 
<tr> 
    <th> 
    Apellido 
    </th> 
    <th> 
    Nombre 
    </th> 
    <th> 
    E-Mail 
    </th> 
    <th> 
    Teléfono Celular 
    </th> 
    <th> 
    CV 
    </th> 
    <th> 
    Foto 
    </th> 
    <?php 
    $query1 = "SELECT * FROM ind_exp ORDER BY nombre ASC"; 
    $qry1 = $mysqli->query($query1); 
    while($f1 = $qry1->fetch_assoc()) 
    { 
    echo '<th>'; 
    echo 'Experiencia en Industrias de: '.mb_convert_encoding($f1['nombre'],'utf-16','utf-8'); 
    echo '</th>'; 
    } 
    $query2 = "SELECT * FROM proy_exp ORDER BY nombre ASC"; 
    $qry2 = $mysqli->query($query2); 
    while($f2 = $qry2->fetch_assoc()) 
    { 
    echo '<th>'; 
    echo 'Experiencia en Proyectos de: '.mb_convert_encoding($f2['nombre'],'utf-16','utf-8'); 
    echo '</th>'; 
    } 
    $query3 = "SELECT * FROM rol_exp ORDER BY nombre ASC"; 
    $qry3 = $mysqli->query($query3); 
    while($f3 = $qry3->fetch_assoc()) 
    { 
    echo '<th>'; 
    echo 'Experiencia en Roles de: '.mb_convert_encoding($f3['nombre'],'utf-16','utf-8'); 
    echo '</th>'; 
    } 
    ?> 
</tr> 
<?php 
while($f = $qry->fetch_assoc()) 
{ 
    $mail = $f['email']; 
    foreach($f as $key=>$value) 
    { 
    $f[$key] = mb_convert_encoding($value,'utf-16','utf-8'); 
    } 
    ?> 
    <tr> 
    <td> 
     <?php 
     echo $f['apellido']; 
     ?> 
    </td> 
    <td> 
     <?php 
     echo $f['nombre']; 
     ?> 
    </td> 
    <td> 
     <?php 
     echo $f['email']; 
     ?> 
    </td> 
    <td> 
     <?php 
     echo $f['cel']; 
     ?> 
    </td> 
    <td> 
     <?php 
     echo 'http://litup.cl/registro/'.$f['cv']; 
     ?> 
    </td> 
    <td> 
     <?php 
     echo 'http://litup.cl/registro/'.$f['foto']; 
     ?> 
    </td> 
    <?php 
    $query1 = "SELECT * FROM ind_exp ORDER BY nombre ASC"; 
    $qry1 = $mysqli->query($query1); 
    while($f1 = $qry1->fetch_assoc()) 
    { 
     echo '<td>'; 
     $query3 = "SELECT * FROM rel WHERE id_reg = '$mail' AND tipo_rel = 'iec'"; 
     $qry3 = $mysqli->query($query3); 
     //echo $qry3->num_rows; 
     //echo $query3; 
     while($f3 = $qry3->fetch_assoc()) 
     { 
     //echo $f1['nombre'].' - '.$f3['nombre_ext']; 
     if($f1['nombre'] == $f3['nombre_ext']) 
     { 
      $aux = $f1['nombre']; 
      $cosa = mb_convert_encoding($aux,'utf-16','utf-8'); 
      echo ' '.$cosa.' '; 
     } 
     if($f3['otro']=='si' && $f1['nombre']== 'Otra Industria') 
     { 
      $aux = $f3['nombre_ext']; 
      $cosa = mb_convert_encoding($aux,'utf-16','utf-8'); 
      echo ' '.$cosa.' '; 
     } 
     } 
     //echo 'Industria de apoyo: '.$f1['nombre']; 
     echo '</td>'; 
    } 

    $query1 = "SELECT * FROM proy_exp ORDER BY nombre ASC"; 
    $qry1 = $mysqli->query($query1); 
    while($f1 = $qry1->fetch_assoc()) 
    { 
     echo '<td>'; 
     $query3 = "SELECT * FROM rel WHERE id_reg = '$mail' AND tipo_rel = 'pec'"; 
     $qry3 = $mysqli->query($query3); 
     //echo $qry3->num_rows; 
     //echo $query3; 
     while($f3 = $qry3->fetch_assoc()) 
     { 
     //echo $f1['nombre'].' - '.$f3['nombre_ext']; 
     if($f1['nombre'] == $f3['nombre_ext']) 
     { 
      $aux = $f1['nombre']; 
      $cosa = mb_convert_encoding($aux,'utf-16','utf-8'); 
      echo ' '.$cosa.' '; 
     } 
     if($f3['otro']=='si' && $f1['nombre']== 'Otro Proyecto') 
     { 
      $aux = $f3['nombre_ext']; 
      $cosa = mb_convert_encoding($aux,'utf-16','utf-8'); 
      echo ' '.$cosa.' '; 
     } 
     } 
     //echo 'Industria de apoyo: '.$f1['nombre']; 
     echo '</td>'; 
    } 


    $query1 = "SELECT * FROM rol_exp ORDER BY nombre ASC"; 
    $qry1 = $mysqli->query($query1); 
    while($f1 = $qry1->fetch_assoc()) 
    { 
     echo '<td>'; 
     $query3 = "SELECT * FROM rel WHERE id_reg = '$mail' AND tipo_rel = 'rec'"; 
     $qry3 = $mysqli->query($query3); 
     //echo $qry3->num_rows; 
     //echo $query3; 
     while($f3 = $qry3->fetch_assoc()) 
     { 
     //echo $f1['nombre'].' - '.$f3['nombre_ext']; 
     if($f1['nombre'] == $f3['nombre_ext']) 
     { 
      $aux = $f1['nombre']; 
      $cosa = mb_convert_encoding($aux,'utf-16','utf-8'); 
      echo ' '.$cosa.' '; 
     } 
     } 
     //echo 'Industria de apoyo: '.$f1['nombre']; 
     echo '</td>'; 
    } 
    ?> 
    </tr> 
    <?php 
} 
echo '</table>'; 
echo $mysqli->error; 
?> 
</body> 

有了這完全是出口到XLS,但現在我需要發送此XLS作爲附件一封電子郵件,但我有點失落。我希望你能幫助我。

如果您需要任何其他信息告訴我。

在此先感謝

+0

我可以建議,如果你想要一個'完美的XLS'你嘗試使用PHPExcel而不是導出一個HTML文件,因爲這是一個可怕的方式來建立一個可靠的XLS – RiggsFolly

+0

甚至一個簡單的CSV文件將更可靠 – RiggsFolly

回答

1

您可以使用EasyXLS library營造真實XLS或XLSX文件:
http://www.easyxls.com/manual/FAQ/export-to-excel-in-php-asp.html

要發送電子郵件使用PHPMailer

$email = new PHPMailer(); 
$email->From  = '[email protected]'; 
$email->FromName = 'Name'; 
$email->Subject = 'Message Subject'; 
$email->Body  = $messagebody; 
$email->AddAddress('[email protected]'); 

$file_to_attach = 'path to file'; 

$email->AddAttachment($file_to_attach , 'file.xls'); 

return $email->Send(); 

您還可以使用郵件()功能,但配置更難以發送附件: http://www.w3schools.com/php/func_mail_mail.asp

+0

我改變方法並使用它:'$ data =「所有HTML代碼」; file_put_contents('excel.xls',$ data);'用你的方法發送附件。謝謝您的幫助。如果有其他人需要這個,我按照這個鏈接:[導出到Excel保存在服務器](http://exporthtmltoexcel.blogspot.cl/2012/07/export-to-excel-save-it-in-server.html ) –