Hi all

I used for my site library PHPExcel
So for difderent pages i need to create different .xls files

i write a module that add for special type of content a form which contain a button, it have function for creating .xls file, but for different pages i need different function

I tried "switch", its ok, .xls create on my server, but it not download. Tried "if" the same.

here code:

function prof_nodeviewform_save () { 

lists_session ('gotov', $_GET['gotov']);
lists_session ('gotov_sel', $_GET['gotov_sel']);
$nodenid = $_GET['gotov'][0];

switch ($nodenid) {
case 9:
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';

$objPHPExcel = PHPExcel_IOFactory::load("exceltemplate/ENVD.xls");

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('C4', $_SESSION['gotov'][1])
            ->setCellValue('C5', $_SESSION['gotov'][2])
            ->setCellValue('C6', $_SESSION['gotov'][3])
            ->setCellValue('C7', $_SESSION['gotov'][4])
	    ->setCellValue('C8', $_SESSION['gotov_sel'][1])
            ->setCellValue('C9', $_SESSION['gotov'][5])
	    ->setCellValue('C10', $_SESSION['gotov_sel'][2])
	    ->setCellValue('C11', $_SESSION['gotov_sel'][3])
            ->setCellValue('C12', $_SESSION['gotov'][6])
            ->setCellValue('C13', $_SESSION['gotov'][7])
            ->setCellValue('C14', $_SESSION['gotov'][8])
	    ->setCellValue('C15', $_SESSION['gotov_sel'][4])
            ->setCellValue('C16', $_SESSION['gotov'][9])
            ->setCellValue('C17', $_SESSION['gotov'][10])
            ->setCellValue('C18', $_SESSION['gotov'][11])
            ->setCellValue('C19', $_SESSION['gotov'][12])
            ->setCellValue('C20', $_SESSION['gotov'][13])
            ->setCellValue('C21', $_SESSION['gotov'][14])
            ->setCellValue('C22', $_SESSION['gotov'][15])
            ->setCellValue('C23', $_SESSION['gotov'][16]);

$objWriter->save('exceltemplate/ENVD.xls');

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="ENVD.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
break;
}
}

Sorry for my english, i hope you understand me.

Comments

Raf’s picture

So far, what you got is right, except for one step.

To give a user an Excel file for download, you got to:
1. Set the headers to tell the browser it's an Excel file <-- CHECK
2. Create the Excel file's contents <-- CHECK
3. Render the Excel file's contents (by, eg. using print, or a method render if it's available) <-- Still need to do this

If you do step 3, the user's browser'll get told that the contents it receives, it an Excel file, and will actually receive the contents it has to treat like such a file. Otherwise, with only step 1 and 2, the browser will be told that the contents it receives, is an Excel file, but the server doesn't send any contents to the browser.

RAD-lance’s picture

Thank for answer.

Im not realy sure how to do step 3, is it print $output , where $output is header and excel content?
Because I didn't found render method or something like this in PHPExcel.

I want to remember that if I dont use switch or if its work good, browser receive file

Raf’s picture

Just checked the documentation, and the save method should do the trick. So your code should work just fine. The only two things I can think of, that might result in this are:

1. Something's been sent to the browser before the headers (like an echo or print or other headers() call). You'd get errors then. If they don't appear, try checking Watchdog (/admin/reports/dblog) for any logged errors about this in there.

2. The documentation states the following:

PHPExcel_Writer_Excel2007 uses temporary storage when writing to php://output. By default, temporary files are stored in the script’s working directory. When there is no access, it falls back to the operating system’s temporary files location.

You're using Excel 5 instead of Excel 2007, but it might be the same for that. Not sure. Since the file appears on your server, but not on the client's browser, I'd guess that's what they mean with "By default, temporary files are stored in the script's working directory." If this is the problem, try making it so your script doesn't have access to the folder.

texas-bronius’s picture

From http://drupal.stackexchange.com/a/132188 I solved my similar situation by simply adding

exit();

as the last line [edit: oh, instead of break!]. This works for query form submit handler calling my PHPExcel generator in Drupal.

--
http://drupaltees.com
80s themed Drupal T-Shirts