I've been using the code below to successfully create spreadsheets for a few years. Now I have the need to insert page breaks at random times. I haven't found any code that does that without having to abandon this code and installing something completely different which I'd rather not do. This code is very simple and effective. Anyone know how? If it's easier creating a new worksheet that would be fine. Thanks in advance.

for ($y = 0; $y <= $totline; $y++) {

    $value = trim($fnames[$y]);

    if ((!isset($value)) OR ($value == "")) 
        {$value = "\t";}
    else 
        {$value = str_replace('"', '""', $value); 
        $value = '"' . $value . '"' . "\t";}

    $line .= $value;
}

$header = trim($line);

$line = " ";

for ($x = 1; $x <= $totrows; $x++) { 

    $rowarr = $arrin[$x];

    $result = strpos($rowarr[0], "-");

    if ($showroom == "yes" or $result === false) {

        for ($y = 0; $y <= $totline; $y++) {

            $value = trim($rowarr[$y]);

            if ((!isset($value)) OR ($value == "")) 
                {$value = "\t";}
            else 
                {$value = str_replace('"', '""', $value); 
                $value = '"' . $value . '"' . "\t";}

            $line .= $value; 
        }

    $data .= trim($line)."\n";
    $line = " ";
    }
}

if ($data == "") { 
    $data = "\n(0) Records Found!\n";                         
} 
$filename = "payroll_" . date('Ymd') . ".xls";

header("Content-type: application/x-msdownload"); 
header("Content-Disposition: attachment; filename=\"$filename\""); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
print "$header\n$data";
print "$header\n$data";   
?>

Recommended Answers

Have you tried adding \r\n rather than just \n?

Jump to Post

All 4 Replies

Have you tried adding \r\n rather than just \n?

I added the '\r\n' and now I get a blank line inserted on a break but not a new page. Here is the latest code:

for ($x = 1; $x <= $totrows; $x++) { 

    $rowarr = $arrin[$x];

    if (!$savedept) {
        $savedept = $rowarr[0];}

    if ($savedept <> $rowarr[0]) {
        $data .= "\r\n";           [B][COLOR="red"] //Want a page break here[/COLOR][/B]
        $savedept = $rowarr[0];}

    for ($y = 0; $y <= $totline; $y++) {

        $value = trim($rowarr[$y]);

        if ((!isset($value)) OR ($value == "")) 
            {$value = "\t";}
        else 
            {$value = str_replace('"', '""', $value); 
            $value = '"' . $value . '"' . "\t";}

        $line .= $value; 
        }

    $data .= trim($line)."\n";
    $line = " ";
}

I really don't think there is a character you can put in there that would force it to create a new worksheet or page. Basically you're just sending a tab delimited file with an Excel header. If you're going to get fancier, I think your only option is to use PEAR or something like PHPExcel. Either way, I think it's time to update. :)

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.