Excel sheet creation & mail attachment

Discussion in 'PHP' started by mayuroak, Feb 6, 2008.

  1. #1
    We are creating excel sheet from tables. It is created successfully. Now we want to save that excel sheet as an attachment for mailing purpose. But whatever we are writing after generation of that excel sheet it takes the whole form inside that excel sheet. So can anybody tell us how to close that excel sheet so that we can continue with mailing function. All things(excel creation, save, close and mail the same excel as an attachment) should happen only on a single click.
    Thanx in advance!

    Code is as follows:

    for($j=0;$j<$i-1;$j++)
    {
    :) $val='"'.$res['initials'][$j].'"'."\t". '"'.$res['fname'][$j].'"'."\t". '"'.$res['mname'][$j].'"'."\t". '"'.$res['lname'][$j].'"'."\t". '"'.$res['birth_date'][$j].'"'."\t". '"'.$res['nationality'][$j].'"'."\t". '"'.$res['marrital_status'][$j].'"'."\t". '"'.$res['gender'][$j].'"'."\t". '"'.$res['hobbies'][$j].'"'."\t". '"'.$res['address'][$j].'"'."\t". '"'.$res['city'][$j].'"'."\t". '"'.$res['state'][$j].'"'."\t". '"'.$res['country'][$j].'"'."\t". '"'.$res['zip_code'][$j].'"'."\t". '"'.$res['resi_phone'][$j].'"'."\t". '"'.$res['off_phone'][$j].'"'."\t". '"'.$res['mobile_phone'][$j].'"'."\t". '"'.$res['personal_email'][$j].'"'."\t". '"'.$res['industry'][$j].'"'."\t". '"'.$res['career_level'][$j].'"'."\t". '"'.$res['qualification'][$j].'"'."\t". '"'.$res['institution_name'][$j].'"'."\t". '"'.$res['current_CTC'][$j].'"'."\t". '"'.$res['expected_CTC'][$j].'"'."\t". '"'.$res['security_clearence'][$j].'"'."\t". '"'.$res['security_type'][$j].'"'."\t". '"'.$res['current_desig'][$j].'"'."\t". '"'.$res['current_company'][$j].'"'."\t". '"'.$res['skills'][$j].'"'."\t". '"'.$res['job_title'][$j].'"'."\t". '"'.$res['company'][$j].'"';
    $line[$j] .= $val;
    $data .= trim($line[$j])."\n";
    }
    $data = str_replace("\r", "", $data);

    $count = mysql_num_fields($result);
    for ($i = 0; $i < $count; $i++)
    {
    $header .= mysql_field_name($result, $i)."\t"; //heading of excel file(field name)
    }//for ($i = 0; $i < $count; $i++)

    $cnt = mysql_num_fields($r);
    for ($i = 0; $i < $cnt; $i++)
    {
    $header .= mysql_field_name($r, $i)."\t"; //heading of excel file(field name)
    }//for ($i = 0; $i < $count; $i++)

    $cnt = mysql_num_fields($r1);
    for ($i = 0; $i < $cnt; $i++)
    {
    $header .= mysql_field_name($r1, $i)."\t"; //heading of excel file(field name)
    }//for ($i = 0; $i < $count; $i++)

    $cnt = mysql_num_fields($r2);
    for ($i = 0; $i < $cnt; $i++)
    {
    $header .= mysql_field_name($r2, $i)."\t"; //heading of excel file(field name)
    }//for ($i = 0; $i < $count; $i++)

    if ($data == "")
    {
    $data = "\nNo matching records found\n";
    }//if ($data == "")

    # This line will stream the file to the user rather than spray it across the screen
    header("Content-type: application/octet-stream");

    # replace excelfile.xls with whatever you want the filename to default to
    header("Content-Disposition: attachment; filename=excelfile.xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    echo $header."\n".$data;

    What's next??????????????
    :)
     
    mayuroak, Feb 6, 2008 IP