php読み出しexcelデータ処理
2126 ワード
/**
* PHPExcel
* @param file
* @return Mixed object/array
**/
import("Org.Util.PHPExcel");
import("Org.Util.IOFactory");
$inputFileName = dirname(__FILE__)."/excel.xlsx";
// Check prerequisites
if (!file_exists($inputFileName)) {
exit("not found {$inputFileName}.
");
}
$PHPExcel = \PHPExcel_IOFactory::load($inputFileName);
//
$sheet = $PHPExcel->getSheet(0);
//
$highestRow = $sheet->getHighestRow();
//
$highestColumm = $sheet->getHighestColumn();
//
$map = [
'A'=> ' ',
'B'=> ' ',
'C'=> ' ',
'D'=> ' ',
'E'=> ' ',
'F'=> ' ',
'G'=> ' '
];
//
$keywords = S('insert_keywords');
if( !$keywords )
{
// 1
for ($row = 1; $row <= $highestRow; $row++)
{
for ($column = 'A'; $column <= $highestColumm; $column++)
{
// A
if($row == 1 || $column == 'H')
{
continue;
}
$value = $sheet->getCell($column.$row)->getValue();
//
if($value instanceof \PHPExcel_RichText)
{
$value = $value->__toString();
}
$keywords[$row][$map[$column]] = trim($value);
// $encode = mb_detect_encoding($value, array("ASCII","UTF-8","GB2312","GBK","BIG5"));
// echo $encode, PHP_EOL, mb_convert_encoding("$value", "big5", "UTF-8"), die;
// $dataset[$row][$map[$column]] = iconv('ASCII', 'UTF-8',$value);
// echo $column, $row, ":", $sheet->getCell($column.$row)->getValue(), PHP_EOL;
}
}
// excel ,
if( $keywords )
{
S('insert_keywords', $keywords, 86400);
}
// $keywords
// code...
}