-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimporta_pj.php
147 lines (106 loc) · 3.78 KB
/
importa_pj.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php include "header.php"; ?>
<body>
<?php //include "menu/me_inicio.php"; ?>
<?php
if(isset($_GET['p'])){
$p = $_GET['p'];
}else{
$p = 'inicio';
}
switch($p){
case 'inicio':
?>
<main class="col-sm-9 offset-sm-3 col-md-10 offset-md-2 pt-3">
<h1>Importar PJ do CulturAZ</h1>
<div>
<br />
<?php
require_once dirname(__FILE__) . '/classes/PHPExcel.php';
if(isset($_POST['enviar'])){
$pathToSave = 'upload/';
if( $_FILES['arquivo']['name'] != '' ){
$pre = date('Ymdhis')."_";
$data = date('Y-m-d H:i:s');
$arquivoTmp = $_FILES['arquivo']['tmp_name'];
$arquivo = $pathToSave.$pre.$_FILES['arquivo']['name'];
$arquivo_base = $_FILES['arquivo']['name'];
if(file_exists($arquivo))
{
echo "O arquivo ".$arquivo_base." já existe! Renomeie e tente novamente<br />";
}
else
{
if( !move_uploaded_file( $arquivoTmp, $arquivo ) )
{
$msg = 'Erro no upload do arquivo ';
}
else
{
$msg = 'Upload do arquivo foi um sucesso!';
}
}
}
echo $msg."<br />";
$fileName = $arquivo;
// detecta automaticamente o tipo de arruivo que será carregado
$excelReader = PHPExcel_IOFactory::createReaderForFile($fileName);
//Se não precisarmos de formatação
$excelReader->setReadDataOnly();
//carregar apenas algumas abas
//$loadSheets = array('aba1', 'aba2');
//$excelReader->setLoadSheetsOnly($loadSheets);
//o comportamente padrão é carregar todas as abas
$excelReader->setLoadAllSheets();
$excelObj = $excelReader->load($fileName);
//$excelObj->getActiveSheet()->toArray(null, true,true,true);
$sheet = $excelObj->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
//echo "Linhas: ".$highestRow;
$matriz = array();
// Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);
// Gera os índices
if($row == 1){
$indice = $rowData[0];
}else{
for($k = 0; $k < count($rowData[0]); $k++){
$matriz[$indice[$k]] = $rowData[0][$k];
}
$RazaoSocial = $matriz["Instituição responsável - Nome completo ou Razão Social"];
$CNPJ = $matriz["Instituição responsável - CPF ou CNPJ"];
$CEP = $matriz["Instituição responsável - CEP"];
$Numero = $matriz["Instituição responsável - Número"];
$Complemento = $matriz["Instituição responsável - Complemento"];
$Telefone1 = $matriz["Instituição responsável - Telefone 1"];
$Telefone2 = $matriz["Instituição responsável - Telefone 2"];
$Telefone3 = $matriz["Instituição responsável - Email Privado"];
$sql_ins = "INSERT INTO `sc_pj` (`Id_PessoaJuridica`, `RazaoSocial`, `CNPJ`, `CEP`, `Numero`, `Complemento`, `Telefone1`, `Telefone2`, `Telefone3`, `IdUsuario`) VALUES (NULL, '$RazaoSocial', '$CNPJ', '$CEP', '$Numero', '$Complemento', '$Telefone1', '$Telefone2', '$Telefone3', '1')";
$x = $wpdb->query($sql_ins);
}
}
}else{ // formulário para importação do arquivo
?>
<div class = "center">
<form method='POST' enctype='multipart/form-data' action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
<p><input type='file' name='arquivo'></p>
<br>
<input type='submit' value='Enviar' name='enviar'>
</form>
</div>
<?php
}
?>
</div>
<?php
break;
?>
<?php } //fim da switch ?>
<?php
include "footer.php";
?>