2

I have the following code that exports a csv file :

//Selecting Item Data based on Date 
$result = mysql_query("SELECT item_no, qty, discount_price, date
  FROM sold_items WHERE date BETWEEN '".$fdate."' AND '".$tdate."'
  ORDER BY date Desc");

$filename = "C:/xampp/htdocs/frame/Sales".".csv";
if(file_exists($filename)) {
    $f = fopen("{$filename}", 'w');
    //Fputs the Table Headers
    fputcsv($f, array('Item No', 'Qty', 'Sell Price', 'Date'));

    //Fputcsv adds records to csv file
    while($row = mysql_fetch_array($result, MYSQL_NUM))
    {
        fputcsv($f, $row);
    }
    //Close file
    fclose($f);
}

The above code works perfectly, the problem is that i need to stop exporting csv from now on and export an xls file. I've changed $filename into:

$filename = "C:/xampp/htdocs/frame/Sales".".xls";

The format of xls is not correct, its like all records are place inside one field in the excel file. I've tried googling and saw some complicated ways phpexcel and something that I could not understand. Is there a simple way of doing it? Maybe with /t to make each record as a seperate field in excel.

2
  • 1
    Have you tried phpexcel.codeplex.com ? Commented Sep 27, 2012 at 13:31
  • 1
    Excel files are a little more complicated than just raw data. It has a specific layout. Use one of the many PHP Excel libraries available. Commented Sep 27, 2012 at 13:36

1 Answer 1

2

Can you use libraries like those

include ‘library/PHPExcel.php’;
include ‘library/PHPExcel/Writer/Excel2007.php’;

See here http://phpexcel.codeplex.com/

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.