-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexampleReader01.php
More file actions
136 lines (106 loc) · 2.94 KB
/
Copy pathexampleReader01.php
File metadata and controls
136 lines (106 loc) · 2.94 KB
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
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDBPDO";
$table = true;
error_reporting(E_ALL);
set_time_limit(0);
date_default_timezone_set('Europe/London');
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PHPExcel Reader Example #01</title>
</head>
<body>
<h1>PHPExcel Reader Example #01</h1>
<h2>Simple File Reader using PHPExcel_IOFactory::load()</h2>
<?php
/** PHPExcel_IOFactory */
include 'Classes/PHPExcel/IOFactory.php';
$inputFileName = './sampleData/file.xls';
echo 'Loading file ', pathinfo($inputFileName, PATHINFO_BASENAME) , ' using IOFactory to identify the format<br />';
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
echo '<hr />';
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
// var_dump($sheetData);
foreach($sheetData[2] as $key => $value)
{
$db[$key] = $key . $value;
}
$find = array(
"'s ",
" ",
"/"
);
$replace = "_";
$db = str_replace($find, $replace, $db);
$sql = "CREATE TABLE `mytable` ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, ";
foreach($db as $key => $dbs)
{
$sql.= "{$dbs} varchar(200) null,";
}
$sql.= "cs1 varchar(40) null";
$sql.= ");";
// create table
try
{
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// use exec() because no results are returned
if ($table)
{
$conn->exec($sql);
echo "Table created successfully";
}
}
catch(PDOException $e)
{
echo "<br />" . $e->getMessage();
}
// echo $sql;
// var_dump($db);
if (is_array($sheetData))
{
foreach($sheetData as $key1 => $value2)
{
if ($key1 == 1 || $key1 == 2) continue;
$field = "";
$fvalue = "";
$i = 0;
foreach($value2 as $key => $value)
{
$i++;
if ($value == '')
{
$value = 'NULL';
}
$find = array(
",",
"'"
);
$replace = "_";
$value = str_replace($find, $replace, $value);
$field.= $db[$key] . ",";
$fvalue.= "'" . $value . "',";
// echo $i.")".$fvalue."</br>";
}
$fvalue = rtrim($fvalue, ',');
$field = rtrim($field, ',');
// echo "<pre>";
// var_dump(explode(',', $fvalue));
// var_dump(explode(',', $field));
// echo "</pre>";
// die();
$query = "INSERT INTO mytable ($field) VALUES ($fvalue)";
// mysqli_query($conn, $query);
$conn->exec($query);
}
// var_dump($query);
}
$conn = null;
?>
<body>
</html>