PHPExcel Warning/Error when opening document - php

I have built an Excel document using the PHPExcel Library. My document opens and looks fine both in Numbers (mac) and Office 2007 (windows). I am saving my file as a .xlsx file.
When I open the file, although it displays properly, it gives the following errors/warnings.
In Numbers, it gives the warning "Password protection on sheets isn't supported and was removed"
In Office 2007 it gives te error "Excel found unreadable content in 'filename.xlsx'. Do you want to recover the contents of this workbook? If you trust the source of this workbook, click Yes." When I click yes, everything displays fine, and it says "Removed Records: Merge cells from /sl/worksheets/sheet1.xml part".
It should be noted that I did not add any password to my file. I also have only one workbook. Anyone know how I can get rid of these or what might be causing them?

I've had the same problem and i resolved simply putting an
exit();
justafter the $objWriter->save('php://output'); command.
Eg.
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="industrializzazione_RTW_'.$rows[0]['stagione'].'.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit();

I ran into something similar while using CakePHP building a spreadsheet with mergecells. It works fine for Excel 2003 but not Excel 2007 format. If I build the 2007 spreadsheet using:
$objPHPExcel->getActiveSheet()->mergeCells('C30:E30');
the spreadsheet loads fine. However using a variable in any way such as these variants:
$mergestr = '\'C'.(30+$i).':E'.(30+$i).'\'';
$objPHPExcel->getActiveSheet()->mergeCells($mergestr);
$objPHPExcel->getActiveSheet()->mergeCells(eval("return \$$mergestr;"));
$objPHPExcel->getActiveSheet()->mergeCells('\'C'.(30+$i).':E'.(30+$i).'\'');
$objPHPExcel->getActiveSheet()->mergeCells('C'.(30+$i).':E'.(30+$i));
I get the same error you do. I'm not sure if there is a way to escape a variable in a way PHPExcel will like for the Excel2007 writer. The Excel5 writer works fine with the last example above. Hope this helps someone.

Related

Issues with exporting html to excel

For exporting our reports, we set the header to 'Content-Disposition: attachment;filename=fileName.xls' and dump the html to the output buffer and it downloaded the excel file nice and clean.
This was used to work until recently and now the downloaded file does not open in Excel. It doesn't give any error, just shows the blank gray screen similar to the screen when you close all the worksheets.
Strange enough, if you just open the file in Notepad++, add a space and save it, Excel WILL open it.
I suspect it to be something to do w/ the encoding of the file which is not acceptable to excel when the file is downloaded but something changes after you edit it in Notepad++ and excel would open it after the edit.
I have tried using ob_clean(), trim and converted the content to utf8 before dumping but nothing seems to work, what am I doing wrong?
Here is a sample code that recreates the issue I have:
ob_clean();
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=filename.xls");
header('Cache-Control: max-age=0');
header_remove('Pragma');
// simple html table
echo '<table><thead><tr><td>header 1</td><td>header 2</td></tr></thead><tbody><tr><td>content 1</td><td>content 2</td></tr></tbody></table>';
The above code snippet downloads the file 'filename.xls', excel initially does not open it, but after editing it the way I described earlier, excel WILL open it.
Thanks.
I had the exact same problem, then searching i found this!
"The Excel team has made a change in the behavior of certain file types to increase security." On: https://support.microsoft.com/en-us/kb/3181507
You can try one of the possible Solutions/Workaround.
The problem is the exel is blocking the unsafe document which downloads from internet.
To avoid this, you have change some settings in excel.
Options -> Trust Center -> Trust Center Settings -> Protacted View
Untick the Enable Protected View for files originated from the internet.
Save it
That's it.
You can open the files.

PHPExcel unable to open the saved file

I am trying to open an existing excel file, modify some cells and save it. I am using Excel2007 for reader and writer.
The input file is about 1 MB large and it has few formulas, protected data and hidden rows and columns and worksheets which I do not modify.
I am able to load the data and read and write some values into it, which I check with various var_dumps in the code.
The problem is while saving it. It throws some fatal errors on timing outs and also if it writes the file the file size is bloated to 9.2 MB, which is okay if I can open it.
code snippet - nothing fancy.
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($inputFile);
$objPHPExcel->setActiveSheetIndex(2);
$activeSheet = $objPHPExcel->getActiveSheet();
$currCell = $activeSheet->getCell("O3");
$cellValidation = $currCell->getDataValidation("O3");
$values = array();
if ($cellValidation->getShowDropDown() == true)
{
$values = $cellValidation->getFormula1();
$valArray = explode(",", $values);
$currCell->setValue($valArray[0]);
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter -> setPreCalculateFormulas(false);
$objWriter->save($outputFile);
I use MS Excel 2010 to open the resultant file but it just takes forever and has not opened it even once.
Please help me to troubleshoot this by giving me pointers as to where I should be looking.
Any help is greatly appreciated.
Instead of saving it to a file, save it to php://output­Docs:
$objWriter->save('php://output');
This will send it AS-IS to the browser.
You want to add some headers­Docs first, like it's common with file downloads, so the browser knows which type that file is and how it should be named (the filename):
// 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');
First do the headers, then the save. For the excel headers see as well the following question: Setting mime type for excel document.
So the final code would have below lines -
// Save Excel 2007 file
#echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
// 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.xlsx"');
$objWriter->save('php://output');
I think this line:
ob_end_clean();
Should solve your problem.
Thanks!
There's a whole lot of reasons for that "bloat" and it very much depends on the actual data in the worksheet, but MS Excel itself uses a lot of different techniques to keep the filesize small, whereas PHPExcel writes a simple version of the OfficeOpenXML format.
For example, MS Excel looks at the string content of all cells, and stores the individual strings in a string table. If a string is used by two or more cells, there will only be a single entry in the string table. However, there's a performance overhead in checking if a string already exists in the string table, so PHPExcel doesn't perform that check but will duplicate entries in the string table. This means that it will create a large file because of the duplication, but keeps the save speed as fast as possible.
Similarly, MS Excel looks at all formulae, and if the formula is similar to an existing formula (with only a row/column offset difference) it will store it as a shared formula rather than a cell formula, so the actual formula data is only stored once. Again, PHPExcel won't perform this check, because it is a big performance overhead in the save, so it stores every formula as a cell formula rather than a shared formula.
And no, I can't explain why the file doesn't load in MS Excel 2010, nor will I be able to explain it without being able to run the whole thing through debug

PHPExcel file not in the proper Microsoft Excel 2003 XLS format

I'm generating a Excell 2003 using PHPExcel 1.8 with this code:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('toUploadFtp.xls');
The file seems ok, and I can read it using MS Office and LibreOffice.
But I have to upload the file to a system that says the format has to be Excell 2003, and he prompts next error:
Oops! Your file is not in the proper Microsoft Excel 2003 XLS format.
If I open the file with LibreOffice and save it again, then I can upload the file correctly to the system.
Any idea?
Thanks,
Create your file by passing the proper version to factory:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
Hope it helps.
Not understanding the exact problem entirely: it seems to be with the additional transfer of the Excel file.
One error is often, that the PHP script outputs spurious data.
Leaving out the final %> is a standard trick to make sure no extra final new line characters are added to the file.
Comparing the files generated and transferred should yield insight on what went wrong.
You may need to set the content to non-text to prevent some text conversion:
header('Content-Type: application/octet'):
Finally I couldn't find a solution. It has to be some kind of bug in the XLS 2003 format.
But I generated a XLSX with $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); and the system recognizes it correctly.
I think you can overcome this problem in changing the filed of Excel5.Since i havnt tried phpexcel i dont know much about this..But i think you can find a solution for this [here].1

PHPExcel error when opening created File in EXCEL

I create with a php-script which uses the PHPExcel Library an simple .xlsx file. But when I want to open it in MS Excel 2010 on Win7, I get an error message that file format is wrong or the file is damaged. Tried several possible solutions from the internet but nothing worked for me.
public function createControllingFile($suffix){
$this->PHPExcel = new PHPExcel();
$year = date('y');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Cache-Control: max-age=0');
$this->objWriter = PHPExcel_IOFactory::createWriter($this->PHPExcel, 'excel5');
$this->objWriter->save('tmp/controlling_'.$year.'_'.$suffix.'.xlsx');
$_SESSION['counter'] = 0;
exit();
}
Hope you can help me, the session thing is to count up something
$this->objWriter->save('tmp/controlling_'.$year.'_'.$suffix.'.xlsx');
saves to a file on your servers filesystem, and nothing is sent to the client browser.
If you want to send to the client's browser, change that line to:
$this->objWriter->save('php://output');
just like in 01simple-download-xlsx.php in the /Tests or /Examples directory

Rows written by PHPExcel cannot be read from other libraries

I know this isn't the right place to ask about this specific vague problem, but maybe someone knows this library well enough to enlighten me. Here is the thing:
I am writting an Excel5 over an existing Excel file with PHPExcel. I need to upload that Excel to the Zoom website, so it can provide me with a list of tracking numbers. However, for some reason the library they are using to read the uploaded Excel files cannot read the rows written by PHPExcel and the only solution I've found so far is to manually copy the contents of my dynamically generated Excel to another document using MS Excel 2007.
In other words, the Zoom website can read the rows written natively by Excel but not rows written by PHPExcel. My file has only one single sheet, and I can open it no problem with Excel 2007.
Even if I manually add some rows to the template and then add more rows with PHPExcel, Zoom will read the rows written manually by me, but not the rows written by PHPExcel.
This is how I'm doing it:
// Starting with the PHPExcel library
$this->load->library('PHPExcel');
$this->load->library('PHPExcel/IOFactory');
$template_file = 'zoom_tracking_template.xls';
$i = 3;
$objReader = IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load($template_file);
$objPHPExcel->setActiveSheetIndex(0);
// Fetching ML payments
foreach($payments as $row)
{
$objPHPExcel->getActiveSheet()->setCellValue('A'.$i, 'VANESSA NEISZER');
$objPHPExcel->getActiveSheet()->setCellValue('B'.$i, '02127616116');
$objPHPExcel->getActiveSheet()->setCellValue('C'.$i, '1ER PISO MINITIENDAS 199 BLVD SABANA GRANDE, CRUCE C / CALLE NEGRIN');
$objPHPExcel->getActiveSheet()->setCellValue('D'.$i, $row->mailing_city);
$objPHPExcel->getActiveSheet()->setCellValue('E'.$i, $row->mailing_name);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$i, $row->mailing_name);
$objPHPExcel->getActiveSheet()->setCellValue('G'.$i, $row->mailing_personal_id);
$objPHPExcel->getActiveSheet()->setCellValue('H'.$i, $row->mailing_phone);
$objPHPExcel->getActiveSheet()->setCellValue('I'.$i, $row->mailing_address1.' '.$row->mailing_address2);
$objPHPExcel->getActiveSheet()->setCellValue('J'.$i, $row->nickname);
$objPHPExcel->getActiveSheet()->setCellValue('K'.$i, '1');
$objPHPExcel->getActiveSheet()->setCellValue('L'.$i, '0.3');
$objPHPExcel->getActiveSheet()->setCellValue('M'.$i, 'M');
$objPHPExcel->getActiveSheet()->setCellValue('N'.$i, 'PRODUCTO');
$objPHPExcel->getActiveSheet()->setCellValue('O'.$i, '0');
$i++;
}
$objPHPExcel->setActiveSheetIndex(0);
$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
// Sending headers to force the user to download the file
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="Envios'.date('dMy').'.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
I have no clue of what PHP library they are using to read Excel files and I am certain they wont tell me if I ask them. I know they use PHP, and their library only read Excel 2003 files, however, I don't know why they can't read my files but they can read other files written manually on MS Excel.
Any clues, ideas or suggestions I could try would be greatly appreciated.
And PHPExcel's main developer is looking at this issue (among others), somewhere in between trying to find a new day job and having a life. I'm not familiar with the zoom website, or the software that they use. PHPExcel BIFF8 files can be read by Excel, OOCalc and Gnumeric without error... but a couple of questions spring to mind.
What version of PHPExcel?
Does any of the data contain UTF-8 characters?
Are there any formulae in the template worksheet?
If so, what are they?

Categories