|
|
|
ถามเรื่อง Read PHP Excel แล้ว Insert เข้าฐานข้อมูลคะ |
|
|
|
|
|
|
|
ถามเรื่อง Read PHP Excel แล้ว Insert เข้าฐานข้อมูลคะ
ปัญหาตอนนี้ คือ มัน insert ได้ แต่ insert ข้ามแถว ทำให้ข้อมูลที่บันทึกลง Database ไม่ครบ ข้อมูล
3,4 ,7,8, หายไป เมื่อเปรียบกับไฟล์ Excel 1 ที่ import เข้าไป
//Read File Excel//
//Read File Excel//
if($typefile == "application/vnd.ms-excel" ){
$fileType = "Excel5";
}else if($typefile == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ){
$fileType = "Excel2007";
}
if($fileType !=""){
$inputFileType = PHPExcel_IOFactory::identify($fileupload1);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($fileupload1);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$objWorksheet = $objPHPExcel->getActiveSheet(0);
$maxRow = $objPHPExcel->getActiveSheet(0)->getHighestRow();
$maxCol = $objPHPExcel->getActiveSheet(0)->getHighestColumn();
$sheetCount = $objPHPExcel->getSheetCount();// THIS IS WHERE IT FAILED.
date_default_timezone_set("Asia/Bangkok");
$saveTimeZone = date_default_timezone_get();
$R_index = 1;
$rec_start = false;
$headingsArray = $objWorksheet->rangeToArray('A15:'.$maxCol.'15',null, true, true, true);
$outHeader = array("BusArea","CostCtr","Cust","CustName");
foreach($headingsArray[15] as $colKey => $valueCol){
$valCol = $valueCol;
if(!(in_array($valCol,$outHeader))){
$valCol = ($valCol !="" ? date("m-y",PHPExcel_Shared_Date::ExcelToPHP($valCol)) : "");
$arrHeadVal[] = $valCol;
}
$col_name[] = $valCol;
}
$i=1;
$A=1; $B=2; $C=3; $D=4; $E=5; $F=6; $G=7; $H=8; $I=9; $J=10; $K=11; $L=12; $M=13;
$N=14; $O=15; $P=16; $Q=17; $R=18; $S=19; $T=20; $U=21; $V=22; $W=23; $X=24; $Y=25; $Z=26;
$Index =0;
$startI =1;
$totalCol = count($col_name);
for($i= $A;$i<= $totalCol;$i++){
if($i >(26 *0)){ $preFix =''; $Index = 0; }
if($i >(26 *1)){ $preFix ='A'; $Index = 1; }
if($i >(26 *2)){ $preFix ='B'; $Index = 2; }
if($i >(26 *3)){ $preFix ='C'; $Index = 3; }
if($i >(26 *4)){ $preFix ='D'; $Index = 4; }
if($i ==(26 *0)+1){ $startI = 1; }
if($i ==(26 *1)+1){ $startI = 1; }
if($i ==(26 *2)+1){ $startI = 1; }
if($i ==(26 *3)+1){ $startI = 1; }
if($i ==(26 *4)+1){ $startI = 1; }
$arrCol_num[$preFix.chr(64+ $startI)]= $i;
$arrCol_char[$i] = $preFix.chr(64+ $startI);
$startI++;
}
$field_col = SetArrayColumn($arrCol_char ,$col_name);
$sheetCount = $objPHPExcel->getSheetCount(); // THIS IS WHERE IT FAILED.
date_default_timezone_set("Asia/Bangkok");
$saveTimeZone = date_default_timezone_get();
$R_index = 16;
$rec_start = false;
for($rowIndex=$R_index;$rowIndex<= $maxRow;$rowIndex++) {
if($objWorksheet->getCell($field_col['BusArea'].$rowIndex )->getCalculatedValue() !=""){
$busCode = trim($objWorksheet->getCell($field_col['BusArea'].$rowIndex )->getCalculatedValue());
//$busarea = trim($objWorksheet->getCell($field_col['BusAreaName'].$rowIndex )->getCalculatedValue());
$costCtr = trim($objWorksheet->getCell($field_col['CostCtr'].$rowIndex )->getCalculatedValue());
$custCode = trim($objWorksheet->getCell($field_col['Cust'].$rowIndex )->getCalculatedValue());
$custName = trim($objWorksheet->getCell($field_col['CustName'].$rowIndex )->getCalculatedValue());
$arrCust[$custCode]['busCode'] = $busCode;
//$arrCust[$custCode]['busarea'] = $busarea;
$arrCust[$custCode]['costCtr'] = $costCtr;
$arrCust[$custCode]['custCodeSap'] = $custCode;
$arrCust[$custCode]['custName'] = $custName;
foreach($arrHeadVal as $val){
if(trim($objWorksheet->getCell($field_col[$val].$rowIndex )->getCalculatedValue()) != ""){
$arrVal[$costCtr][$custCode][$val]['val'] = trim($objWorksheet->getCell($field_col[$val].$rowIndex )->getCalculatedValue());
}
}
}
$rowIndex++;
//var_dump($rowIndex);
//exit;
}
}//end if fileType
}//end cnt tmpFile
Code (PHP)
//insert to database//
$strFlag = 0;
$strValue = "";
//****Start foreach costCenter****//
foreach($arrVal as $rs1 => $key1){
$costCenter = $rs1;
//****Start foreach customerCode****//
foreach($key1 as $rs => $key){
$custCostCtr = $rs;
$busCode = $arrCust[$custCostCtr]['busCode'];
//$busarea = $arrCust[$custCostCtr]['busarea'];
$custName = $arrCust[$custCostCtr]['custName'];
$costCtr = $arrCust[$custCostCtr]['costCtr'];
//var_dump("costCtr : ".$costCtr."<br><br>");
//****Start foreach yearMonth****//
foreach($key as $rs2 => $key2){
$dataMonth = explode("-",$rs2);
$yearMonth = $dataMonth[1].$dataMonth[0];
$amt = $key2['val'];
if($amt > 0 ){
$chkDup = "select count(*) cnt from tb_sale
where sale_ym = '".$yearMonth."'
and costCtr_code = '".$costCenter."'
and sap_code = '".$custCostCtr."' ";
//$chkDup = "select count(*) cnt from tb_sale where sale_ym = '".$yearMonth."' and sap_code = '".$custCostCtr."'";
$dup = $conn->queryStrSQL($chkDup);
//echo $chkDup."<br>";
if($dup[0]['cnt'] == 0){
$strValue .= ($strValue != "" ? "," : "");
$strValue .= "
(
'".$yearMonth."'
,(select top 1 cust_id from tm_customerBid where cust_sapCode = '".$custCostCtr."' and cust_nameEn = '".$custName."' and status = 'A')
,'".$custCostCtr."'
,'".$custName."'
,(select com_id from tm_businessArea where bus_code = '".$busCode."' and status = 'A')
,'".$custCostCtr."'
,(select top 1 com_name from tm_businessArea where bus_code = '".$busCode."' and status = 'A')
,'".$custCostCtr."'
,(select bus_id from tm_businessArea where bus_code = '".$busCode."' and status = 'A')
,'".$busCode."'
,(select top 1 bus_name from tm_businessArea where bus_code = '".$busCode."' and status = 'A')
,(select top 1 costCtr_id from tm_costCenter where costCtr_code = '".$costCenter."' and status = 'A')
,'".$costCenter."'
,(select top 1 costCtr_name from tm_costCenter where costCtr_code = '".$costCenter."' and status = 'A')
,".$amt."
,'".$_SESSION['uName']."'
,getdate()
,'A'
)
";
$strFlag++;
//var_dump($strFlag);
}else{
//duplicate
$updateSale = "UPDATE tb_sale set amount = '".$amt."'
,updateUser = '".$_SESSION['uName']."'
,updateDatetime = getdate()
where costCtr_code = '".$costCtr."'
and bus_code = '".$busCode."'
and sale_ym = '".$yearMonth."' ";
$conn->queryStrSQL($updateSale);
//continue;
}
}
if($strValue !="" && $strFlag==10){
$strSQL = "INSERT INTO tb_sale";
$strSQL .= "(
sale_ym
,cus_id
,cus_code
,cus_name
,com_id
,com_code
,com_name
,sap_code
,bus_id
,bus_code
,bus_name
,costCtr_id
,costCtr_code
,costCtr_name
,amount
,createUser
,createDatetime
,status
)";
$strSQL .= "VALUES ";
$conn->queryStrSQL($strSQL.$strValue);
$strValue = "";
$strFlag = 0;
//echo "strFlag10 : ".$strSQL.$strValue."<br>";
}
}//Start foreach yearMonth
}//end customerCode
}// end foreach costCenter
if($strValue !=""){
$strSQL = "INSERT INTO tb_sale";
$strSQL .= "(
sale_ym
,cus_id
,cus_code
,cus_name
,com_id
,com_code
,com_name
,sap_code
,bus_id
,bus_code
,bus_name
,costCtr_id
,costCtr_code
,costCtr_name
,amount
,createUser
,createDatetime
,status
)";
$strSQL .= "VALUES ";
$conn->queryStrSQL($strSQL.$strValue);
//echo "!Empty strValue : ".$strSQL.$strValue."<br>";
}
$rsljson['status'] = 'ok';
$rsljson['bc'] = 'Upload file sale success.';
exit(json_encode($rsljson));
}
Excel
Database
Tag : PHP, Ms SQL Server 2016, Bootstrap Framework
|
ประวัติการแก้ไข 2018-10-11 16:39:14
|
|
|
|
|
Date :
2018-10-11 16:38:28 |
By :
nottpoo |
View :
781 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|