使用PHP将整个Excel文件显示为表格。

huangapple go评论98阅读模式
英文:

Displaying the whole excel file as a table using PHP

问题

我有一个包含多个工作表的Excel文件。我想在使用PHP时将Excel文件中的数据显示为表格,而无需插入数据库。

英文:

I have a excel file with multiple sheets. I want to display the data in excel file as a table using PHP without inserting to database.

答案1

得分: 1

你可以使用simplexlsx PHP库来完成这个任务。

以下是该库的代码:

  1. <?php
  2. ini_set('error_reporting', E_ALL);
  3. ini_set('display_errors', true);
  4. require_once __DIR__.'/../src/SimpleXLSX.php';
  5. echo '<h1>Read several sheets</h1>';
  6. if ($xlsx = SimpleXLSX::parse('countries_and_population.xlsx')) {
  7. echo '<pre>'.print_r($xlsx->sheetNames(), true).'</pre>';
  8. echo '<table cellpadding="10">';
  9. echo '<tr><td valign="top">';
  10. // output worksheet 1
  11. $dim = $xlsx->dimension();
  12. $num_cols = $dim[0];
  13. $num_rows = $dim[1];
  14. echo '<h2>'.$xlsx->sheetName(0).'</h2>';
  15. echo '<table border=1>';
  16. foreach ($xlsx->rows(1) as $r) {
  17. echo '<tr>';
  18. for ($i = 0; $i < $num_cols; $i++) {
  19. echo '<td>' . (!empty($r[$i]) ? $r[$i] : '&nbsp;') . '</td>';
  20. }
  21. echo '</tr>';
  22. }
  23. echo '</table>';
  24. echo '</td><td valign="top">';
  25. // output worksheet 2
  26. $dim = $xlsx->dimension(2);
  27. $num_cols = $dim[0];
  28. $num_rows = $dim[1];
  29. echo '<h2>'.$xlsx->sheetName(1).'</h2>';
  30. echo '<table border=1>';
  31. foreach ($xlsx->rows(2) as $r) {
  32. echo '<tr>';
  33. for ($i = 0; $i < $num_cols; $i++) {
  34. echo '<td>' . (!empty($r[$i]) ? $r[$i] : '&nbsp;') . '</td>';
  35. }
  36. echo '</tr>';
  37. }
  38. echo '</table>';
  39. echo '</td></tr></table>';
  40. } else {
  41. echo SimpleXLSX::parseError();
  42. }
  43. ?>

你可以在上述代码中看到如何使用simplexlsx PHP库来读取Excel文件的内容并将其输出为HTML表格。

英文:

You can do this by using simplexlsx PHP Library

below is the library code

https://github.com/fahadpatel/simplexlxs.git

  1. &lt;?php
  2. ini_set(&#39;error_reporting&#39;, E_ALL);
  3. ini_set(&#39;display_errors&#39;, true);
  4. require_once __DIR__.&#39;/../src/SimpleXLSX.php&#39;;
  5. echo &#39;&lt;h1&gt;Read several sheets&lt;/h1&gt;&#39;;
  6. if ( $xlsx = SimpleXLSX::parse(&#39;countries_and_population.xlsx&#39;)) {
  7. echo &#39;&lt;pre&gt;&#39;.print_r( $xlsx-&gt;sheetNames(), true ).&#39;&lt;/pre&gt;&#39;;
  8. echo &#39;&lt;table cellpadding=&quot;10&quot;&gt;
  9. &lt;tr&gt;&lt;td valign=&quot;top&quot;&gt;&#39;;
  10. // output worsheet 1
  11. $dim = $xlsx-&gt;dimension();
  12. $num_cols = $dim[0];
  13. $num_rows = $dim[1];
  14. echo &#39;&lt;h2&gt;&#39;.$xlsx-&gt;sheetName(0).&#39;&lt;/h2&gt;&#39;;
  15. echo &#39;&lt;table border=1&gt;&#39;;
  16. foreach ( $xlsx-&gt;rows( 1 ) as $r ) {
  17. echo &#39;&lt;tr&gt;&#39;;
  18. for ( $i = 0; $i &lt; $num_cols; $i ++ ) {
  19. echo &#39;&lt;td&gt;&#39; . ( ! empty( $r[ $i ] ) ? $r[ $i ] : &#39;&amp;nbsp;&#39; ) . &#39;&lt;/td&gt;&#39;;
  20. }
  21. echo &#39;&lt;/tr&gt;&#39;;
  22. }
  23. echo &#39;&lt;/table&gt;&#39;;
  24. echo &#39;&lt;/td&gt;&lt;td valign=&quot;top&quot;&gt;&#39;;
  25. // output worsheet 2
  26. $dim = $xlsx-&gt;dimension( 2 );
  27. $num_cols = $dim[0];
  28. $num_rows = $dim[1];
  29. echo &#39;&lt;h2&gt;&#39;.$xlsx-&gt;sheetName(1).&#39;&lt;/h2&gt;&#39;;
  30. echo &#39;&lt;table border=1&gt;&#39;;
  31. foreach ( $xlsx-&gt;rows( 2 ) as $r ) {
  32. echo &#39;&lt;tr&gt;&#39;;
  33. for ( $i = 0; $i &lt; $num_cols; $i ++ ) {
  34. echo &#39;&lt;td&gt;&#39; . ( ! empty( $r[ $i ] ) ? $r[ $i ] : &#39;&amp;nbsp;&#39; ) . &#39;&lt;/td&gt;&#39;;
  35. }
  36. echo &#39;&lt;/tr&gt;&#39;;
  37. }
  38. echo &#39;&lt;/table&gt;&#39;;
  39. echo &#39;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&#39;;
  40. } else {
  41. echo SimpleXLSX::parseError();
  42. }

huangapple
  • 本文由 发表于 2020年1月6日 20:12:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定