"Cannot modify header information" error when closing workbook

Re: "Cannot modify header information" error when closing workbook

by Mayank Madhav -
Number of replies: 0
Hi Jean,

I am using Moodle 1.9.9. I am facing same problem as you did. All other existing moodle files using MoodleExcelWorkBook class to create workbook,write on SpreadSheets are working fine but when i created table and wrote it to Excelsheet just like your code, it faults around $workbook->close() and prints all sorts of characters on the screen.

I was getting same Warning due to whitespaces before and after my php tag,on rectifying whitespaces the warning goes but I keep getting same garbage on my screen triggered by workbook.close()

I looked at course/reports/log, and grade/report/grading/report.php for similar code. Even when i am doing exactly like their code the error persists.

A different test code I wrote just to see working of MoodleExcelWorkbook is working and giving file to download. I think something is inserting whitespace in my code

<?php
require_once('config.php');
require_once($CFG->dirroot.'/lib/excellib.class.php');
$variable = $_GET['variable'];
if($variable !=null && $variable == 'var'){
$strgrades = get_string('grades');
$downloadfilename = clean_filename("$strgrades.xls");
/// Creating a workbook
$workbook = new MoodleExcelWorkbook("-");
/// Sending HTTP headers
$workbook->send($downloadfilename);
/// Adding the worksheet
$myxls =& $workbook->add_worksheet($strgrades);

/// Print names of all the fields
$myxls->write_string(0,0,"firstname");
$myxls->write_string(0,1,"lastname");
$myxls->write_string(0,2,"idnumber");
$myxls->write_string(0,3,"institution");
$myxls->write_string(0,4,"department");
$myxls->write_string(0,5,"email");
$pos=6;
$i = 0;
while ($i<2) {
$i++;
$myxls->write_string($i,0,"manis");
$myxls->write_string($i,1,"tam");
$myxls->write_string($i,2,"mkt001");
$myxls->write_string($i,3,"CDAC Mumbai");
$myxls->write_string($i,4,"ETU");
$myxls->write_string($i,5,"tamta.manish@gmail.com");
}
}
if($variable !=null && $variable=='var')
{
if($workbook != null) {
if($variable == 'var'){
$workbook->close();
exit;
}
}
}
$link='index.php';
$option['variable']='var';
$label="Download";
print_single_button($link, $option, $label);
?>

In my code I am just writing simultaneously to a table and ExcelWorksheet in same Foreach...for data to appear on table on page as well as write to ExcelSheet. Did the 'flexible_table' work for you?