The converter failed to save the file from mySQL

Sep 8, 2009 at 3:49 AM

How to save file from mySQL? I got error message when dowload and save file as

The converter failed to save the file

Code

<?php

// database connection ...

// 01simple.php -- Send output to client browser instead of saving to diskerror_reporting(E_ALL);
ini_set('display_errors', '1');
/** PHPExcel */
require_once '../phpexcel1.7.0/Classes/PHPExcel.php';

/** PHPExcel_IOFactory */
require_once '../phpexcel1.7.0/Classes/PHPExcel/IOFactory.php';
/** PHPExcel_richText.php */
//require_once '../phpexcel1.7.0/Classes/PHPExcel/RichText.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
                             ->setLastModifiedBy("Maarten Balliauw")
                             ->setTitle("Office 2007 XLSX Test Document")
                             ->setSubject("Office 2007 XLSX Test Document")
                             ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                             ->setKeywords("office 2007 openxml php")
                             ->setCategory("Test result file");
// Add data for export sample from 01 that OK to save file
/*
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Hello')
            ->setCellValue('B2', 'world!')
            ->setCellValue('C1', 'Hello2')
            ->setCellValue('D2', 'world2!');
*/

// Add data for export sample from MySQL that failed to save the file

$query = $db->query("select * from category where reID=0 order by ID");  //
    $i=1;
    while($row = $db->fetch_array($query)){ //category 
     $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('ID'.$i, $row[ID])
            ->setCellValue('type'.$i, $row[type])
            ->setCellValue('title'.$i, $row[title])
            ->setCellValue('regDate'.$i, $row[regDate]);  
     $i++; 
 }

// Rename sheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

// Save Excel 2007 file
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="mysql_export.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;

?>

Developer
Sep 8, 2009 at 4:05 AM

This looks like an error:

->setCellValue('ID'.$i, $row[ID])
->setCellValue('type'.$i, $row[type])
->setCellValue('title'.$i, $row[title])
->setCellValue('regDate'.$i, $row[regDate]);  


Should it not be this?

->setCellValue('A'.$i, $row[ID])
->setCellValue('B'.$i, $row[type])
->setCellValue('C'.$i, $row[title])
->setCellValue('D'.$i, $row[regDate]);   

 

Coordinator
Sep 8, 2009 at 8:28 AM

And just for luck, associative indexes should be quoted:

->setCellValue('A'.$i, $row['ID'])
->setCellValue('B'.$i, $row['type'])
->setCellValue('C'.$i, $row['title'])
->setCellValue('D'.$i, $row['regDate']); 
Sep 9, 2009 at 2:41 AM
Thanks lots koyama & MarkBaker,
 
------------- Now all work
$objPHPExcel->setActiveSheetIndex(0) //title
            ->setCellValue('A1', 'ID')
            ->setCellValue('B1', 'type')
            ->setCellValue('C1', 'title')
            ->setCellValue('D1', 'regDate');
   
$query = $db->query("select * from category where reID=0 order by ID");  //data
    $i=2;
    while($row = $db->fetch_array($query)){ //category 
     $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.$i, $row[ID])
            ->setCellValue('B'.$i, $row[type])
            ->setCellValue('C'.$i, $row[title])
            ->setCellValue('D'.$i, $row[regDate]);  
     $i++; 
 }
On Tue, Sep 8, 2009 at 5:28 PM, MarkBaker <notifications@codeplex.com> wrote:

From: MarkBaker

And just for luck, associative indexes should be quoted:

->setCellValue('A'.$i, $row['ID'])
->setCellValue('B'.$i, $row['type'])
->setCellValue('C'.$i, $row['title'])
->setCellValue('D'.$i, $row['regDate']); 

Read the full discussion online.

To add a post to this discussion, reply to this email (PHPExcel@discussions.codeplex.com)

To start a new discussion for this project, email PHPExcel@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com