name start_date end_date city one 11/25/2011 3:30:00 PM 11/29/2011 4:40:00 AM Jaipur two 10/22/2011 5:30:00 PM 10/25/2011 6:30:00 AM Kota three 3/10/2011 2:30:00 PM 3/11/2011 12:30:00 AM Bikaner chandigarh
现在我有一些问题,请建议我优化的方法
>我们如何获得工作表名称(bcoz在一个excel有7张)
>现在我做的是存储这些
数据到db,下面是代码片段
$inputFileName = "test.xls"; $inputFileType = PHPExcel_IOFactory::identify($inputFileName); $objReader = PHPExcel_IOFactory::createReader($inputFileType); $objReader->setReadDataOnly(true); /** Load $inputFileName to a PHPExcel Object **/ $objPHPExcel = $objReader->load($inputFileName); $total_sheets=$objPHPExcel->getSheetCount(); // here 4 $allSheetName=$objPHPExcel->getSheetNames(); // array ([0]=>'student',[1]=>'teacher',[2]=>'school',[3]=>'college') $objWorksheet = $objPHPExcel->setActiveSheetIndex(0); // first sheet $highestRow = $objWorksheet->getHighestRow(); // here 5 $highestColumn = $objWorksheet->getHighestColumn(); // here 'E' $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // here 5 for ($row = 1; $row <= $highestRow; ++$row) { for ($col = 0; $col <= $highestColumnIndex; ++$col) { $value=$objWorksheet->getCellByColumnAndRow($col,$row)->getValue(); if(is_array($arr_data) ) { $arr_data[$row-1][$col]=$value; } } } print_r($arr_data);
并返回
Array ( [0] => Array ( [0] => name [1] => start_date [2] => end_date [3] => city [4] => ) [1] => Array ( [0] => one [1] => 40568.645833333 [2] => 40570.5 [3] => Jaipur [4] => ) [2] => Array ( [0] => two [1] => 40568.645833333 [2] => 40570.5 [3] => Kota [4] => ) [3] => Array ( [0] => three [1] => 40568.645833333 [2] => 40570.5 [3] => Bikaner [4] => ) [4] => Array ( [0] => [1] => [2] => [3] => Chandigarh [4] => ) )
我需要它
>每个excel表(即第一行)的标题变为数组($arr_data)的关键字
> rest成为数组的值.
>时间更改为一些整数值,这与excel表中的相同
>所有行中的数组的空白字段(即blank头文件列)应该被截断(这里[4])
>如果Excel表格的第一个字段(或某些的组合条件)
字段)不满足,那么该行不应该添加到数组中
即所期望的数组应该看起来像
Array ( [0] => Array ( [name] => one [start_date] => 11/25/2011 3:30:00 PM [end_date] => 11/29/2011 4:40:00 AM [city] => Jaipur ) [1] => Array ( [name] => two [start_date] => 10/22/2011 5:30:00 PM [end_date] => 10/25/2011 6:30:00 AM [city] => Kota ) [2] => Array ( [name] => three [start_date] => 3/10/2011 2:30:00 PM [end_date] => 3/11/2011 12:30:00 AM [city] => Bikaner ) )
之后,我将数据存储到我的数据库中,使用所需数组上的MysqL操作.
>在db中存储上述数据还有其他简单的方法
更新
@Mark感谢您的解决方案,它帮助我很多,但仍然有一些问题
>当任何单元格为空时,如何处理excel sheet..bcoz中的空白单元格,然后显示通知
Notice: Undefined index: C in C:\xampp\htdocs\xls_reader\Tests\excel2007.PHP
on line 60
Notice: Undefined index: D in C:\xampp\htdocs\xls_reader\Tests\excel2007.PHP
on line 60
而第60行是
foreach($headingsArray as $columnKey => $columnHeading) { $namedDataArray[$r][$columnHeading] = $dataRow[$row]$columnKey]; }
>如何在检索完整数据数组之前设置条件,即如果我想要的是,如果第一列和第二列在任何行中为空/空白,则该行不应该添加到我们所需的数组中
谢谢
how do we get the sheet name ( bcoz in
one excel there are 7 sheets )?
获取当前活动表单:
$sheetName = $objPHPExcel->getActiveSheet()->getTitle();
time changed into some integer value,
that shoud be same as in excel sheet
查看PHPExcel_Shared_Date :: ExcelToPHP($excelDate)或PHPExcel_Shared_Date :: ExcelToPHPObject($excelDate)将日期/时间值转换为PHP时间戳记或DateTime对象
看看$objPHPExcel-> getActiveSheet() – > toArray()方法,而不是自己循环遍历所有的rwos和列.如果要使用toArray格式化参数,请不要使用$objReader-> setReadDataOnly(true);否则PHPExcel无法区分数字和日期/时间.最新的SVN代码在工作表对象中添加了一个rangeToArray()方法,它允许您一次读取一行(或单元格块),例如. $objPHPExcel-> getActiveSheet() – > rangeToArray( ‘A1:A4’)
其余的问题基本上是PHP数组操作
编辑
PS.而不是告诉我们手册真的非常糟糕…告诉我们如何改进它.
编辑2
使用最新的SVN代码来利用rangeToArray()方法:
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0); $highestRow = $objWorksheet->getHighestRow(); $highestColumn = $objWorksheet->getHighestColumn(); $headingsArray = $objWorksheet->rangeToArray('A1:'.$highestColumn.'1',null,true,true); $headingsArray = $headingsArray[1]; $r = -1; $namedDataArray = array(); for ($row = 2; $row <= $highestRow; ++$row) { $dataRow = $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,true); if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) { ++$r; foreach($headingsArray as $columnKey => $columnHeading) { $namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey]; } } } echo '<pre>'; var_dump($namedDataArray); echo '</pre><hr />';