代码:
<?php require_once('Connections/valesilveira.php'); ?>
<?php
mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
$colname_dados = "-1";
if (isset($_GET['id_proposta'])) {
$colname_dados = $_GET['id_proposta'];
}
mysql_select_db($database_valesilveira, $valesilveira);
$query_dados = sprintf("SELECT posicao, dados.desc, quantidade, unitario FROM propostas, clientes, dados WHERE propostas.id_proposta = dados.id_proposta AND propostas.id_cliente = clientes.id_cliente AND propostas.id_proposta = %s ORDER BY posicao ASC", GetSQLValueString($colname_dados, "int"));
$dados = mysql_query($query_dados, $valesilveira) or die(mysql_error());
$row_dados = mysql_fetch_assoc($dados);
$totalRows_dados = mysql_num_rows($dados);
$colname_propostas = "-1";
if (isset($_GET['id_proposta'])) {
$colname_propostas = $_GET['id_proposta'];
}
mysql_select_db($database_valesilveira, $valesilveira);
$query_propostas = sprintf("SELECT id_proposta, data FROM propostas WHERE id_proposta = %s", GetSQLValueString($colname_propostas, "int"));
$propostas = mysql_query($query_propostas, $valesilveira) or die(mysql_error());
$row_propostas = mysql_fetch_assoc($propostas);
$totalRows_propostas = mysql_num_rows($propostas);
$data = $row_propostas['data'];
setlocale (LC_ALL, "pt_BR", "ptb");
$data_formatada = strftime("%d de %B de %Y", strtotime($data));
?>
<?php
// require the PHPExcel file
require 'Classes/PHPExcel.php';
// simple query
$headings = array('posicao','desc','quantidade','unitario','iliquido','total');
if ($result = mysql_query($query_dados) or die(mysql_error())) {
// Create a new PHPExcel object
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setTitle('List of Data');
$rowNumber = 1;
$col = 'A';
foreach($headings as $heading) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$heading);
$col++;
}
// Loop through the result set
$rowNumber = 2;
while ($row = mysql_fetch_row($result)) {
$col = 'A';
foreach($row as $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
$rowNumber++;
}
// Freeze pane so that the heading line won't scroll
$objPHPExcel->getActiveSheet()->freezePane('A2');
// Save as an Excel BIFF (xls) file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="userList.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
exit();
}
echo 'a problem has occurred... no data retrieved from the database';
?>
<?php
mysql_free_result($dados);
mysql_free_result($propostas);
mysql_free_result($data);
?>
好的,我到目前为止所做的代码,但这只是从数据库中获取数据并循环它,我想为最后 2 个 cols 做一些安排。
我得到了 posicao、desc、quantidade、unitario 的数据,但是对于 iliquido 和 total,我想做一些计算。
对于 iliquido,我想制作 unitario*quantidade 并在列上显示所有结果。
对于总数,我想用 iliquido 值的总数(例如=sum(2ndrow:lastrow))制作一行。