<form action='index.php' method='post' enctype='multipart/form-data'> <INPUT type='file' name='importfile'> <INPUT type='hidden' name='op' value='import_excel'> <INPUT type='submit' value='匯入'> </form>
include_once '../class/PHPExcel/IOFactory.php'; $reader = PHPExcel_IOFactory::createReader('Excel5'); $PHPExcel = $reader->load( $_FILES['importfile']['tmp_name'] ); // 檔案名稱 $sheet = $PHPExcel->getSheet(0); // 讀取第一個工作表(編號從 0 開始) $highestRow = $sheet->getHighestRow(); // 取得總列數 // 一次讀取一列 for ($row = 1; $row <= $highestRow; $row++) { $v=""; //讀取一列中的每一格 for ($col = 0; $col <= 8; $col++) { //格式檢查 if( PHPExcel_Shared_Date::isDateTime( $sheet->getCellByColumnAndRow($col , $row ) )){ $val = PHPExcel_Shared_Date::ExcelToPHPObject( $sheet->getCellByColumnAndRow( $col , $row )->getValue())->format('Y-m-d'); }else{ $val = $sheet->getCellByColumnAndRow($col, $row)->getCalculatedValue(); } if(!get_magic_quotes_runtime()) { $v[$col]=addSlashes($val); } } $sql = "insert into `".$xoopsDB->prefix("contact")."` (`gsn` , `name` , `tel` , `email` , `birthday` , `zip` , `county` , `city` , `addr`) values('{$v[0]}' , '{$v[1]}' , '{$v[2]}' , '{$v[3]}' , '{$v[4]}' , '{$v[5]}' , '{$v[6]}' , '{$v[7]}' , '{$v[8]}')"; $xoopsDB->queryF($sql); }
$objActSheet->getStyle('A1:I1')->getAlignment() ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER) ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER) ->setWrapText(true);
對齊的值有以下這些: HORIZONTAL_CENTER、 HORIZONTAL_CENTER_CONTINUOUS、 HORIZONTAL_GENERAL、 HORIZONTAL_JUSTIFY、 HORIZONTAL_LEFT、 HORIZONTAL_RIGHT、 VERTICAL_BOTTOM、 VERTICAL_CENTER、 VERTICAL_JUSTIFY、 VERTICAL_TOP
$objBorder=$objActSheet->getDefaultStyle()->getBorders(); $objBorder->getBottom() ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN) ->getColor()->setRGB('000000');
function num2alpha($n){ for($r = ""; $n >= 0; $n = intval($n / 26) - 1) $r = chr($n%26 + 0x41) . $r; return $r; }
$objActSheet->setCellValueByColumnAndRow($第N欄, $第N列 ,$內容);