Plaatsen van gegevens in excel via php
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
55
56
57
58
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
<?php
session_start();
include "includes/Include_config.php";
include "../values.php";
//if the user has not logged in
if(!isLoggedIn())
{
header('Location: ../index.php');
die();
}
$result = mysql_query("SELECT code, codeiedereen from `inlogcodes` WHERE subdomein='".mysql_real_escape_string($_SESSION['domein'])."'");
$count = mysql_num_fields($result);
$header = "";
$data = "";
for ($i = 0; $i < $count; $i++){
$header .= mysql_field_name($result, $i)."\t";
}
while($row = mysql_fetch_row($result)){
$line = '';
foreach($row as $value){
if(!isset($value) || $value == ""){
$value = "\t";
}else{
# important to escape any quotes to preserve them in the data.
$value = str_replace('"', '""', $value);
# needed to encapsulate data in quotes because some data might be multi line.
# the good news is that numbers remain numbers in Excel even though quoted.
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
# this line is needed because returns embedded in the data have "\r"
# and this looks like a "box character" in Excel
$data = str_replace("\r", "", $data);
# Nice to let someone know that the search came up empty.
# Otherwise only the column name headers will be output to Excel.
if ($data == "") {
$data = "\nno matching records found\n";
}
# This line will stream the file to the user rather than spray it across the screen
header("Content-Type: application/vnd.ms-excel; name='excel'");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=inlogcodes.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $header."\n".$data;
?>
session_start();
include "includes/Include_config.php";
include "../values.php";
//if the user has not logged in
if(!isLoggedIn())
{
header('Location: ../index.php');
die();
}
$result = mysql_query("SELECT code, codeiedereen from `inlogcodes` WHERE subdomein='".mysql_real_escape_string($_SESSION['domein'])."'");
$count = mysql_num_fields($result);
$header = "";
$data = "";
for ($i = 0; $i < $count; $i++){
$header .= mysql_field_name($result, $i)."\t";
}
while($row = mysql_fetch_row($result)){
$line = '';
foreach($row as $value){
if(!isset($value) || $value == ""){
$value = "\t";
}else{
# important to escape any quotes to preserve them in the data.
$value = str_replace('"', '""', $value);
# needed to encapsulate data in quotes because some data might be multi line.
# the good news is that numbers remain numbers in Excel even though quoted.
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
# this line is needed because returns embedded in the data have "\r"
# and this looks like a "box character" in Excel
$data = str_replace("\r", "", $data);
# Nice to let someone know that the search came up empty.
# Otherwise only the column name headers will be output to Excel.
if ($data == "") {
$data = "\nno matching records found\n";
}
# This line will stream the file to the user rather than spray it across the screen
header("Content-Type: application/vnd.ms-excel; name='excel'");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=inlogcodes.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $header."\n".$data;
?>
http://php.net/fputcsv lijkt te willen omzeilen.
En die stuur je met de melding "het is toch een excelsheet hoor", naar de gebruiker.
Verder kan ik van de woordenbrij die de vraag lijken te vormen, geen chocola maken.
je script maakt niet zo zeer een excel file, als wel een csv-file (waarbij je PHP-eigen functies als En die stuur je met de melding "het is toch een excelsheet hoor", naar de gebruiker.
Verder kan ik van de woordenbrij die de vraag lijken te vormen, geen chocola maken.