ESH/excel/index1.php

189 lines
5.1 KiB
PHP
Raw Permalink Normal View History

2024-10-23 18:28:06 +05:30
<?php
include ('config.php');
//$conn = mysqli_connect("localhost","root","","phpsamples");
require_once('vendor/php-excel-reader/excel_reader2.php');
require_once('vendor/SpreadsheetReader.php');
if (isset($_POST["import"]))
{
$allowedFileType = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
if(in_array($_FILES["file"]["type"],$allowedFileType)){
$targetPath = 'uploads/'.$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $targetPath);
$Reader = new SpreadsheetReader($targetPath);
$sheetCount = count($Reader->sheets());
for($i=0;$i<$sheetCount;$i++)
{
$Reader->ChangeSheet($i);
foreach ($Reader as $Row)
{
$code = "";
if(isset($Row[0])) {
$code = mysqli_real_escape_string($conn,$Row[0]);
}
$fname = "";
if(isset($Row[1])) {
$fname = mysqli_real_escape_string($conn,$Row[1]);
}
$lname = "";
if(isset($Row[2])) {
$lname = mysqli_real_escape_string($conn,$Row[2]);
}
$designation = "";
if(isset($Row[3])) {
$designation = mysqli_real_escape_string($conn,$Row[3]);
}
$father_name = "";
if(isset($Row[4])) {
$father_name = mysqli_real_escape_string($conn,$Row[4]);
}
$gender = "";
if(isset($Row[5])) {
$gender = mysqli_real_escape_string($conn,$Row[5]);
}
$blood_group = "";
if(isset($Row[6])) {
$blood_group = mysqli_real_escape_string($conn,$Row[6]);
}
$primary_phone = "";
if(isset($Row[7])) {
$primary_phone = mysqli_real_escape_string($conn,$Row[7]);
}
$is_first_aider = "";
if(isset($Row[8])) {
$is_first_aider = mysqli_real_escape_string($conn,$Row[8]);
}
if (!empty($code) || !empty($fname) ||!empty($lname) ||!empty($designation) ||!empty($father_name) ||!empty($gender) ||!empty($blood_group) ||!empty($primary_phone) ||!empty($is_first_aider) ) {
$query = "insert into employee(emp_code,fname,lname,designation_id,father_name,gender,blood_group,primary_phone,is_first_aid) values('".$code."','".$fname."','".$lname."','".$designation."','".$father_name."','".$gender."','".$blood_group."','".$primary_phone."','".$is_first_aider."')";
$result = mysqli_query($conn, $query);
if (! empty($result)) {
$type = "success";
$message = "Excel Data Imported into the Database";
} else {
$type = "error";
$message = "Problem in Importing Excel Data";
}
}
}
}
}
else
{
$type = "error";
$message = "Invalid File Type. Upload Excel File.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: Arial;
width: 550px;
}
.outer-container {
background: #F0F0F0;
border: #e0dfdf 1px solid;
padding: 40px 20px;
border-radius: 2px;
}
.btn-submit {
background: #333;
border: #1d1d1d 1px solid;
border-radius: 2px;
color: #f0f0f0;
cursor: pointer;
padding: 5px 20px;
font-size:0.9em;
}
.tutorial-table {
margin-top: 40px;
font-size: 0.8em;
border-collapse: collapse;
width: 100%;
}
.tutorial-table th {
background: #f0f0f0;
border-bottom: 1px solid #dddddd;
padding: 8px;
text-align: left;
}
.tutorial-table td {
background: #FFF;
border-bottom: 1px solid #dddddd;
padding: 8px;
text-align: left;
}
#response {
padding: 10px;
margin-top: 10px;
border-radius: 2px;
display:none;
}
.success {
background: #c7efd9;
border: #bbe2cd 1px solid;
}
.error {
background: #fbcfcf;
border: #f3c6c7 1px solid;
}
div#response.display-block {
display: block;
}
</style>
</head>
<body>
<div class="outer-container">
<form action="" method="post"
name="frmExcelImport" id="frmExcelImport" enctype="multipart/form-data">
<div>
<label>Choose Excel
File</label> <input type="file" name="file"
id="file" accept=".xls,.xlsx">
<button type="submit" id="submit" name="import"
class="btn-submit">Import</button>
</div>
</form>
</div>
<div id="response" class="<?php if(!empty($type)) { echo $type . " display-block"; } ?>"><?php if(!empty($message)) { echo $message; } ?></div>
</body>
</html>