【问题标题】:PHPEXCEL to store entries in an array and displayPHPEXCEL 将条目存储在数组中并显示
【发布时间】:2012-03-10 14:34:24
【问题描述】:

这是我的代码的摘录,它显示了 excel 条目并存储到数组中。

逻辑是先检测哪个条目无效,标记下来,然后对于其他有效条目,检查是否重复,如果是,标记下来。

最后,再次扫描整个工作表,检索不在这两个列表中的所有条目。 (重复或无效)

它的问题是:

1)当我显示表格时,虽然它可以显示但它警告我“ 数据表警告:从第 0 行的数据源请求未知参数“0”

2) 存入数组时,只能存到第一行

所以,我想知道我的循环逻辑有什么错误吗?我是否使用 PHPEXCEL 以正确的方式读取电子表格?谢谢你。

$reader = PHPExcel_IOFactory::createReader($readerType);
$PHPExcel = $reader->load($file);
$sheet = $PHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn());

$pattern="/^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/";

for ($row = 1; $row <= $highestRow; $row++){
for ($head = 0; $head < $highestColumn; $head++){
$testMail = $sheet->getCellByColumnAndRow($head, $row)->getValue();
if (preg_match($pattern,$testMail))
$mailColumn=$head;
}}
if(!isset($mailColumn))
{die('No email column detected, please check your file and import again.');}


$invaild[] = NULL ;
$email[] = NULL ;
$duplicate[] = NULL ;

for ($row = 1; $row <= $highestRow; $row++) {
    for ($y = 0; $y < $highestColumn; $y++) {
    $val = $sheet->getCellByColumnAndRow($y, $row)->getValue();

    if ($y == $mailColumn && !preg_match($pattern,$val))
    {$invaild[]=$row;}
    elseif ($y == $mailColumn && in_array($val,$email))
    {$duplicate[]=$val;
    $duplicate[]=$row;}
    //elseif (!in_array($row,$duplicate) && !in_array($row,$invaild) )
    //{echo $val;}

    if ($y == $mailColumn)
    {$email[]=$val;
    $email=array_unique($email);}

  }
}
$invaild=array_unique($invaild);


foreach ($invaild as $c)
{echo $c;}
echo "<br>";
foreach ($duplicate as $d)
{echo $d;}

?>


<div id="stylized" class="view">
<h1><?echo $file.' Result';?></h1> 
<p>Import from spreadsheet files</p>
   <div id="container">
<table cellpadding="0" cellspacing="0" border="0" class="display" id="viewImport">
<thead>
<tr>

<?
for ($head = 0; $head < $highestColumn; $head++){
if ($head==$mailColumn)
echo "<th field='col'$head> Email address </th>";
else
echo "<th field='col'$head> Unname coloum $head </th>";
}
?>
</tr>
</thead>

<?
for ($row = 1; $row <= $highestRow; $row++) {
    echo "<tr>";
    for ($y = 0; $y < $highestColumn; $y++) {
        if (!in_array($row,$duplicate) && !in_array($row,$invaild)){
            $val = $sheet->getCellByColumnAndRow($y, $row)->getValue();
            echo "<td>";
            if (!$val)
            echo "-";
            else
            echo $val;
            echo "</td>";}
        }
    echo "</tr>";   
    }



?>

</table>
</div>


I work on this these day but still get this display error thank you

![enter image description here][1]

【问题讨论】:

  • 我找不到任何错误...
  • 我已经试过你的代码,它会输出类似这样的东西......
  • test.xlsx 结果 从电子表格文件导入 Unname coloum 0 Unname coloum 1 Unname coloum 2 Unname coloum 3 Unname coloum 4 Unname coloum 5 电子邮件地址 1 10072011_1 Project Report N/A ABC airuser kamal.joshi@mail .com 2 TEST 测试 Te TEE TEST joshi.kamal@mail.com

标签: php phpexcel


【解决方案1】:

这是我尝试过的使用你的逻辑的代码。我已经通过了 excel 文件。(test.xlsx)

<?php
    /** PHPExcel */
    include_once('PHPExcel.php');

    /** PHPExcel_Writer_Excel2007 */
    include_once('PHPExcel/Writer/Excel2007.php');

    include_once('PHPExcel/Reader/Excel2007.php');

    //$objPHPExcel = new PHPExcel();

    $file = 'test.xlsx';
    $readerType = 'Excel2007';

    $reader = PHPExcel_IOFactory::createReader($readerType);
$PHPExcel = $reader->load($file);
$sheet = $PHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn());

$pattern="/^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/";

for ($row = 1; $row <= $highestRow; $row++){
for ($head = 0; $head < $highestColumn; $head++){
$testMail = $sheet->getCellByColumnAndRow($head, $row)->getValue();
if (preg_match($pattern,$testMail))
$mailColumn=$head;
}}
if(!isset($mailColumn))
{die('No email column detected, please check your file and import again.');}


$invaild[] = NULL ;
$email[] = NULL ;
$duplicate[] = NULL ;

for ($row = 1; $row <= $highestRow; $row++) {
    for ($y = 0; $y < $highestColumn; $y++) {
    $val = $sheet->getCellByColumnAndRow($y, $row)->getValue();

    if ($y == $mailColumn && !preg_match($pattern,$val))
    {$invaild[]=$row;}
    elseif ($y == $mailColumn && in_array($val,$email))
    {$duplicate[]=$val;
    $duplicate[]=$row;}
    //elseif (!in_array($row,$duplicate) && !in_array($row,$invaild) )
    //{echo $val;}

    if ($y == $mailColumn)
    {$email[]=$val;
    $email=array_unique($email);}

  }
}
$invaild=array_unique($invaild);


/*foreach ($invaild as $c)
{echo $c;}
echo "<br>";
foreach ($duplicate as $d)
{echo $d;}*/

?>


<div id="stylized" class="view">
<h1><?php echo $file.' Result';?></h1> 
<p>Import from spreadsheet files</p>
   <div id="container">
<table cellpadding="0" cellspacing="0" border="0" class="display" id="viewImport">
<thead>
<tr>

<?php
for ($head = 0; $head < $highestColumn; $head++){
if ($head==$mailColumn)
echo "<th field='col' $head> Email address </th>";
else
echo "<th field='col' $head> Unname coloum $head </th>";
}
?>
</tr>
</thead>

<?php
for ($row = 1; $row <= $highestRow; $row++) {
            //Here I have added condition
    if (!in_array($row,$duplicate) && !in_array($row,$invaild)){
        echo "<tr>";
    }
    for ($y = 0; $y < $highestColumn; $y++) {
        if (!in_array($row,$duplicate) && !in_array($row,$invaild)){
            $val = $sheet->getCellByColumnAndRow($y, $row)->getValue();
            echo "<td>";
            if (!$val)
            echo "-";
            else
            echo $val;
            echo "</td>";}
        }
            //Here I have added condition
    if (!in_array($row,$duplicate) && !in_array($row,$invaild)){
        echo "</tr>";   
    }
    }



?>

</table>
</div>

这是输出的来源。

<div id="stylized" class="view">
<h1>test.xlsx Result</h1> 
<p>Import from spreadsheet files</p>
   <div id="container">
<table cellpadding="0" cellspacing="0" border="0" class="display" id="viewImport">
<thead>
<tr>

<th field='col' 0> Unname coloum 0 </th><th field='col' 1> Unname coloum 1 </th><th field='col' 2> Unname coloum 2 </th><th field='col' 3> Unname coloum 3 </th><th field='col' 4> Unname coloum 4 </th><th field='col' 5> Unname coloum 5 </th><th field='col' 6> Email address </th>  </tr>

</thead>

<tr><td>1</td><td>REP_10072011_1</td><td>Project </td><td>N/A</td><td>Me</td><td>TEST1</td><td>kamal.joshi@mail.com</td></tr><tr><td>2</td><td>TEST</td><td>Test</td><td>Te</td><td>TEE</td><td>TEST</td><td>joshi.kamal@mail.com</td></tr> 
</table>

</div>

好吧,我已经尝试过 dataTable jQuery 插件,并且它工作得很好.. 看看链接http://datatables.net/forums/discussion/1283/warning-unexpected-number-of-td-elements./p1

我的建议:请通过您的 jquery dataTables 插件集成。确保您在 excel 表的输出页面中包含所需的 jquery,并带有匹配的类或 id 选择器,例如

$(document).ready(function() {
    $('#viewImport').dataTable();
} );

编辑:检查一下...

<?php
    /** PHPExcel */
    include_once('PHPExcel.php');

    /** PHPExcel_Writer_Excel2007 */
    include_once('PHPExcel/Writer/Excel2007.php');

    include_once('PHPExcel/Reader/Excel2007.php');

    //$objPHPExcel = new PHPExcel();

    $file = 'test.xlsx';
    $readerType = 'Excel2007';

    $reader = PHPExcel_IOFactory::createReader($readerType);
$PHPExcel = $reader->load($file);
$sheet = $PHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn());

$pattern="/^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/";

for ($row = 1; $row <= $highestRow; $row++){
for ($head = 0; $head < $highestColumn; $head++){
$testMail = $sheet->getCellByColumnAndRow($head, $row)->getValue();
if (preg_match($pattern,$testMail))
$mailColumn=$head;
}}
if(!isset($mailColumn))
{die('No email column detected, please check your file and import again.');}


$invaild[] = NULL ;
$email[] = NULL ;
$duplicate[] = NULL ;

for ($row = 1; $row <= $highestRow; $row++) {
    for ($y = 0; $y < $highestColumn; $y++) {
    $val = $sheet->getCellByColumnAndRow($y, $row)->getValue();

    if ($y == $mailColumn && !preg_match($pattern,$val))
    {$invaild[]=$row;}
    elseif ($y == $mailColumn && in_array($val,$email))
    {$duplicate[]=$val;
    $duplicate[]=$row;}
    //elseif (!in_array($row,$duplicate) && !in_array($row,$invaild) )
    //{echo $val;}

    if ($y == $mailColumn)
    {$email[]=$val;
    $email=array_unique($email);}

  }
}
$invaild=array_unique($invaild);


/*foreach ($invaild as $c)
{echo $c;}
echo "<br>";
foreach ($duplicate as $d)
{echo $d;}*/

?>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.dataTables.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
            $('#viewImport').dataTable();
        } );
</script>
<div id="stylized" class="view">
<h1><?php echo $file.' Result';?></h1> 
<p>Import from spreadsheet files</p>
   <div id="container">
<table cellpadding="0" cellspacing="0" border="0" class="display" id="viewImport">
<thead>
<tr>

<?php
for ($head = 0; $head < $highestColumn; $head++){
if ($head==$mailColumn)
echo "<th field='col' $head> Email address </th>";
else
echo "<th field='col' $head> Unname coloum $head </th>";
}
?>
</tr>
</thead>

<?php
for ($row = 1; $row <= $highestRow; $row++) {
    if (!in_array($row,$duplicate) && !in_array($row,$invaild)){
        echo "<tr>";
    }
    for ($y = 0; $y < $highestColumn; $y++) {
        if (!in_array($row,$duplicate) && !in_array($row,$invaild)){
            $val = $sheet->getCellByColumnAndRow($y, $row)->getValue();
            echo "<td>";
            if (!$val)
            echo "-";
            else
            echo $val;
            echo "</td>";}
        }
    if (!in_array($row,$duplicate) && !in_array($row,$invaild)){
        echo "</tr>";   
    }
    }



?>

</table>
</div>

【讨论】:

  • 所以从你的图片来看,我猜你正在使用jquery的dataTable插件......让我试试,很快就会回复你......
  • 这里我附上了我的输出图像......即使我已经为你提供了链接。可能对你有帮助..
  • 抱歉,您能打印出数组结果吗?在我的追踪中,它不是从第二行开始捕获
  • 感谢您的帮助。由于赏金即将结束,您为此付出了巨大的努力。在我尝试解决一些问题后,我会发布这个问题。期待您的光临
  • 很高兴您更新了代码,您知道我可以在此插件中编辑列标题吗?正如你所看到的,目前我自动生成一个未命名的列+数字,如果用户可以直接编辑和保存一个新的列标题会很好
猜你喜欢
  • 2021-08-28
  • 1970-01-01
  • 2019-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-20
  • 2021-10-31
相关资源
最近更新 更多