我正在使用PHPExcel从Excel工作表中读取数据并存储在mysql表中,直到现在我都能够上传.xls以及.xlsx文件,并且在上传了xls之后,我得到了数据的表结构以下
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
现在我有一些问题,请向我建议优化方法
数据存入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工作表(即第一行)的
整个行中数组的
字段),则不应将该行添加到
即所需的数组应该看起来像
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操作将数据存储到我的数据库中。
注意:请不要引用手册(这确实非常糟糕)...只需告诉我方法名称即可。
更新
@Mark谢谢您的解决方案,它对我有很大帮助,但仍然存在一些问题
而第60行是
foreach($headingsArray as $columnKey => $columnHeading) {
$namedDataArray[$r][$columnHeading] = $dataRow[$row]$columnKey];
}
谢谢
最佳答案
要获取当前的事件表,请执行以下操作:
$sheetName = $objPHPExcel->getActiveSheet()->getTitle();
查看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, true);
$headingsArray = $headingsArray[1];
$r = -1;
$namedDataArray = array();
for ($row = 2; $row <= $highestRow; ++$row) {
$dataRow = $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,null, true, true, 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 />';
关于PHPExcel阅读器-需要帮助,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4792543/