You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

179 lines
5.8 KiB

<?php
ini_set('display_errors', 'on');
include "header.php";
require_once 'PHPExcel/PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$file_name = $_FILES["fileToUpload"]["name"];
$temp_file_name = $_FILES["fileToUpload"]["tmp_name"];
$target_dir = "excel-uploads/";
$target_file = strtolower($target_dir . iconv("UTF-8", "big5", basename($file_name)));
$EXTENSION = pathinfo($target_file, PATHINFO_EXTENSION);
if (move_uploaded_file($temp_file_name, $target_file)) {
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
if ($EXTENSION == 'xls')
$objReader = PHPExcel_IOFactory::createReader('Excel5');
// 讀取上傳到服務器的 excel
// 將讀取模式設置為僅讀取值,並保留格式
// 指標到第一個sheet
$objPHPExcel = $objReader->load($target_file);
$sheet = $objPHPExcel->getSheet(0);
// 取得所有資料列數
// 取得所有欄位行數
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
// 轉換 excel 欄位英文為數字
$highestColumn = PHPExcel_Cell::columnIndexFromString($highestColumn);
$excelArray = array();
for ($row = 1; $row <= $highestRow; $row++) {
// $val = $sheet->getCellByColumnAndRow(0, $row)->getCalculatedValue();
// 遇到整列空值跳出寫入陣列
$break_point = true;
for ($column = 1; $column < $highestColumn; $column++) {
$val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
if (!empty($val)) {
$break_point = false;
break;
}
}
if ($break_point)
break;
for ($column = 1; $column < $highestColumn; $column++) {
$val = $sheet->getCellByColumnAndRow($column, $row)->getValue();
// 讀取 EXCEL 資料到陣列
$excelArray[$row][] = $val;
}
}
for ($i = 1; $i < count($excelArray) + 1; $i++) {
print_r($excelArray[$i]);
echo "<br/><br/>";
// 寫入 account table
if ($i > 1) {
if (checkAccountStatus($link, $excelArray[$i])) {
addAccount($link, $user_id, $excelArray[$i]);
}
}
}
mysqli_close($link);
}
}
function checkContractStatus($link, $excelArray)
{
$contractno = $excelArray[0];
$sql = "
SELECT
*
FROM contract_new_order
WHERE 1=1
AND contractno = '$contractno';
";
$result = mysqli_query($link, $sql);
if ($result->num_rows > 0) {
return false;
} else {
return true;
}
}
// INSERT INTO `contract_new_order` (`id`, `contracttype`, `contractno`, `facilityno`, `company`, `taxid`, `address`, `tel`, `promiser`, `customerid`, `contractperson`, `contractaddress`, `contracttel`, `contractemail`, `contract_employee`, `order_date`, `all_amount_details`, `all_amount`, `facility_kind`, `weight`, `numberofpassenger`, `numberofstop`, `numberoffloor`, `opentype`, `speed`, `creater`, `create_at`) VALUES (NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '', '', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, CURRENT_TIMESTAMP)
function checkAccountStatus($link, $excelArray)
{
$accountid = $excelArray[0];
$sql = "
SELECT
*
FROM account
WHERE 1=1
AND accountid = '$accountid';
";
$result = mysqli_query($link, $sql);
if ($result->num_rows > 0) {
return false;
} else {
return true;
}
}
function nameToAccountid($link, $name)
{
$sql = "
SELECT
accountid
FROM account
WHERE accountid = '$name' ";
$res = mysqli_query($link, $sql);
while ($row = mysqli_fetch_row($res))
$name = $row[0];
mysqli_free_result($res);
return $name;
}
function addAccount($link, $user_id, $excelArray)
{
$accounttype = 'A';
$accountid = $excelArray[0];
$pwd = empty($excelArray[23]) ? $excelArray[24] : $excelArray[23];
$name = $excelArray[22];
$tel = $excelArray[24];
$address = $excelArray[12];
$email = $excelArray[25];
$repairerid = nameToAccountid($link, $excelArray[2]);
$creater = $user_id;
$create_at = date("Y-m-d H:i:s");
$sql = "
INSERT INTO `account` (
`accounttype`,
`accountid`,
`pwd`,
`name`,
`tel`,
`address`,
`email`,
`repairerid`,
`creater`,
`create_at`
) VALUES (
'$accounttype',
'$accountid',
'$pwd',
'$name',
'$tel',
'$address',
'$email',
'$repairerid',
'$creater',
'$create_at'
)
";
mysqli_query($link, $sql);
}
?>
<div class="container">
<form class="form-inline" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" enctype="multipart/form-data">
<div class="bottom-margin">資料
<label for="fileToUpload">上傳</label>
<div>
<input type="file" name="fileToUpload" id="fileToUpload">
</div>
<div class="bottom-margin">
<button type="submit" name="submit">確定</button>
</div>
</div>
<input type="hidden" name="token" value="<?php echo $_GET['token']; ?>">
</form>
</div>