-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcel_db.php
140 lines (134 loc) · 5.45 KB
/
excel_db.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
<!DOCTYPE html>
<html>
<head>
<?php require 'db.php';
include '/classes/PHPExcel/IOFactory.php';
$user = $_COOKIE["user"];
$tableName = "";
$inputFileName = "";
if ($_POST["hf_download"] == "download") {
// Instantiate a new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set the active Excel worksheet to sheet 0
$objPHPExcel->setActiveSheetIndex(0);
// Initialise the Excel row number
$rowCount = 1;
DBLogin("a8823305_audio");
$result = DBQuery("SELECT * from ".$tableName);
//start of printing column names as names of MySQL fields
$column = 'A';
for ($i = 1; $i < mysql_num_fields($result); $i++)
{
$objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, mysql_field_name($result,$i));
$column++;
}
//end of adding column names
//start while loop to get data
$rowCount = 2;
while($row = mysql_fetch_row($result))
{
$column = 'A';
for($j=1; $j<mysql_num_fields($result);$j++)
{
if(!isset($row[$j]))
$value = NULL;
elseif ($row[$j] != "")
$value = strip_tags($row[$j]);
else
$value = "";
$objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, $value);
$column++;
}
$rowCount++;
}
DBC();
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$tableName.'".xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
}
else if ($_POST["hf_download"] == "" && $_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else if (sizeof($_FILES["file"]) > 0 && strpos($_FILES["file"]["name"], ".xls") != -1) {
$inputFileName = $_FILES["file"]["name"];
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);
}
if (isset($_POST["btn_upload"]) && sizeof($_FILES["file"]) > 0 && strpos($_FILES["file"]["name"], ".xls") != -1) {
DBLogin("a8823305_audio");
$SQL = "CREATE TABLE ".$workbookTitle;
$inputFileName = $_FILES["file"]["name"];
echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory to identify the format<br />';
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
// Set the active Excel worksheet to sheet 0
$objPHPExcel->setActiveSheetIndex(0);
// Initialise the Excel row number
$rowCount = 1;
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$colNames = "";
// Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){
// Read a row of data into an array
$rowData = $sheetData->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);
if ($row == 1) {
$colNames .= "(";
foreach ($rowData as $cell) {
$colNames .= $cell.",";
}
$colNames = substr($colNames,0,strlen($colNames)-1).")";
$SQL .= $colNames;
} else {
$SQL .= "INSERT INTO ".$tableName." ".$colNames." (";
foreach ($rowData as $cell) {
$SQL .= $cell.",";
}
$SQL .= substr($SQL,0,strlen($SQL)-1).")";
}
}
DBQuery($SQL);
DBC();
}
?>
<script type="text/javascript" src="js/jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#btn_download").on("click", function() {
$("#form1").submit();
});
$("#btn_upload").on("click", function() {
$("#form1").submit();
});
});
</script>
</head>
<body>
<?php
if (logged_in()) {
echo "Logged in as ".$user.". <a href=\"logout.php\">Logout</a><br>\n";
include 'menu_bar.php';
if ($error != "") {
echo "<span class='error'>".$error."</span><br>\n";
}
echo "<form id='form1' name='form1' action='excel_db.php' method='post'>\n";
echo "<table id='tbl_exceldb'>\n";
echo "<tr><th>download</th><th>upload</th></tr>\n";
echo "<tr><td><input type='button' id='btn_download' name='btn_download' value='Download' /></td>\n";
echo "<td><label for='file'>Filename:</label><input type='file' name='file' id='file'><input type='button' id='btn_upload' name='btn_upload' value='Upload' /></td></tr>\n";
echo "</table>\n";
echo "</form>\n";
} else {
header("Location: cms.php");
}
?>
</body>
</html>