Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHPExcel] Export w locie
Forum PHP.pl > Forum > PHP
maniek74
Witam
Na stronie http://designconcept.webdev20.pl/articles/...anie-xls-w-php/ znalazłem jak wygenerować plik Excel.
Skopiowałem kod i jesli chce go zapisać na dysku to jest wszystko oki, ale jeśli chce go otworzyć w locie to Excel informuje o błedzie:
Program Excel nie może otworzyć pliku ze względu na nieprawidlowy format lub rozszerzenie pliku.

  1. <php
  2. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  3. header('Content-Disposition: attachment;filename=plik.xlsx');
  4. header('Cache-Control: max-age=0');
  5. $objWriter->save('php://output');
  6. ?>


Może ktoś mnie naprowadzić gdzie jest błąd??
Pozdrawiam
kapslokk
Jeżeli to co wrzuciłeś to cały Twój kod to:
1. Masz <php, a nie <?php
2. Używasz obiektu $objWriter, a nie utworzyłeś go.

Jeśli to nie jest cały kod, to wrzuć cały.
maniek74
Napisałem że wziołem ze strony nie pisałem calości żeby niezaśmiecać:
oto cały kod
  1. <?php
  2. // include PHPExcel
  3. require('PHPExcel.php');
  4. // create new PHPExcel object
  5. $objPHPExcel = new PHPExcel;
  6. // set default font
  7. $objPHPExcel->getDefaultStyle()->getFont()->setName('Calibri');
  8. // set default font size
  9. $objPHPExcel->getDefaultStyle()->getFont()->setSize(8);
  10. // create the writer
  11. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
  12.  
  13. /**
  14.   * Define currency and number format.
  15.   */
  16. // currency format, € with < 0 being in red color
  17. $currencyFormat = '#,#0.## \&#8364;;[Red]-#,#0.## \&#8364;';
  18. // number format, with thousands separator and two decimal points.
  19. $numberFormat = '#,#0.##;[Red]-#,#0.##';
  20.  
  21. // writer already created the first sheet for us, let's get it
  22. $objSheet = $objPHPExcel->getActiveSheet();
  23. // rename the sheet
  24. $objSheet->setTitle('Raport');
  25.  
  26. // let's bold and size the header font and write the header
  27. // as you can see, we can specify a range of cells, like here: cells from A1 to A4
  28. $objSheet->getStyle('A1:D1')->getFont()->setBold(true)->setSize(12);
  29.  
  30. // write header
  31. $objSheet->getCell('A1')->setValue('Produkt');
  32. $objSheet->getCell('B1')->setValue('Ilość');
  33. $objSheet->getCell('C1')->setValue('Cena');
  34. $objSheet->getCell('D1')->setValue('Cena razem');
  35.  
  36. // we could get this data from database, but for simplicty, let's just write it
  37. $objSheet->getCell('A2')->setValue('Jabłko');
  38. $objSheet->getCell('B2')->setValue(10);
  39. $objSheet->getCell('C2')->setValue(5);
  40. $objSheet->getCell('D2')->setValue('=B2*C2');
  41.  
  42. $objSheet->getCell('A3')->setValue('Pomidor');
  43. $objSheet->getCell('B3')->setValue(6);
  44. $objSheet->getCell('C3')->setValue(3);
  45. $objSheet->getCell('D3')->setValue('=B3*C3');
  46.  
  47. $objSheet->getCell('A4')->setValue('Chleb');
  48. $objSheet->getCell('B4')->setValue(10);
  49. $objSheet->getCell('C4')->setValue(2.5);
  50. $objSheet->getCell('D4')->setValue('=B4*C4');
  51.  
  52. $objSheet->getCell('A5')->setValue('TOTAL');
  53. $objSheet->getCell('B5')->setValue('=SUM(B2:B4)');
  54. $objSheet->getCell('C5')->setValue('-');
  55. $objSheet->getCell('D5')->setValue('=SUM(D2:D4)');
  56.  
  57. // bold and resize the font of the last row
  58. $objSheet->getStyle('A5:D5')->getFont()->setBold(true)->setSize(12);
  59.  
  60. // set number and currency format to columns
  61. $objSheet->getStyle('B2:B5')->getNumberFormat()->setFormatCode($numberFormat);
  62. $objSheet->getStyle('C2:D5')->getNumberFormat()->setFormatCode($currencyFormat);
  63.  
  64. // create some borders
  65. // first, create the whole grid around the table
  66. $objSheet->getStyle('A1:D5')->getBorders()->
  67. getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  68. // create medium border around the table
  69. $objSheet->getStyle('A1:D5')->getBorders()->
  70. getOutline()->setBorderStyle(PHPExcel_Style_Border::BORDER_MEDIUM);
  71. // create a double border above total line
  72. $objSheet->getStyle('A5:D5')->getBorders()->
  73. getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_DOUBLE);
  74. // create a medium border on the header line
  75. $objSheet->getStyle('A1:D1')->getBorders()->
  76. getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_MEDIUM);
  77.  
  78. // autosize the columns
  79. $objSheet->getColumnDimension('A')->setAutoSize(true);
  80. $objSheet->getColumnDimension('B')->setAutoSize(true);
  81. $objSheet->getColumnDimension('C')->setAutoSize(true);
  82. $objSheet->getColumnDimension('D')->setAutoSize(true);
  83.  
  84. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  85. header('Content-Disposition: attachment;filename=plik.xlsx');
  86. header('Cache-Control: max-age=0');
  87. $objWriter->save('php://output');
  88.  
  89. ?>
  90.  
kapslokk
Przed otwarciem <?php masz spacje. Usuń je.
maniek74
nie mam spacji na początku pliku, napisałem że plik generuje się prawidłowo, problem jest jak chce żeby się utworzył w locie
kapslokk
Skopiowałem cały Twój kod, usunąłem te spacje i generuje się w locie bez problemu.
maniek74
Dzięki, faktycznie na pliku osobnym generuje się prawidłowo.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.