Hi;
I've built a calendar for my group that is a bit easier to configure and create reports for our purposes than Outlook (which is the application supported by the overall institution). I am trying to build an Excel and/or .csv export file that will dump a file on the users desktop if they want to put their items from our calendar into their Outlook calendar. I'm real close, but Outlook b-tches about various things when I try to import the dump file.
Here is the code:
<?php
session_start();
header("Content-Type: application/vnd.ms-excel");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=outlookimport.xls");
$link = mysqli_connect("localhost", "user", "password", "database") or die("Could not connect : " . mysql_error());
?>
<html><body>
<?
$RATER=$_SESSION[LOGINARRAY][RATER];
$sql = "SELECT CONCAT(SCHL,':',SVC_GROUP,':',COMMENT) as Subject, DATE_FORMAT(ARRIVE, '%Y-%m-%d') AS 'Start Date', DATE_FORMAT(ARRIVE, '%l:%i:00 %p') AS 'Start Time', DATE_FORMAT(DEPART, '%Y-%m-%d') AS 'End Date', DATE_FORMAT(DEPART, '%l:%i:00 %p') AS 'End Time' FROM CALENDAR LEFT JOIN SCHOOLS ON SCHOOLS.SCHOOLID=CALENDAR.SCHOOLID LEFT JOIN SVC_CODES ON CALENDAR.SVC_CODE=SVC_CODES.SVC_CODE WHERE RATER='$RATER' and (SCHL !=''and SVC_GROUP !=''and COMMENT !='') ORDER BY ARRIVE";
$result = mysqli_query($link,$sql) or die("Query failed : " . mysql_error());
$row = 1;
echo "<table>";
//the MYSQL_ASSOC gets field names instead of numbers
while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {
if ($row == 1) {
echo "<tr>";
foreach ($line as $col_key => $col_value) {
echo "<th>$col_key</th>";
}
echo "</tr>";
}
//start a new row
echo "<tr>";
foreach ($line as $col_value) {
echo "<td>".$col_value."</td>";
}
echo "</tr>";
$row++;
}
//get the record count right
$row=$row-1;
echo "</table>";
?>
</body></html>
I've tried various version of headers and filenames like:
header("Content-Type: text/csv");
...
header("content-disposition: attachment;filename=outlookimport.csv");
If I save the downloaded file as an Excel (.xls) file the Outlook import calendar process complains that there are no named ranges, or that I don't have the right to open the file or that someone else has it open (no they don't, it's closed) blah blah blah. If the output file is saved as .csv, then Outlook can't find the column names and asks the user to map Field 2, Field 3, etc. etc to import fields which I know will be beyond the ken of most of my users.
Anything obvious that I'm missing in this code? Different suggestions for all that header stuff (that I don't really understand very well).
Thanks!