ASKER
ASKER
ASKER
$top = file_get_contents('top.php');
$bottom = file_get_contents('bottom.php');
$rows = '';
// get your data here
// assume a mysqli object connection
while ($row = $conn->fetch_object()) {
// dump your row here
$rows .= '<Row>';
$rows .= '<Cell><Data ss:Type="String">{$row['value1']}</Data></Cell>';
$rows .= '<Cell><Data ss:Type="String">{$row['value2']}</Data></Cell>';
...
$rows .= '</Row>';
}
$output = $top . $rows . $bottom;
$filesize = strlen($output);
$filename = 'set_your_name_here.xml');
header('Content-type: application/vnd.ms-excel');
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-Length: " . $filesize);
header("Content-Disposition: inline; filename=".$filename);
echo $output;
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
ASKER
ASKER
ASKER
ASKER
<Table ss:ExpandedColumnCount="4" ss:ExpandedRowCount="4" x:FullColumns="1"
x:FullRows="1" ss:DefaultRowHeight="15">
Remove the RowCount and ColumnCount attributes OR include them in your body section and set the values to the number of rows / columns you will output - I find it easier to leave off<?php
$data = array(
array('id' => 1, 'date' => '1980-03-29', 'email' => 'john@somewhere.com', 'name' => 'John Smith'),
array('id' => 1, 'date' => '1991-11-07', 'email' => 'mary@somewhere.com', 'name' => 'Mary Jones'),
array('id' => 1, 'date' => '1970-12-19', 'email' => 'fred@somewhere.com', 'name' => 'Fred Black'),
array('id' => 1, 'date' => '1975-06-03', 'email' => 'sam@somewhere.com' , 'name' => 'Sam White')
);
$output = file_get_contents('t2987-top.xml');
foreach($data as $item) {
$output .= <<< ROW
<Row>
<Cell><Data ss:Type="Number">{$item['id']}</Data></Cell>
<Cell ss:StyleID="s62"><Data ss:Type="DateTime">{$item['date']}</Data></Cell>
<Cell><Data ss:Type="String">{$item['email']}</Data></Cell>
<Cell><Data ss:Type="String">{$item['name']}</Data></Cell>
</Row>
ROW;
}
$output .= file_get_contents('t2987-bottom.xml');
$filename = 'sample-xml-spreadsheet' . date('Y-m-d') . '.xml';
$filesize = strlen($output);
header('Content-type: application/vnd.ms-excel');
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-Length: " . $filesize);
header("Content-Disposition: inline; filename=".$filename);
echo $output;
Working sample hereASKER
ASKER
ASKER
ASKER
ASKER
<?php
define('SAVE_LOCATION','urpts/');
function date_conv($x) {
$d = explode("-", substr($x,0,10));
$r = $d[1] . "/" . $d[2] . "/" . $d[0] . " " . substr($x,11,5);
return $r;
}
session_start();
include "db_connect_nb.php";
// get prior saturday
$psat = "2018-02-17 00:00:00";
$currfri = "2018-02-23 17:00:00";
$qry = "Select * from cvisitor where ruid <> 0 and last_time >= '" . $psat . "' and last_time <= '" . $currfri . "' order by last_time";
$res = mysqli_query($link, $qry);
// get registered user info
$ncv = mysqli_num_rows($res);
//echo "cvqry = " . $qry . "<br>";
//echo "ncv = " . $ncv . "<br>";
$output = file_get_contents('urpts-top.xml');
for ($i = 0; $i < $ncv; $i++) {
$cv = mysqli_fetch_array($res,MYSQLI_ASSOC);
$lakos = strpos($cv['email'], "@lakos.com");
if (! $lakos) {
// get options / addons str
$oastr = "";
if ($cv['pepump'] == "Y") {
$oastr = "PE Pump";
}
if ($cv['pumprepair'] == "Y") {
if ($oastr == "") {
$oastr = "Pump Repair";
} else {
$oastr = $oastr . ", " . "Pump Repair";
}
}
if ($cv['valve_kit'] != "") {
if ($oastr == "") {
$oastr = $cv['valve_kit'] . "valve kit";
} else {
$oastr = $oastr . ", " . $cv['valve_kit'] . "valve kit";
}
}
if ($cv['dec_contact'] == "Y") {
if ($oastr == "") {
$oastr = "DEC Contact";
} else {
$oastr = $oastr . ", " . "DEC Contact";
}
}
if ($cv['multi'] == "Y") {
if ($oastr == "") {
$oastr = "Multi Tower";
} else {
$oastr = $oastr . ", " . "Multi Tower";
}
}
if ($cv['replbags'] != 0) {
if ($oastr == "") {
$oastr = $cv['replbags'] . " repl bags";
} else {
$oastr = $oastr . ", " . $cv['replbags'] . " repl bags";
}
}
if ($cv['strainer'] != "") {
if ($oastr == "") {
$oastr = "Strainer";
} else {
$oastr = $oastr . ", " . "Strainer";
}
}
// full stream opts, if any
if ($cv['cltype'] == "F") {
$qryfso = "SELECT * from fs_opts where vid = " . $cv['vid'];
$resfso = mysqli_query($link, $qryfso);
$nfso = mysqli_num_rows($resfso);
if ($nfso != 0) {
$fso = mysqli_fetch_array($resfso,MYSQLI_ASSOC);
if ($fso['lowprosep'] == "Y") {
if ($oastr == "") {
$oastr = "Low Pro Separator";
} else {
$oastr = $oastr . ", " . "Low Pro Separator";
}
}
if ($fso['SRI'] == "Y") {
if ($oastr == "") {
$oastr = "SRI";
} else {
$oastr = $oastr . ", " . "SRI";
}
}
if ($fso['PGK'] == "Y") {
if ($oastr == "") {
$oastr = "Pressure Gauge Kit";
} else {
$oastr = $oastr . ", " . "Pressure Gauge Kit";
}
}
if ($fso['WOF'] == "Y") {
if ($oastr == "") {
$oastr = "Weld on Flanges";
} else {
$oastr = $oastr . ", " . "Weld on Flanges";
}
}
if ($fso['Fspool'] == "Y") {
if ($oastr == "") {
$oastr = "Flanged Spools";
} else {
$oastr = $oastr . ", " . "Flanged Spools";
}
}
if ($fso['GCS'] == "Y") {
if ($oastr == "") {
$oastr = "Grooved Coupling Spools";
} else {
$oastr = $oastr . ", " . "Grooved Coupling Spools";
}
}
if ($fso['GRCoup'] == "Y") {
if ($oastr == "") {
$oastr = "Grooved Couplings";
} else {
$oastr = $oastr . ", " . "Grooved Couplings";
}
}
}
}
// b_hydro & sand_opts
if ($_SESSION['group'] == "sand") {
$qryso = "SELECT * from sand_opts where vid = " . $_SESSION['vid'];
$resso = mysqli_query($link, $qryso);
$nso = mysqli_num_rows($resso);
if ($nso != 0) {
$so = mysqli_fetch_array($resso,MYSQLI_ASSOC);
if ($so['bkwash'] != "") {
if ($oastr == "") {
$oastr = "Backwash: " . $so['bkwash'];
} else {
$oastr = $oastr . ", " . "Backwash: " . $so['bkwash'];
}
}
if ($so['material'] != "") {
if ($so['material'] == "C") {
$matl = "Carbon Steel";
} else {
$matl = "Stainless Steel";
}
if ($oastr == "") {
$oastr = "Material: " . $matl;
} else {
$oastr = $oastr . ", " . "Material: " . $matl;
}
}
if ($so['bkwhtank'] != "") {
if ($oastr == "") {
$oastr = $so['bkwhtank'];
} else {
$oastr = $oastr . ", " . $so['bkwhtank'];
}
}
if ($so['hlctrls'] != "") {
if ($oastr == "") {
$oastr = $so['hlctrls'];
} else {
$oastr = $oastr . ", " . $so['hlctrls'];
}
}
}
}
$ruid = $cv['ruid'];
$qryru = "SELECT * from registered_users where ix = " . $ruid;
$resru = mysqli_query($link, $qryru);
$ru = mysqli_fetch_array($resru,MYSQLI_ASSOC);
if ($ru['state'] == "") {
$stctry = $ru['country'];
} else {
$stctry = $ru['state'] . "/" . $ru['country'];
}
$dttime = date_conv(substr($cv['last_time'],0,10)) . substr($cv['last_time'],11,8);
$namestr = $ru['firstname'] . " " . $ru['lastname'];
$affil = $ru['affilliation'];
$projname = $cv['proj_name'];
$model = $cv['model'];
$price = "$" . $number_format($cv['net_price'],2);
$purge_meth = $cv['purge_method'];
$voltage = $cv['voltage'];
$output .= <<< ROW
<Row>
<Cell><Data ss:Type="String">$namestr</Data></Cell>
<Cell><Data ss:Type="String">$affil</Data></Cell>
<Cell><Data ss:Type="String">$stctry</Data></Cell>
<Cell><Data ss:Type="String">$dttime</Data></Cell>
<Cell><Data ss:Type="String">$projname</Data></Cell>
<Cell><Data ss:Type="String">$model</Data></Cell>
<Cell><Data ss:Type="String">$price</Data></Cell>
<Cell><Data ss:Type="String">$purge_meth</Data></Cell>
<Cell><Data ss:Type="String">$voltage</Data></Cell>
<Cell><Data ss:Type="String">$oastr</Data></Cell>
</Row>
ROW;
}
}
$output .= file_get_contents('urpts-bottom.xml');
$filename = 'sample-xml-spreadsheet' . date('Y-m-d') . '.xml';
$fullpath = SAVE_LOCATION . $filename;
echo "fullpath = " . $fullpath . "<br>";
file_put_contents($fullpath, $output);
<?php
define('SAVE_LOCATION','urpts/');
function date_conv($x) {
$d = explode("-", substr($x,0,10));
$r = $d[1] . "/" . $d[2] . "/" . $d[0] . " " . substr($x,11,5);
return $r;
}
session_start();
//include "db_connect_nb.php";
// get prior saturday
$psat = "2018-02-17 00:00:00";
$currfri = "2018-02-23 17:00:00";
//$qry = "Select * from cvisitor where ruid <> 0 and last_time >= '" . $psat . "' and last_time <= '" . $currfri . "' order by last_time";
//$res = mysqli_query($link, $qry);
// get registered user info
//$ncv = mysqli_num_rows($res);
//echo "cvqry = " . $qry . "<br>";
//echo "ncv = " . $ncv . "<br>";
$output = file_get_contents('urpts-top.xml');
for ($i = 0; $i < 20; $i++) {
// $cv = mysqli_fetch_array($res,MYSQLI_ASSOC);
// $lakos = strpos($cv['email'], "@lakos.com");
/* if (! $lakos) {
// get options / addons str
$oastr = "";
if ($cv['pepump'] == "Y") {
$oastr = "PE Pump";
}
if ($cv['pumprepair'] == "Y") {
if ($oastr == "") {
$oastr = "Pump Repair";
} else {
$oastr = $oastr . ", " . "Pump Repair";
}
}
if ($cv['valve_kit'] != "") {
if ($oastr == "") {
$oastr = $cv['valve_kit'] . "valve kit";
} else {
$oastr = $oastr . ", " . $cv['valve_kit'] . "valve kit";
}
}
if ($cv['dec_contact'] == "Y") {
if ($oastr == "") {
$oastr = "DEC Contact";
} else {
$oastr = $oastr . ", " . "DEC Contact";
}
}
if ($cv['multi'] == "Y") {
if ($oastr == "") {
$oastr = "Multi Tower";
} else {
$oastr = $oastr . ", " . "Multi Tower";
}
}
if ($cv['replbags'] != 0) {
if ($oastr == "") {
$oastr = $cv['replbags'] . " repl bags";
} else {
$oastr = $oastr . ", " . $cv['replbags'] . " repl bags";
}
}
if ($cv['strainer'] != "") {
if ($oastr == "") {
$oastr = "Strainer";
} else {
$oastr = $oastr . ", " . "Strainer";
}
}
// full stream opts, if any
if ($cv['cltype'] == "F") {
$qryfso = "SELECT * from fs_opts where vid = " . $cv['vid'];
$resfso = mysqli_query($link, $qryfso);
$nfso = mysqli_num_rows($resfso);
if ($nfso != 0) {
$fso = mysqli_fetch_array($resfso,MYSQLI_ASSOC);
if ($fso['lowprosep'] == "Y") {
if ($oastr == "") {
$oastr = "Low Pro Separator";
} else {
$oastr = $oastr . ", " . "Low Pro Separator";
}
}
if ($fso['SRI'] == "Y") {
if ($oastr == "") {
$oastr = "SRI";
} else {
$oastr = $oastr . ", " . "SRI";
}
}
if ($fso['PGK'] == "Y") {
if ($oastr == "") {
$oastr = "Pressure Gauge Kit";
} else {
$oastr = $oastr . ", " . "Pressure Gauge Kit";
}
}
if ($fso['WOF'] == "Y") {
if ($oastr == "") {
$oastr = "Weld on Flanges";
} else {
$oastr = $oastr . ", " . "Weld on Flanges";
}
}
if ($fso['Fspool'] == "Y") {
if ($oastr == "") {
$oastr = "Flanged Spools";
} else {
$oastr = $oastr . ", " . "Flanged Spools";
}
}
if ($fso['GCS'] == "Y") {
if ($oastr == "") {
$oastr = "Grooved Coupling Spools";
} else {
$oastr = $oastr . ", " . "Grooved Coupling Spools";
}
}
if ($fso['GRCoup'] == "Y") {
if ($oastr == "") {
$oastr = "Grooved Couplings";
} else {
$oastr = $oastr . ", " . "Grooved Couplings";
}
}
}
}
// b_hydro & sand_opts
if ($_SESSION['group'] == "sand") {
$qryso = "SELECT * from sand_opts where vid = " . $_SESSION['vid'];
$resso = mysqli_query($link, $qryso);
$nso = mysqli_num_rows($resso);
if ($nso != 0) {
$so = mysqli_fetch_array($resso,MYSQLI_ASSOC);
if ($so['bkwash'] != "") {
if ($oastr == "") {
$oastr = "Backwash: " . $so['bkwash'];
} else {
$oastr = $oastr . ", " . "Backwash: " . $so['bkwash'];
}
}
if ($so['material'] != "") {
if ($so['material'] == "C") {
$matl = "Carbon Steel";
} else {
$matl = "Stainless Steel";
}
if ($oastr == "") {
$oastr = "Material: " . $matl;
} else {
$oastr = $oastr . ", " . "Material: " . $matl;
}
}
if ($so['bkwhtank'] != "") {
if ($oastr == "") {
$oastr = $so['bkwhtank'];
} else {
$oastr = $oastr . ", " . $so['bkwhtank'];
}
}
if ($so['hlctrls'] != "") {
if ($oastr == "") {
$oastr = $so['hlctrls'];
} else {
$oastr = $oastr . ", " . $so['hlctrls'];
}
}
}
}
$ruid = $cv['ruid'];
$qryru = "SELECT * from registered_users where ix = " . $ruid;
$resru = mysqli_query($link, $qryru);
$ru = mysqli_fetch_array($resru,MYSQLI_ASSOC);
if ($ru['state'] == "") {
$stctry = $ru['country'];
} else {
$stctry = $ru['state'] . "/" . $ru['country'];
}
$dttime = date_conv(substr($cv['last_time'],0,10)) . substr($cv['last_time'],11,8);
$namestr = $ru['firstname'] . " " . $ru['lastname'];
$affil = $ru['affilliation'];
$projname = $cv['proj_name'];
$model = $cv['model'];
$price = "$" . $number_format($cv['net_price'],2);
$purge_meth = $cv['purge_method'];
$voltage = $cv['voltage'];
*/
$namestr = "NAMESTR-{$i}";
$affil = "AFFIL-{$i}";
$stctry = "STCTRY-{$i}";
$dttime = "DTTIME-{$i}";
$projname = "PROJNAME-{$i}";
$model = "MODEL-{$i}";
$price = "PRICE-{$i}";
$purge_meth = "PURGE_METH-{$i}";
$voltage = "VOLTAGE-{$i}";
$oastr = "OASTR-{$i}";
$output .= <<< ROW
<Row>
<Cell><Data ss:Type="String">$namestr</Data></Cell>
<Cell><Data ss:Type="String">$affil</Data></Cell>
<Cell><Data ss:Type="String">$stctry</Data></Cell>
<Cell><Data ss:Type="String">$dttime</Data></Cell>
<Cell><Data ss:Type="String">$projname</Data></Cell>
<Cell><Data ss:Type="String">$model</Data></Cell>
<Cell><Data ss:Type="String">$price</Data></Cell>
<Cell><Data ss:Type="String">$purge_meth</Data></Cell>
<Cell><Data ss:Type="String">$voltage</Data></Cell>
<Cell><Data ss:Type="String">$oastr</Data></Cell>
</Row>
ROW;
// }
}
$output .= file_get_contents('urpts-bottom.xml');
$filename = 'sample-xml-spreadsheet' . date('Y-m-d') . '.xml';
$fullpath = SAVE_LOCATION . $filename;
echo "fullpath = " . $fullpath . "<br>";
file_put_contents($fullpath, $output);
ASKER
ASKER
ASKER
ASKER
ASKER
<Worksheet ss:Name="urpts_urpt2018-02-16_22_26">
<Worksheet ss:Name="urpts_urpt<?php echo date('Y_m_d_H_i_s');?>">
Then instead of using file_get_contents(...) on the file do thisob_start();
require_once('urpts-top.xml');
$output = ob_get_clean();
Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.
TRUSTED BY
but looks like that has been deprecated
https://github.com/PHPOffice/PHPExcel
maybe try this one?
https://github.com/PHPOffice/PhpSpreadsheet