SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    IL
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP export to xlsx?

    I found this great site that shows you how to export to Excel through PHP but it exports to the old .xls format. Does anyone know how to tweak this so that it exports to .xlsx instead?

    http://www.phpsimple.net/tutorials/mysql_to_excel/

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Take a look at PHPExcel, does exactly what it say's on the tin.

    Off Topic:

    Pft, I'm so impressionable by marketing these days!
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Off Topic:

    Pft, I'm so impressionable by marketing these days!
    Beaten to it by over half an hour, I'm slipping... put it down to my rewriting of the PHPExcel calculation engine taking a lot more of my time these days.
    I'm not sure it can be called marketing if it's a FOSS library can it?
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  4. #4
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    IL
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I hesitate because what I have works beautifully except for the fact that it doesn't export to xlsx.

    In playing around with phpexcel I see that it saves a copy of an excel file to the same directory that the php file is in. How do I get it to display a 'save or open' window instead?

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DKY View Post
    In playing around with phpexcel I see that it saves a copy of an excel file to the same directory that the php file is in.
    The sample test scripts do, but you're not obliged to.The argument for the object writer save method is a standard directory/filename

    PHP Code:
    $objWriter PHPExcel_IOFactory::createWriter($objPHPExcel'Excel2007');
    $objWriter->save('/usr/local/myWorkbooks/workbook1.xlsx'); 
    Quote Originally Posted by DKY View Post
    How do I get it to display a 'save or open' window instead?
    PHP Code:
    // redirect output to client browser
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="workbook1.xlsx"');
    header('Cache-Control: max-age=0');

    $objWriter PHPExcel_IOFactory::createWriter($objPHPExcel'Excel2007');
    $objWriter->save('php://output'); 
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  6. #6
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    IL
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think I ever got around to playing with the open/save dialog but I can't get the code above to work. I have placed it in the "01simple.php" so that it looks like this now
    PHP Code:
    // Save Excel 2007 file
    echo date('H:i:s') . " Write to Excel2007 format\n";
    //$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    //$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
    // redirect output to client browser
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="workbook1.xlsx"');
    header('Cache-Control: max-age=0');
    $objWriter PHPExcel_IOFactory::createWriter($objPHPExcel'Excel2007');
    $objWriter->save('php://output'); 
    but I get a browser window with a bunch of (here are the two top lines)
    07:31:12 Create new PHPExcel object 07:31:12 Set properties 07:31:12 Add some data 07:31:12 Rename sheet 07:31:12 Write to Excel2007 format PK;< ![[Content_Types].xml­MN0=-Jܲ@%*Q`Icձ-{w{&I E j7"og%NKϦby]*yHFp4SH߸A\a­XJD-dH­

  7. #7
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    IL
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nevermind, I figured it out by looking at the "01 simple-download-xlsx.php" example file.

  8. #8
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mark Baker View Post
    The sample test scripts do, but you're not obliged to.The argument for the object writer save method is a standard directory/filename

    PHP Code:
    $objWriter PHPExcel_IOFactory::createWriter($objPHPExcel'Excel2007');
    $objWriter->save('/usr/local/myWorkbooks/workbook1.xlsx'); 


    PHP Code:
    // redirect output to client browser
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="workbook1.xlsx"');
    header('Cache-Control: max-age=0');

    $objWriter PHPExcel_IOFactory::createWriter($objPHPExcel'Excel2007');
    $objWriter->save('php://output'); 
    I use the code, and it runs ok. but the excel file download from website opened with some warning :

    Excel found unreadable content in demo.xlsx. Do you want to recover the contents of this workbook? If you trust the source of the worksheet, click yes.

    Can anyone help me?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •