4

im working in a php project in witch i have to send a mail with an excel attachment , for the mail im using phpmailer. Im having a problem with the format of the excel file that the code below produces.

$table.='<table></table>';
    if (file_exists("EXCEL REPORT/".$subject." ".date("M-d-Y").".xls")) 
    {
        unlink("EXCEL REPORT/".$subject." ".date("M-d-Y").".xls");
        file_put_contents("EXCEL REPORT/".$subject." ".date("M-d-Y").".xls", $table); 
        sendmailatt($to,$cc,$subject." ".date("M-d-Y"),"mail",<br>REGIA","EXCEL REPORT/".$subject." ".date("M-d-Y").".xls");
    } 
    else {
       file_put_contents("EXCEL REPORT/".$subject." ".date("M-d-Y").".xls", $table); 
       sendmailatt($to,$cc,$subject." ".date("M-d-Y"),"Questo e' un invio automatico del venduto per agent.<br><br>Saluti,<br>REGIA","EXCEL REPORT/".$subject." ".date("M-d-Y").".xls");
    }

i want to convert the attachment into excel 2007 before sending the mail... is there any easy way for achieving this

SierraOscar
  • 17,507
  • 6
  • 40
  • 68
SnakeFoot
  • 174
  • 3
  • 16
  • 2
    This code doesn't actually produce an excel file, it simply writes a block of html markup (`$table.='
    ';`) to a file and gives it an extension of .xls.... that isn't an Excel file at all.... if you want an Excel file in any native Excel format (BIFF, OfficeOpenXML, even SpreadsheetML), then look at some of the libraries for PHP that can write native file formats, such as [PHPExcel](https://github.com/PHPOffice/PHPExcel)
    – Mark Baker Mar 24 '16 at 10:49
  • i replace the content for privacy issues.... but the code produces an excel file. – SnakeFoot Mar 24 '16 at 10:55
  • 1
    I beg to differ.... html markup is not a native Excel file format; and unless you're using magic somewhere in your universe, file_put_contents() will not convert html markup into a native format Excel file.... just because you give a file an extension of .xls, doesn't make it an Excel file, and just because MS Excel can import html markup, doesn't make it an Excel format – Mark Baker Mar 24 '16 at 10:57
  • I know that but i was looking if there is any class that converts the file without actually making the code any slower? – SnakeFoot Mar 24 '16 at 10:58
  • If your want a BIFF format or an OfficeOpenXML format excel file, then you need to write the appropriate format to that file – Mark Baker Mar 24 '16 at 10:59
  • 1
    `is there any class that converts the file without actually making the code any slower ` No. I'm afraid magic doesn't exist in this universe outside of Harry Potter – Mark Baker Mar 24 '16 at 10:59
  • Of course, how much slower depends on whether you need to generate html markup and then convert it to Excel, or simply create the Excel data directly – Mark Baker Mar 24 '16 at 11:01
  • This is two questions - figure out your file conversion first, worry about email attachments later - PHPMailer doesn't care what file type you use. (and hello Mark!) – Synchro Mar 24 '16 at 11:04
  • Well clarify with your client whether they really want an OfficeOpenXML format .xlsx file, or whether a file of html markup with an xlsx extension is ok with them.... I'm guessing from the fact that you've been asked to change it that they want OfficeOpenXML format – Mark Baker Mar 24 '16 at 11:08
  • the code works... the issue is the excel format – SnakeFoot Mar 24 '16 at 11:09
  • yes, it needs to be in xlsx – SnakeFoot Mar 24 '16 at 11:10
  • @Mark Baker: Excel will not opening "a file of html markup with an xlsx extension". With an xls extension it will but also not without a warning. – Axel Richter Mar 24 '16 at 11:28
  • 1
    So if it needs to be native xlsx format (which is OfficeOpenXML format) then you need to take a look at some of the libraries for writing native format Excel, such as PHPExcel..... yes they will be slower to create the file (depending on how you produced your original markup) – Mark Baker Mar 24 '16 at 11:34
  • It is possible to take a file of HTML markup and convert it to an OfficeOpenXML (xlsx) file with PHPExcel, though it may lose some formatting because it isn't a full-blown html parser; but depending on how you would generate the markup, you could write the data directly to PHPExcel, possibly with a template for the styling, which may be easier and gives a lot more flexibility – Mark Baker Mar 24 '16 at 11:36

1 Answers1

3

Here is a code example of converting from .xls to .xlsx using PHPExcel (it might be deprecated, but it still works just fine, especially for a simple conversion of Excel5 to Excel2007):

<?php

    $xls_to_convert = 'test.xls';

    //------------------------------------------------------------------------------------

    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

    //These four lines are the entire script
    require_once dirname(__FILE__) . '/PHPExcel/Classes/PHPExcel/IOFactory.php';
    $objPHPExcel = PHPExcel_IOFactory::load(dirname(__FILE__) . '/' . $xls_to_convert);
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save(str_replace('.xls', '.xlsx', $xls_to_convert));

    echo 'File ' . str_replace('.xls', '.xlsx', $xls_to_convert) . ' created in ' , getcwd() , EOL;

The above example assumes:

1) PHPExcel is installed into your web files folder (e.g. home/username/public_html, xampp/htdocs, var/www, etc)

2) test.xls is also located in the same folder

3) Tested with the latest (final) version of PHPExcel, 1.8

4) The code is taken from PHPExcel/Examples/07reader.php

Many thanks to Mark Baker for the PHPExcel and PhpSpreadsheet applications!

cssyphus
  • 37,875
  • 18
  • 96
  • 111