0

[This guy seems to have the same problem as me but it's Java and I don't understand anything :/ excel can not open the downloaded file through jsp ] Hello everyone,

First of all, I want to say that English isn't my mother language so you might see some mistakes but I hope you will understand me. So I'm a student and I have a 'student job', creating a website for a micro-bakery! They need to have a file with all their clients and I decided to use PhpExcel to generate it!

I did everything properly but it still doesn't work! I looked for help on google of course but nobody has the same error message as me. Here is what I get.

Error message I get when I try to open the file : In english : "Impossible to open the file "....xlsx" because it's format or extension is not valid. Please verify that the file is not broken and that it's extension match the file format"

The source code is here : `

if($action=="client-excel"){
    /** Error reporting */
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    date_default_timezone_set('Europe/London');


    /** Include PHPExcel */
    require_once '../libs/phpExcel/PHPExcel.php';


    // Create new PHPExcel object
    $objPHPExcel = new PHPExcel();

    // Set document properties
    $objPHPExcel->getProperties()->setCreator("BakeryName")
                                                 ->setLastModifiedBy("Clients")
                                                 ->setTitle("Liste Clients ")
                                                 ->setSubject("Liste Clients ")
                                                 ->setDescription("Liste Clients ")
                                                 ->setKeywords("Liste Clients ")
                                                 ->setCategory("Liste Clients");

                    $toSupp=array();

                        $objPHPExcel->createSheet();
                        $array=array();
                        $array['header'][]="#client";
                        $array['header'][]="Nom";
                        $array['header'][]="Mail";
                        $array['header'][]="Nombre de pain(s) acheté(s)";
                        $array['data']=array();

                        $list = Client::getAllClients($order="id_client",$letter="",$valid=false,$start=0,$limit=100000);                                                           foreach($list as $client){
                            $client_array=array(
                                $client->getId(),
                                $client->getName(),
                                $client->getEmail(),
                                $client->getNbPainsCmd(),

                                );
                            $array['data'][]=$client_array;
                        }
                        if(sizeof($array['data'])==0){
                            $toSupp = $z;
                        }

                        $objPHPExcel->setActiveSheetIndex(0);

                        $j=0;
                        foreach($array['header'] as $h){
                            $objRichText = new PHPExcel_RichText();
                            $objRichText->createTextRun($h)->getFont()->setBold(true);
                             $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow ($j,1,  $objRichText);
                            $objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($j)->setAutoSize(true);
                            $j++;
                        }

                        for($i=0;$i<sizeof($array['data']);$i++){
                            for($j=0;$j<sizeof($array['data'][$i]);$j++){
                            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow ($j,$i+2, $array['data'][$i][$j]);
                            }
                        }                       
                        // Rename worksheet
                        $objPHPExcel->getActiveSheet()->setTitle("Liste clients");

                    // Redirect output to a client’s web browser (Excel2007)
                    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
                    header('Content-Disposition: attachment;filename="filename.xlsx"');
                    header('Cache-Control: max-age=0');

                        header('Cache-Control: max-age=1');

                        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
                    header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
                    header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
                    header ('Pragma: public'); // HTTP/1.0

                    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
                    $objWriter->save('php://output');
                    exit;

                }`

Thank you very much for all your potential answers!

Community
  • 1
  • 1
Alex
  • 1
  • 2
  • Open the file in a text editor, and look for any leading/trailing whitespace characters, BOM header, or any plaintext messages/html markup or similar... that will help to identify the problem – Mark Baker Mar 25 '17 at 17:50
  • Though I'm intrigued by why you're using a rich text object for setting the headers to bold when you could simply set the cell style to bold – Mark Baker Mar 25 '17 at 17:52
  • I don't understand what you are suggesting me to do :s Sorry and thanks for the help! – Alex Mar 25 '17 at 18:08
  • Instead of opening the file in MS Excel; open it in a text editor, like the editor you use to write your PHP scripts, or Windows Notepad.... and look to see if the file contains any PHP error messages – Mark Baker Mar 25 '17 at 18:34
  • Nope, just some random characters like this one : œ72ØÅŸRi«ÓúÓJÀؾ¥U – Alex Mar 26 '17 at 08:46

0 Answers0