Reputation: 12537
For a long time now, I have been trying to give users the ability to 'save as' or 'open' the created spreadsheet using the dialog box.
This is what I tried, I am getting no errors, just gibberish characters on the screen:
//CREATE SPEADSHEET
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');
// It will be called file.xls
header('Content-Disposition: attachment; filename="file.xls"');
// Write file to the browser
$objWriter->save('php://output');
I would greatly appreciate any help with figuring this one out.
Upvotes: 3
Views: 5028
Reputation: 2342
I had the same problem, i was getting gibberish in the response. and the excel could not be open. The solution is not to use form submit or ajax call, only use document.location to forward the process of the excel generator. For example:
document.location="/php/Controller/YnrAPICall.php?is_string=1¶ms="+ JSON.stringify(params);
Upvotes: 0
Reputation: 6913
I just tried your part of the code and it worked just fine, if you are getting just gibberish, the problem coud actually be on the part where you are generating the table, check that part to see if there is any mistake, bellow is the code I used to test it:
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
/** Include PHPExcel */
require_once '../../PHPExcel/Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Lumina")
->setLastModifiedBy("Lumina")
->setTitle("Entry members")
->setSubject("Entry members")
->setDescription("List of members participting.")
->setKeywords("office PHPExcel php")
->setCategory("Entry members");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', $row_titles['TITLE01'])
->setCellValue('B1', $row_titles['TITLE02'])
->setCellValue('C1', $row_titles['TITLE03'])
->setCellValue('D1', $row_titles['TITLE04'])
->setCellValue('E1', $row_titles['TITLE05'])
->setCellValue('F1', $row_titles['TITLE06'])
->setCellValue('G1', $row_titles['TITLE07'])
->setCellValue('H1', $row_titles['TITLE08'])
->setCellValue('I1', $row_titles['TITLE09'])
->setCellValue('J1', $row_titles['TITLE10'])
->setCellValue('K1', $row_titles['TITLE11'])
->setCellValue('L1', $row_titles['TITLE12'])
->setCellValue('M1', $row_titles['TITLE13'])
->setCellValue('N1', $row_titles['TITLE14'])
->setCellValue('O1', $row_titles['TITLE15'])
->setCellValue('P1', $row_titles['TITLE16'])
->setCellValue('Q1', $row_titles['TITLE17'])
->setCellValue('R1', $row_titles['TITLE18'])
->setCellValue('S1', $row_titles['TITLE19'])
->setCellValue('T1', $row_titles['TITLE20'])
->setCellValue('U1', $row_titles['TEAM_TX'])
->setCellValue('V1', $row_titles['T_SHIRT_SIZE'])
->setCellValue('W1', $row_titles['INPUT1_TX'])
->setCellValue('X1', $row_titles['INPUT2_TX'])
->setCellValue('Y1', $row_titles['INPUT3_TX'])
->setCellValue('Z1', $row_titles['INPUT4_TX']);
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Participants');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Save Excel5 file
//echo date('H:i:s') , " Write to Excel5 format" , EOL;
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.html', '.xls', __FILE__));
// We'll be outputting an excel file
header('Content-type: application/vnd.ms-excel');
// It will be called file.xls
header('Content-Disposition: attachment; filename="file.xls"');
// Write file to the browser
$objWriter->save('php://output');
I hope this can be of any help as I am also new to PHP.
Upvotes: 5