Database-tabel naar excel
Code (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
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
<?php
$data = ""; //variable to hold the data
$header = ""; //variable to hold the header
$sql = "SELECT * FROM people;";
//echo "sql: ".$sql."<br />"; //debug
require_once('./inc/database.php');
if(!$connection = mysql_connect(HOST,UN,PASS)) { echo 'geen verbinding met database.<br /><br />'; }
elseif(!$db = mysql_select_db(DATABASE)) { echo 'geen verbinding met database.<br /><br />'; }
elseif(!$result = mysql_query($sql)) { echo 'fout: '.mysql_error().'<br /><br />'; }
else
{
$nr_of_fields = mysql_num_fields($result); //save the number of fields
//extract all the fieldnames from the table:
for ($i=0; $i<$nr_of_fields; $i++)
{
$header .= mysql_field_name($result, $i)."\t";
}
//extract all tyhe data from the table:
while($row = mysql_fetch_row($result))
{
$line = '';
foreach($row as $value)
{
if((!isset($value)) OR ($value == ""))
{
$value = "\t"; //no value
}
else
{
$value = str_replace('"', '""', $value); //preserve any quotes that are found
$value = '"'.$value.'"'."\t";
}
$line .= $value;
}#end of foreach $row
$data .= trim($line)."\n"; //remove any whitespace from $line, make every line appear on a new line
}$data = str_replace("\r","",$data); //replace carriage returns with nothing, they'd mess up the output
}
if($data == "")
{
$data = "\nGeen Gegevens Gevonden!\n"; //no data was found, inform the user
}
//make the excel-file:
header("Content-type: application/x-msdownload"); //make sure the file will be downloaded
header("Content-Disposition: attachment; filename=ledenbestand.xls"); //create the filename
header("pragma: no-cache");
header("Expires: 0");
print $header."\n".$data; //print the data for the file
?>
$data = ""; //variable to hold the data
$header = ""; //variable to hold the header
$sql = "SELECT * FROM people;";
//echo "sql: ".$sql."<br />"; //debug
require_once('./inc/database.php');
if(!$connection = mysql_connect(HOST,UN,PASS)) { echo 'geen verbinding met database.<br /><br />'; }
elseif(!$db = mysql_select_db(DATABASE)) { echo 'geen verbinding met database.<br /><br />'; }
elseif(!$result = mysql_query($sql)) { echo 'fout: '.mysql_error().'<br /><br />'; }
else
{
$nr_of_fields = mysql_num_fields($result); //save the number of fields
//extract all the fieldnames from the table:
for ($i=0; $i<$nr_of_fields; $i++)
{
$header .= mysql_field_name($result, $i)."\t";
}
//extract all tyhe data from the table:
while($row = mysql_fetch_row($result))
{
$line = '';
foreach($row as $value)
{
if((!isset($value)) OR ($value == ""))
{
$value = "\t"; //no value
}
else
{
$value = str_replace('"', '""', $value); //preserve any quotes that are found
$value = '"'.$value.'"'."\t";
}
$line .= $value;
}#end of foreach $row
$data .= trim($line)."\n"; //remove any whitespace from $line, make every line appear on a new line
}$data = str_replace("\r","",$data); //replace carriage returns with nothing, they'd mess up the output
}
if($data == "")
{
$data = "\nGeen Gegevens Gevonden!\n"; //no data was found, inform the user
}
//make the excel-file:
header("Content-type: application/x-msdownload"); //make sure the file will be downloaded
header("Content-Disposition: attachment; filename=ledenbestand.xls"); //create the filename
header("pragma: no-cache");
header("Expires: 0");
print $header."\n".$data; //print the data for the file
?>
in PHPmyadmin kun je ook exporteren als xls bestand
Het moet juist niet uit PHPmyadmin. Andere mensen die geen toegang hebben tot PHPmyadmin moeten ook een excel-formaat kunnen downloaden van de database.