42_RichText.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Helper\Html as HtmlHelper;
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  4. require __DIR__ . '/../Header.php';
  5. // Create new Spreadsheet object
  6. $helper->log('Create new Spreadsheet object');
  7. $spreadsheet = new Spreadsheet();
  8. // Set document properties
  9. $helper->log('Set document properties');
  10. $spreadsheet->getProperties()->setCreator('Maarten Balliauw')
  11. ->setLastModifiedBy('Maarten Balliauw')
  12. ->setTitle('PhpSpreadsheet Test Document')
  13. ->setSubject('PhpSpreadsheet Test Document')
  14. ->setDescription('Test document for PhpSpreadsheet, generated using PHP classes.')
  15. ->setKeywords('office PhpSpreadsheet php')
  16. ->setCategory('Test result file');
  17. // Add some data
  18. $helper->log('Add some data');
  19. $html1 = '<font color="#0000ff">
  20. <h1 align="center">My very first example of rich text<br />generated from html markup</h1>
  21. <p>
  22. <font size="14" COLOR="rgb(0,255,128)">
  23. <b>This block</b> contains an <i>italicized</i> word;
  24. while this block uses an <u>underline</u>.
  25. </font>
  26. </p>
  27. <p align="right"><font size="9" color="red">
  28. I want to eat <ins><del>healthy food</del> <strong>pizza</strong></ins>.
  29. </font>
  30. ';
  31. $html2 = '<p>
  32. <font color="#ff0000">
  33. 100&deg;C is a hot temperature
  34. </font>
  35. <br>
  36. <font color="#0080ff">
  37. 10&deg;F is cold
  38. </font>
  39. </p>';
  40. $html3 = '2<sup>3</sup> equals 8';
  41. $html4 = 'H<sub>2</sub>SO<sub>4</sub> is the chemical formula for Sulphuric acid';
  42. $html5 = '<strong>bold</strong>, <em>italic</em>, <strong><em>bold+italic</em></strong>';
  43. $wizard = new HtmlHelper();
  44. $richText = $wizard->toRichTextObject($html1);
  45. $spreadsheet->getActiveSheet()
  46. ->setCellValue('A1', $richText);
  47. $spreadsheet->getActiveSheet()
  48. ->getColumnDimension('A')
  49. ->setWidth(48);
  50. $spreadsheet->getActiveSheet()
  51. ->getRowDimension(1)
  52. ->setRowHeight(-1);
  53. $spreadsheet->getActiveSheet()->getStyle('A1')
  54. ->getAlignment()
  55. ->setWrapText(true);
  56. $richText = $wizard->toRichTextObject($html2);
  57. $spreadsheet->getActiveSheet()
  58. ->setCellValue('A2', $richText);
  59. $spreadsheet->getActiveSheet()
  60. ->getRowDimension(1)
  61. ->setRowHeight(-1);
  62. $spreadsheet->getActiveSheet()
  63. ->getStyle('A2')
  64. ->getAlignment()
  65. ->setWrapText(true);
  66. $spreadsheet->setActiveSheetIndex(0)
  67. ->setCellValue('A3', $wizard->toRichTextObject($html3));
  68. $spreadsheet->setActiveSheetIndex(0)
  69. ->setCellValue('A4', $wizard->toRichTextObject($html4));
  70. $spreadsheet->setActiveSheetIndex(0)
  71. ->setCellValue('A5', $wizard->toRichTextObject($html5));
  72. // Rename worksheet
  73. $helper->log('Rename worksheet');
  74. $spreadsheet->getActiveSheet()
  75. ->setTitle('Rich Text Examples');
  76. // Save
  77. $helper->write($spreadsheet, __FILE__);