PHP Excel Reader - 编写表格

时间:2013-05-22 06:53:50

标签: php excel

嘿伙计们,我是PHP新手,正在尝试自学这门语言。现在我正在尝试使用纯PHP实现一些东西,以便在浏览器上显示Excel文档。我正在使用PHP Excel Reader来阅读文档但不知何故它们没有显示。

虽然我可以进行自己的调试和研究,如果有错误信息告诉我哪条线路导致错误,但没有,我现在面临的问题是

  1. 表格未显示
  2. 没有任何来自apache的错误消息
  3. 我想要实现的目标:

    • 使用PHP Excel Reader在浏览器上以表格格式显示Excel电子表格
    • 检查.xls文档中的工作表数量,获取工作表名称并相应显示按钮数量
    • 按钮必须能够允许用户在一个文档的不同工作表之间进行遍历

    现在我可以获取代码来计算工作表数量并正确显示其名称和按钮,但仍然无法显示相应的表格。

    我已尝试过多种方法来解决基于文档的问题,但无济于事。

    以下是我目前的代码:

    使用example.php

        <?php
        error_reporting(E_ALL ^ E_NOTICE);
        require_once 'reader.php';
        $data = new Spreadsheet_Excel_Reader("test.xls");
        ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
        <script type="text/javascript" src="scripts/excel.js"></script>
        <style>
        table.excel {
                border-style:ridge;
                border-width:1;
                border-collapse:collapse;
                font-family:sans-serif;
                font-size:12px;
        }
        table.excel thead th, table.excel tbody th {
                background:#CCCCCC;
                border-style:ridge;
                border-width:1;
                text-align: center;
                vertical-align:bottom;
        }
        table.excel tbody th {
                text-align:center;
                width:20px;
        }
        table.excel tbody td {
                vertical-align:bottom;
        }
        .clearfloat {
                clear: both;
        }
        table.excel tbody td {
            padding: 0 3px;
                border: 1px solid #EEEEEE;
        }
        table.excel tname {
                font-family: Arial, Helvetica, sans-serif;
                font-size: 12px;
                font-weight: bold;
                color: #000000;
                padding-right: 10px;
                padding-left: 10px;
        }
        .sheetnames {
                font-family: Arial, Helvetica, sans-serif;
                font-size: 12px;
                font-weight: bold;
                text-transform: uppercase;
                color: #000000;
                padding-right: 20px;
                float: left;
                padding-bottom: 20px;
        }
        </style>
        </head>
    
        <body onLoad="changeSheet(0)">
        <form action="<?php echo $editFormAction; ?>" method="post" name="worksheet">
        <?php
        $i = -1;
        for($s=0; $s<count($data->sheets)-1; $s++) {
        $i++;
        ?>
        <div class="sheetnames">
        <input name="sheet" type="button" value="<?php echo $data->boundsheets[$i]['name'];?>" onClick="changeSheet(<?php echo $i ?>)">
        </div>
        <?php
    
    }
    ?>
    <div class="clearfloat"></div>
    <div id="test"></div>
    </form>
    </body>
    </html>
    

    excel.js

    // JavaScript Document
    var xmlHttp 
    
    function changeSheet(n)
    { 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
     {
     alert ("Browser does not support HTTP Request")
     return
     }
    var url="script pages/changeSheet.php"
    url=url+"?q="+n
    xmlHttp.onreadystatechange=stateChanged 
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
    }
    
    function stateChanged() 
    { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
     { 
     document.getElementById("test").innerHTML=xmlHttp.responseText 
     } 
    }
    
    function GetXmlHttpObject()
    {
    var xmlHttp=null;
    try
     {
     // Firefox, Opera 8.0+, Safari
     xmlHttp=new XMLHttpRequest();
     }
    catch (e)
     {
     //Internet Explorer
     try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
     catch (e)
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
     }
    return xmlHttp;
    }
    

    changeSheet.php

    <?php
    error_reporting(E_ALL ^ E_NOTICE);
    require_once 'reader.php';
    $data = new Spreadsheet_Excel_Reader("test.xls");
    
    $q = $_GET['q'];
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    <body>
    <?php
    echo $data->dump(true,false, $sheet=$q); 
    ?>
    </body>
    </html>
    

    附带的代码是PHP Excel Reader的库文件,我认为这不会导致问题。

    再一次,我感谢你们花时间阅读我的问题。出了什么问题?导致表格无法显示的原因是什么?

1 个答案:

答案 0 :(得分:0)

您的Excel_Reader版本看起来不支持“:: dump()”。

看起来你必须自己遍历表格,行和列。

尝试使用var_dump($ data)查看其中的内容。

相关问题