创建XLS并通过电子邮件发送

时间:2017-01-28 17:24:34

标签: php excel email

我希望你能帮助我,我有这个代码:

<?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作为附件发送到电子邮件中,但我对此有点失落。我希望你能帮助我。

如果您需要任何其他信息,请告诉我。

提前致谢

1 个答案:

答案 0 :(得分:1)

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

使用PHPMailer发送电子邮件:

$email = new PHPMailer();
$email->From      = 'frommail@domain.com';
$email->FromName  = 'Name';
$email->Subject   = 'Message Subject';
$email->Body      = $messagebody;
$email->AddAddress( 'tomail@domain2.com' );

$file_to_attach = 'path to file';

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

return $email->Send();

您也可以使用mail()函数,但配置发送附件更加困难: http://www.w3schools.com/php/func_mail_mail.asp