Asher Sommer
Asher Sommer

Reputation: 53

PHP PEAR spreadsheet Excel Writer data limit

I have a problem with PEAR and this issue seems quite strange.

I am generating a Spreadsheet with php where I loop several passages to create a sizing table. As long As I just use few details in the chart the script will be executed, but there seems to be a limit. Once above that number I will get a Fatal Error Message.

This is the code I'm using for the workbook:

require_once '../Spreadsheet/Excel/Writer.php';

        $workbook = new Spreadsheet_Excel_Writer('sizecharts/'.$file.'.xls');

        $worksheet =& $workbook->addWorksheet('NL Worksheet '.$itemname);
        $workbook->setVersion(8);

        $format_top =& $workbook->addFormat();
        $format_top->setAlign('top');
        $format_top->setTextWrap(1);

        $format_center =& $workbook->addFormat();
        $format_center->setAlign('center');

        $format_heading1 =& $workbook->addFormat();
        $format_heading1->setAlign('center');
        $format_heading1->setBold(1);
        $format_heading1->setSize(16);

        $format_info =& $workbook->addFormat();
        $format_info->setAlign('center');
        $format_info->setBold(1);
        $format_info->setSize(12);

        $bold =& $workbook->addFormat();
        $bold->setBold(1);

        $details =& $workbook->addFormat();
        $details->setBorder(1);

        $details_header =& $workbook->addFormat();
        $details_header->setBold(1);
        $details_header->setBorder(1);

        $details_cell =& $workbook->addFormat();
        $details_cell->setBorder(1);
        $details_cell->setAlign('center');

        $details_cell_header =& $workbook->addFormat();
        $details_cell_header->setBold(1);
        $details_cell_header->setBorder(1);
        $details_cell_header->setAlign('center');

        $details_num =& $workbook->addFormat();
        $details_num->setBold(1);
        $details_num->setBorder(1);
        $details_num->setAlign('right');


        $worksheet->setMerge(1,0,1,$size_count-1);
        $worksheet->setMerge(3,0,3,$size_count-1);
        $worksheet->setMerge(5,0,5,$size_count-1);

        $worksheet->setInputEncoding('utf-8');
        $worksheet->write(1, 0, 'MYCOMPANY', $format_heading1);
        $worksheet->write(3, 0, '耐斯德克服饰(上海)有限公司', $format_heading1);
        $worksheet->write(5, 0, 'Measurements  '.$itemname, $format_info);

        $worksheet->setInputEncoding('ISO-8859-7');
        $worksheet->write(7, $size_count-2, 'File#:');
        $worksheet->write(7, $size_count-1, $file);

        $worksheet->write(8, 0, 'Date:');
        $worksheet->write(8, 1, $date);

        $worksheet->write(9, 0, 'Customer:');
        $worksheet->write(9, 1, $customer);

        $worksheet->write(10, 0, 'Item Type:');
        $worksheet->write(10, 1, $type);

        $worksheet->write(11, 0, 'Units:');
        $worksheet->write(11, 1, $unit);

        $worksheet->setColumn(0,0,10);
        $worksheet->setColumn(0,1,30);

        $worksheet->write(13, 0, '#',$details_num);
        $worksheet->write(13, 1, 'Details:',$details_header);




        for($k=1;$k<($size_count-1);$k++){

            $size_collect_xls   = $sizes_arr[$k];

            $worksheet->setInputEncoding('utf-8');  
            $worksheet->write(13,$k+1, $size_collect_xls,$details_cell_header);
            $worksheet->setInputEncoding('ISO-8859-7');

        }


        for($i=0;$i<$count;$i++){

            if(isset($_POST['details'][$i])){   

            $detail             = $_POST['details'][$i];    

            $worksheet->write(14+$i, 0, ($i+1).'.',$details_num);
            $worksheet->write(14+$i, 1, $detail,$details);

            $det_collect        = "";

                        for($k=1;$k<($size_count-1);$k++){


                            $key                = "det".$k;

                            $det_collect_val    = $_POST[$key][$i]; 

                            $worksheet->write(14+$i,$k+1, $det_collect_val,$details_cell);

                        }


            }

        }           


        // We still need to explicitly close the workbook
        $workbook->close();
        ?>

I am using this to write the file.

What I later get is this error Message

 <b>Fatal error</b>:  Call to undefined method PEAR_Error::setInputEncoding() in <b>/home/MYCOMPANY/www/www/quotbot/sizechartinput.php</b> on line <b>740</b><br />

Which refers to this line of code:

 $worksheet->setMerge(1,0,1,$size_count-1);

If I exchange this line with lets say this:

 $worksheet->setInputEncoding('utf-8');

My error Message changes to this:

 <b>Fatal error</b>:  Call to undefined method PEAR_Error::setInputEncoding()

In this case the error reports there is a problem with the encoding instead of the cell. So this means there is no problem with the command. It's more of a problem with the amount of data in the generated worksheet.

So is there any restriction for the amount of data inserted into the excel worksheet?

Upvotes: 2

Views: 1567

Answers (1)

T. Fabre
T. Fabre

Reputation: 1527

It's unrelated.

The line

$worksheet =& $workbook->addWorksheet('NL Worksheet '.$itemname);

Can either return "a reference to a worksheet object on success, PEAR_Error on failure" (ref here). The error message tells you that you are calling an undefined method on PEAR_Error, meaning that your call failed.

You need to check after that line that addWorksheet was succesful. Simple example taken from the documentation:

// Make sure the worksheet name is less than 31 characters
$worksheet =& $workbook->addWorksheet(substr('NL Worksheet '.$itemname, 0, 31));
if (PEAR::isError($worksheet)) {
    die($worksheet->getMessage());
}

Upvotes: 2

Related Questions