For God so loved the world, that He gave His only begotten Son, that all who believe in Him should not perish but have everlasting life
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

714 lines
24 KiB

  1. # PHPExcel Developer Documentation
  2. ## Reading and writing to file
  3. As you already know from part REF _Ref191885438 \w \h 3.3 REF _Ref191885438 \h Readers and writers, reading and writing to a persisted storage is not possible using the base PHPExcel classes. For this purpose, PHPExcel provides readers and writers, which are implementations of PHPExcel_Writer_IReader and PHPExcel_Writer_IWriter.
  4. ### PHPExcel_IOFactory
  5. The PHPExcel API offers multiple methods to create a PHPExcel_Writer_IReader or PHPExcel_Writer_IWriter instance:
  6. Direct creation via PHPExcel_IOFactory. All examples underneath demonstrate the direct creation method. Note that you can also use the PHPExcel_IOFactory class to do this.
  7. #### Creating PHPExcel_Reader_IReader using PHPExcel_IOFactory
  8. There are 2 methods for reading in a file into PHPExcel: using automatic file type resolving or explicitly.
  9. Automatic file type resolving checks the different PHPExcel_Reader_IReader distributed with PHPExcel. If one of them can load the specified file name, the file is loaded using that PHPExcel_Reader_IReader. Explicit mode requires you to specify which PHPExcel_Reader_IReader should be used.
  10. You can create a PHPExcel_Reader_IReader instance using PHPExcel_IOFactory in automatic file type resolving mode using the following code sample:
  11. ```php
  12. $objPHPExcel = PHPExcel_IOFactory::load("05featuredemo.xlsx");
  13. ```
  14. A typical use of this feature is when you need to read files uploaded by your users, and you don’t know whether they are uploading xls or xlsx files.
  15. If you need to set some properties on the reader, (e.g. to only read data, see more about this later), then you may instead want to use this variant:
  16. ```php
  17. $objReader = PHPExcel_IOFactory::createReaderForFile("05featuredemo.xlsx");
  18. $objReader->setReadDataOnly(true);
  19. $objReader->load("05featuredemo.xlsx");
  20. ```
  21. You can create a PHPExcel_Reader_IReader instance using PHPExcel_IOFactory in explicit mode using the following code sample:
  22. ```php
  23. $objReader = PHPExcel_IOFactory::createReader("Excel2007");
  24. $objPHPExcel = $objReader->load("05featuredemo.xlsx");
  25. ```
  26. Note that automatic type resolving mode is slightly slower than explicit mode.
  27. #### Creating PHPExcel_Writer_IWriter using PHPExcel_IOFactory
  28. You can create a PHPExcel_Writer_Iwriter instance using PHPExcel_IOFactory:
  29. ```php
  30. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
  31. $objWriter->save("05featuredemo.xlsx");
  32. ```
  33. ### Excel 2007 (SpreadsheetML) file format
  34. Excel2007 file format is the main file format of PHPExcel. It allows outputting the in-memory spreadsheet to a .xlsx file.
  35. #### PHPExcel_Reader_Excel2007
  36. ##### Reading a spreadsheet
  37. You can read an .xlsx file using the following code:
  38. ```php
  39. $objReader = new PHPExcel_Reader_Excel2007();
  40. $objPHPExcel = $objReader->load("05featuredemo.xlsx");
  41. ```
  42. ##### Read data only
  43. You can set the option setReadDataOnly on the reader, to instruct the reader to ignore styling, data validation, … and just read cell data:
  44. ```php
  45. $objReader = new PHPExcel_Reader_Excel2007();
  46. $objReader->setReadDataOnly(true);
  47. $objPHPExcel = $objReader->load("05featuredemo.xlsx");
  48. ```
  49. ##### Read specific sheets only
  50. You can set the option setLoadSheetsOnly on the reader, to instruct the reader to only load the sheets with a given name:
  51. ```php
  52. $objReader = new PHPExcel_Reader_Excel2007();
  53. $objReader->setLoadSheetsOnly( array("Sheet 1", "My special sheet") );
  54. $objPHPExcel = $objReader->load("05featuredemo.xlsx");
  55. ```
  56. ##### Read specific cells only
  57. You can set the option setReadFilter on the reader, to instruct the reader to only load the cells which match a given rule. A read filter can be any class which implements PHPExcel_Reader_IReadFilter. By default, all cells are read using the PHPExcel_Reader_DefaultReadFilter.
  58. The following code will only read row 1 and rows 20 – 30 of any sheet in the Excel file:
  59. ```php
  60. class MyReadFilter implements PHPExcel_Reader_IReadFilter {
  61. public function readCell($column, $row, $worksheetName = '') {
  62. // Read title row and rows 20 - 30
  63. if ($row == 1 || ($row >= 20 && $row <= 30)) {
  64. return true;
  65. }
  66. return false;
  67. }
  68. }
  69. $objReader = new PHPExcel_Reader_Excel2007();
  70. $objReader->setReadFilter( new MyReadFilter() );
  71. $objPHPExcel = $objReader->load("06largescale.xlsx");
  72. ```
  73. #### PHPExcel_Writer_Excel2007
  74. ##### Writing a spreadsheet
  75. You can write an .xlsx file using the following code:
  76. ```php
  77. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  78. $objWriter->save("05featuredemo.xlsx");
  79. ```
  80. ##### Formula pre-calculation
  81. By default, this writer pre-calculates all formulas in the spreadsheet. This can be slow on large spreadsheets, and maybe even unwanted. You can however disable formula pre-calculation:
  82. ```php
  83. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  84. $objWriter->setPreCalculateFormulas(false);
  85. $objWriter->save("05featuredemo.xlsx");
  86. ```
  87. ##### Office 2003 compatibility pack
  88. Because of a bug in the Office2003 compatibility pack, there can be some small issues when opening Excel2007 spreadsheets (mostly related to formula calculation). You can enable Office2003 compatibility with the following code:
  89. ```
  90. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  91. $objWriter->setOffice2003Compatibility(true);
  92. $objWriter->save("05featuredemo.xlsx");
  93. ```
  94. __Office2003 compatibility should only be used when needed__
  95. Office2003 compatibility option should only be used when needed. This option disables several Office2007 file format options, resulting in a lower-featured Office2007 spreadsheet when this option is used.
  96. ### Excel 5 (BIFF) file format
  97. Excel5 file format is the old Excel file format, implemented in PHPExcel to provide a uniform manner to create both .xlsx and .xls files. It is basically a modified version of [PEAR Spreadsheet_Excel_Writer][21], although it has been extended and has fewer limitations and more features than the old PEAR library. This can read all BIFF versions that use OLE2: BIFF5 (introduced with office 95) through BIFF8, but cannot read earlier versions.
  98. Excel5 file format will not be developed any further, it just provides an additional file format for PHPExcel.
  99. __Excel5 (BIFF) limitations__
  100. Please note that BIFF file format has some limits regarding to styling cells and handling large spreadsheets via PHP.
  101. #### PHPExcel_Reader_Excel5
  102. ##### Reading a spreadsheet
  103. You can read an .xls file using the following code:
  104. ```php
  105. $objReader = new PHPExcel_Reader_Excel5();
  106. $objPHPExcel = $objReader->load("05featuredemo.xls");
  107. ```
  108. ##### Read data only
  109. You can set the option setReadDataOnly on the reader, to instruct the reader to ignore styling, data validation, … and just read cell data:
  110. ```php
  111. $objReader = new PHPExcel_Reader_Excel5();
  112. $objReader->setReadDataOnly(true);
  113. $objPHPExcel = $objReader->load("05featuredemo.xls");
  114. ```
  115. ##### Read specific sheets only
  116. You can set the option setLoadSheetsOnly on the reader, to instruct the reader to only load the sheets with a given name:
  117. ```php
  118. $objReader = new PHPExcel_Reader_Excel5();
  119. $objReader->setLoadSheetsOnly( array("Sheet 1", "My special sheet") );
  120. $objPHPExcel = $objReader->load("05featuredemo.xls");
  121. ```
  122. ##### Read specific cells only
  123. You can set the option setReadFilter on the reader, to instruct the reader to only load the cells which match a given rule. A read filter can be any class which implements PHPExcel_Reader_IReadFilter. By default, all cells are read using the PHPExcel_Reader_DefaultReadFilter.
  124. The following code will only read row 1 and rows 20 to 30 of any sheet in the Excel file:
  125. ```php
  126. class MyReadFilter implements PHPExcel_Reader_IReadFilter {
  127. public function readCell($column, $row, $worksheetName = '') {
  128. // Read title row and rows 20 - 30
  129. if ($row == 1 || ($row >= 20 && $row <= 30)) {
  130. return true;
  131. }
  132. return false;
  133. }
  134. }
  135. $objReader = new PHPExcel_Reader_Excel5();
  136. $objReader->setReadFilter( new MyReadFilter() );
  137. $objPHPExcel = $objReader->load("06largescale.xls");
  138. ```
  139. #### PHPExcel_Writer_Excel5
  140. ##### Writing a spreadsheet
  141. You can write an .xls file using the following code:
  142. ```php
  143. $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
  144. $objWriter->save("05featuredemo.xls");
  145. ```
  146. ### Excel 2003 XML file format
  147. Excel 2003 XML file format is a file format which can be used in older versions of Microsoft Excel.
  148. __Excel 2003 XML limitations__
  149. Please note that Excel 2003 XML format has some limits regarding to styling cells and handling large spreadsheets via PHP.
  150. #### PHPExcel_Reader_Excel2003XML
  151. ##### Reading a spreadsheet
  152. You can read an Excel 2003 .xml file using the following code:
  153. ```php
  154. $objReader = new PHPExcel_Reader_Excel2003XML();
  155. $objPHPExcel = $objReader->load("05featuredemo.xml");
  156. ```
  157. ##### Read specific cells only
  158. You can set the option setReadFilter on the reader, to instruct the reader to only load the cells which match a given rule. A read filter can be any class which implements PHPExcel_Reader_IReadFilter. By default, all cells are read using the PHPExcel_Reader_DefaultReadFilter.
  159. The following code will only read row 1 and rows 20 to 30 of any sheet in the Excel file:
  160. ```php
  161. class MyReadFilter implements PHPExcel_Reader_IReadFilter {
  162. public function readCell($column, $row, $worksheetName = '') {
  163. // Read title row and rows 20 - 30
  164. if ($row == 1 || ($row >= 20 && $row <= 30)) {
  165. return true;
  166. }
  167. return false;
  168. }
  169. }
  170. $objReader = new PHPExcel_Reader_Excel2003XML();
  171. $objReader->setReadFilter( new MyReadFilter() );
  172. $objPHPExcel = $objReader->load("06largescale.xml");
  173. ```
  174. ### Symbolic LinK (SYLK)
  175. Symbolic Link (SYLK) is a Microsoft file format typically used to exchange data between applications, specifically spreadsheets. SYLK files conventionally have a .slk suffix. Composed of only displayable ANSI characters, it can be easily created and processed by other applications, such as databases.
  176. __SYLK limitations__
  177. Please note that SYLK file format has some limits regarding to styling cells and handling large spreadsheets via PHP.
  178. #### PHPExcel_Reader_SYLK
  179. ##### Reading a spreadsheet
  180. You can read an .slk file using the following code:
  181. ```php
  182. $objReader = new PHPExcel_Reader_SYLK();
  183. $objPHPExcel = $objReader->load("05featuredemo.slk");
  184. ```
  185. ##### Read specific cells only
  186. You can set the option setReadFilter on the reader, to instruct the reader to only load the cells which match a given rule. A read filter can be any class which implements PHPExcel_Reader_IReadFilter. By default, all cells are read using the PHPExcel_Reader_DefaultReadFilter.
  187. The following code will only read row 1 and rows 20 to 30 of any sheet in the SYLK file:
  188. ```php
  189. class MyReadFilter implements PHPExcel_Reader_IReadFilter {
  190. public function readCell($column, $row, $worksheetName = '') {
  191. // Read title row and rows 20 - 30
  192. if ($row == 1 || ($row >= 20 && $row <= 30)) {
  193. return true;
  194. }
  195. return false;
  196. }
  197. }
  198. $objReader = new PHPExcel_Reader_SYLK();
  199. $objReader->setReadFilter( new MyReadFilter() );
  200. $objPHPExcel = $objReader->load("06largescale.slk");
  201. ```
  202. ### Open/Libre Office (.ods)
  203. Open Office or Libre Office .ods files are the standard file format for Open Office or Libre Office Calc files.
  204. #### PHPExcel_Reader_OOCalc
  205. ##### Reading a spreadsheet
  206. You can read an .ods file using the following code:
  207. ```php
  208. $objReader = new PHPExcel_Reader_OOCalc();
  209. $objPHPExcel = $objReader->load("05featuredemo.ods");
  210. ```
  211. ##### Read specific cells only
  212. You can set the option setReadFilter on the reader, to instruct the reader to only load the cells which match a given rule. A read filter can be any class which implements PHPExcel_Reader_IReadFilter. By default, all cells are read using the PHPExcel_Reader_DefaultReadFilter.
  213. The following code will only read row 1 and rows 20 to 30 of any sheet in the Calc file:
  214. ```php
  215. class MyReadFilter implements PHPExcel_Reader_IReadFilter {
  216. public function readCell($column, $row, $worksheetName = '') {
  217. // Read title row and rows 20 - 30
  218. if ($row == 1 || ($row >= 20 && $row <= 30)) {
  219. return true;
  220. }
  221. return false;
  222. }
  223. }
  224. $objReader = new PHPExcel_Reader_OOcalc();
  225. $objReader->setReadFilter( new MyReadFilter() );
  226. $objPHPExcel = $objReader->load("06largescale.ods");
  227. ```
  228. ### CSV (Comma Separated Values)
  229. CSV (Comma Separated Values) are often used as an import/export file format with other systems. PHPExcel allows reading and writing to CSV files.
  230. __CSV limitations__
  231. Please note that CSV file format has some limits regarding to styling cells, number formatting, ...
  232. #### PHPExcel_Reader_CSV
  233. ##### Reading a CSV file
  234. You can read a .csv file using the following code:
  235. ```php
  236. $objReader = new PHPExcel_Reader_CSV();
  237. $objPHPExcel = $objReader->load("sample.csv");
  238. ```
  239. ##### Setting CSV options
  240. Often, CSV files are not really “comma separated”, or use semicolon (;) as a separator. You can instruct PHPExcel_Reader_CSV some options before reading a CSV file.
  241. Note that PHPExcel_Reader_CSV by default assumes that the loaded CSV file is UTF-8 encoded. If you are reading CSV files that were created in Microsoft Office Excel the correct input encoding may rather be Windows-1252 (CP1252). Always make sure that the input encoding is set appropriately.
  242. ```php
  243. $objReader = new PHPExcel_Reader_CSV();
  244. $objReader->setInputEncoding('CP1252');
  245. $objReader->setDelimiter(';');
  246. $objReader->setEnclosure('');
  247. $objReader->setLineEnding("\r\n");
  248. $objReader->setSheetIndex(0);
  249. $objPHPExcel = $objReader->load("sample.csv");
  250. ```
  251. ##### Read a specific worksheet
  252. CSV files can only contain one worksheet. Therefore, you can specify which sheet to read from CSV:
  253. ```php
  254. $objReader->setSheetIndex(0);
  255. ```
  256. ##### Read into existing spreadsheet
  257. When working with CSV files, it might occur that you want to import CSV data into an existing PHPExcel object. The following code loads a CSV file into an existing $objPHPExcel containing some sheets, and imports onto the 6th sheet:
  258. ```php
  259. $objReader = new PHPExcel_Reader_CSV();
  260. $objReader->setDelimiter(';');
  261. $objReader->setEnclosure('');
  262. $objReader->setLineEnding("\r\n");
  263. $objReader->setSheetIndex(5);
  264. $objReader->loadIntoExisting("05featuredemo.csv", $objPHPExcel);
  265. ```
  266. #### PHPExcel_Writer_CSV
  267. ##### Writing a CSV file
  268. You can write a .csv file using the following code:
  269. ```php
  270. $objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
  271. $objWriter->save("05featuredemo.csv");
  272. ```
  273. ##### Setting CSV options
  274. Often, CSV files are not really “comma separated”, or use semicolon (;) as a separator. You can instruct PHPExcel_Writer_CSV some options before writing a CSV file:
  275. ```php
  276. $objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
  277. $objWriter->setDelimiter(';');
  278. $objWriter->setEnclosure('');
  279. $objWriter->setLineEnding("\r\n");
  280. $objWriter->setSheetIndex(0);
  281. $objWriter->save("05featuredemo.csv");
  282. ```
  283. ##### Write a specific worksheet
  284. CSV files can only contain one worksheet. Therefore, you can specify which sheet to write to CSV:
  285. ```php
  286. $objWriter->setSheetIndex(0);
  287. ```
  288. ##### Formula pre-calculation
  289. By default, this writer pre-calculates all formulas in the spreadsheet. This can be slow on large spreadsheets, and maybe even unwanted. You can however disable formula pre-calculation:
  290. ```php
  291. $objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
  292. $objWriter->setPreCalculateFormulas(false);
  293. $objWriter->save("05featuredemo.csv");
  294. ```
  295. ##### Writing UTF-8 CSV files
  296. A CSV file can be marked as UTF-8 by writing a BOM file header. This can be enabled by using the following code:
  297. ```php
  298. $objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
  299. $objWriter->setUseBOM(true);
  300. $objWriter->save("05featuredemo.csv");
  301. ```
  302. ##### Decimal and thousands separators
  303. If the worksheet you are exporting contains numbers with decimal or thousands separators then you should think about what characters you want to use for those before doing the export.
  304. By default PHPExcel looks up in the server's locale settings to decide what characters to use. But to avoid problems it is recommended to set the characters explicitly as shown below.
  305. English users will want to use this before doing the export:
  306. ```php
  307. PHPExcel_Shared_String::setDecimalSeparator('.');
  308. PHPExcel_Shared_String::setThousandsSeparator(',');
  309. ```
  310. German users will want to use the opposite values.
  311. ```php
  312. PHPExcel_Shared_String::setDecimalSeparator(',');
  313. PHPExcel_Shared_String::setThousandsSeparator('.');
  314. ```
  315. Note that the above code sets decimal and thousand separators as global options. This also affects how HTML and PDF is exported.
  316. ### HTML
  317. PHPExcel allows you to read or write a spreadsheet as HTML format, for quick representation of the data in it to anyone who does not have a spreadsheet application on their PC, or loading files saved by other scripts that simply create HTML markup and give it a .xls file extension.
  318. __HTML limitations__
  319. Please note that HTML file format has some limits regarding to styling cells, number formatting, ...
  320. #### PHPExcel_Reader_HTML
  321. ##### Reading a spreadsheet
  322. You can read an .html or .htm file using the following code:
  323. ```php
  324. $objReader = new PHPExcel_Reader_HTML();
  325. $objPHPExcel = $objReader->load("05featuredemo.html");
  326. ```
  327. __HTML limitations__
  328. Please note that HTML reader is still experimental and does not yet support merged cells or nested tables cleanly
  329. #### PHPExcel_Writer_HTML
  330. Please note that PHPExcel_Writer_HTML only outputs the first worksheet by default.
  331. ##### Writing a spreadsheet
  332. You can write a .htm file using the following code:
  333. ```php
  334. $objWriter = new PHPExcel_Writer_HTML($objPHPExcel);
  335. $objWriter->save("05featuredemo.htm");
  336. ```
  337. ##### Write all worksheets
  338. HTML files can contain one or more worksheets. If you want to write all sheets into a single HTML file, use the following code:
  339. ```php
  340. $objWriter->writeAllSheets();
  341. ```
  342. ##### Write a specific worksheet
  343. HTML files can contain one or more worksheets. Therefore, you can specify which sheet to write to HTML:
  344. ```php
  345. $objWriter->setSheetIndex(0);
  346. ```
  347. ##### Setting the images root of the HTML file
  348. There might be situations where you want to explicitly set the included images root. For example, one might want to see
  349. ```html
  350. <img style="position: relative; left: 0px; top: 0px; width: 140px; height: 78px;" src="http://www.domain.com/*images/logo.jpg" border="0">
  351. ```
  352. instead of
  353. ```html
  354. <img style="position: relative; left: 0px; top: 0px; width: 140px; height: 78px;" src="./images/logo.jpg" border="0">.
  355. ```
  356. You can use the following code to achieve this result:
  357. ```php
  358. $objWriter->setImagesRoot('http://www.example.com');
  359. ```
  360. ##### Formula pre-calculation
  361. By default, this writer pre-calculates all formulas in the spreadsheet. This can be slow on large spreadsheets, and maybe even unwanted. You can however disable formula pre-calculation:
  362. ```php
  363. $objWriter = new PHPExcel_Writer_HTML($objPHPExcel);
  364. $objWriter->setPreCalculateFormulas(false);
  365. $objWriter->save("05featuredemo.htm");
  366. ```
  367. ##### Embedding generated HTML in a web page
  368. There might be a situation where you want to embed the generated HTML in an existing website. PHPExcel_Writer_HTML provides support to generate only specific parts of the HTML code, which allows you to use these parts in your website.
  369. Supported methods:
  370. - generateHTMLHeader()
  371. - generateStyles()
  372. - generateSheetData()
  373. - generateHTMLFooter()
  374. Here's an example which retrieves all parts independently and merges them into a resulting HTML page:
  375. ```php
  376. <?php
  377. $objWriter = new PHPExcel_Writer_HTML($objPHPExcel);
  378. echo $objWriter->generateHTMLHeader();
  379. ?>
  380. <style>
  381. <!--
  382. html {
  383. font-family: Times New Roman;
  384. font-size: 9pt;
  385. background-color: white;
  386. }
  387. <?php
  388. echo $objWriter->generateStyles(false); // do not write <style> and </style>
  389. ?>
  390. -->
  391. </style>
  392. <?php
  393. echo $objWriter->generateSheetData();
  394. echo $objWriter->generateHTMLFooter();
  395. ?>
  396. ```
  397. ##### Writing UTF-8 HTML files
  398. A HTML file can be marked as UTF-8 by writing a BOM file header. This can be enabled by using the following code:
  399. ```php
  400. $objWriter = new PHPExcel_Writer_HTML($objPHPExcel);
  401. $objWriter->setUseBOM(true);
  402. $objWriter->save("05featuredemo.htm");
  403. ```
  404. ##### Decimal and thousands separators
  405. See section PHPExcel_Writer_CSV how to control the appearance of these.
  406. ### PDF
  407. PHPExcel allows you to write a spreadsheet into PDF format, for fast distribution of represented data.
  408. __PDF limitations__
  409. Please note that PDF file format has some limits regarding to styling cells, number formatting, ...
  410. #### PHPExcel_Writer_PDF
  411. PHPExcel’s PDF Writer is a wrapper for a 3rd-Party PDF Rendering library such as tcPDF, mPDF or DomPDF. Prior to version 1.7.8 of PHPExcel, the tcPDF library was bundled with PHPExcel; but from version 1.7.8 this was removed. Instead, you must now install a PDF Rendering library yourself; but PHPExcel will work with a number of different libraries.
  412. Currently, the following libraries are supported:
  413. Library | Version used for testing | Downloadable from | PHPExcel Internal Constant
  414. --------|--------------------------|----------------------------------|----------------------------
  415. tcPDF | 5.9 | http://www.tcpdf.org/ | PDF_RENDERER_TCPDF
  416. mPDF | 5.4 | http://www.mpdf1.com/mpdf/ | PDF_RENDERER_MPDF
  417. domPDF | 0.6.0 beta 3 | http://code.google.com/p/dompdf/ | PDF_RENDERER_DOMPDF
  418. The different libraries have different strengths and weaknesses. Some generate better formatted output than others, some are faster or use less memory than others, while some generate smaller .pdf files. It is the developers choice which one they wish to use, appropriate to their own circumstances.
  419. Before instantiating a Writer to generate PDF output, you will need to indicate which Rendering library you are using, and where it is located.
  420. ```php
  421. $rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
  422. $rendererLibrary = 'mPDF5.4';
  423. $rendererLibraryPath = dirname(__FILE__).'/../../../libraries/PDF/' . $rendererLibrary;
  424. if (!PHPExcel_Settings::setPdfRenderer(
  425. $rendererName,
  426. $rendererLibraryPath
  427. )) {
  428. die(
  429. 'Please set the $rendererName and $rendererLibraryPath values' .
  430. PHP_EOL .
  431. ' as appropriate for your directory structure'
  432. );
  433. }
  434. ```
  435. ##### Writing a spreadsheet
  436. Once you have identified the Renderer that you wish to use for PDF generation, you can write a .pdf file using the following code:
  437. ```php
  438. $objWriter = new PHPExcel_Writer_PDF($objPHPExcel);
  439. $objWriter->save("05featuredemo.pdf");
  440. ```
  441. Please note that PHPExcel_Writer_PDF only outputs the first worksheet by default.
  442. ##### Write all worksheets
  443. PDF files can contain one or more worksheets. If you want to write all sheets into a single PDF file, use the following code:
  444. ```php
  445. $objWriter->writeAllSheets();
  446. ```
  447. ##### Write a specific worksheet
  448. PDF files can contain one or more worksheets. Therefore, you can specify which sheet to write to PDF:
  449. ```php
  450. $objWriter->setSheetIndex(0);
  451. ```
  452. ##### Formula pre-calculation
  453. By default, this writer pre-calculates all formulas in the spreadsheet. This can be slow on large spreadsheets, and maybe even unwanted. You can however disable formula pre-calculation:
  454. ```php
  455. $objWriter = new PHPExcel_Writer_PDF($objPHPExcel);
  456. $objWriter->setPreCalculateFormulas(false);
  457. $objWriter->save("05featuredemo.pdf");
  458. ```
  459. ##### Decimal and thousands separators
  460. See section PHPExcel_Writer_CSV how to control the appearance of these.
  461. ### Generating Excel files from templates (read, modify, write)
  462. Readers and writers are the tools that allow you to generate Excel files from templates. This requires less coding effort than generating the Excel file from scratch, especially if your template has many styles, page setup properties, headers etc.
  463. Here is an example how to open a template file, fill in a couple of fields and save it again:
  464. ```php
  465. $objPHPexcel = PHPExcel_IOFactory::load('template.xlsx');
  466. $objWorksheet = $objPHPexcel->getActiveSheet();
  467. $objWorksheet->getCell('A1')->setValue('John');
  468. $objWorksheet->getCell('A2')->setValue('Smith');
  469. $objWriter = PHPExcel_IOFactory::createWriter($objPHPexcel, 'Excel5');
  470. $objWriter->save('write.xls');
  471. ```
  472. Notice that it is ok to load an xlsx file and generate an xls file.
  473. [21]: http://pear.php.net/package/Spreadsheet_Excel_Writer
  474. [22]: http://www.codeplex.com/PHPExcel/Wiki/View.aspx?title=Credits&referringTitle=Home