代码:
<?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)。
答案 0 :(得分:0)
您只需将PHPExcel单元格的值作为常用Excel公式进行插入即可。所以就这样说:
=A$row*C$row
在列中,您希望获得该行的unitario * quantdade结果。总和相同。
例如,当您进行此循环时:
while ($row = mysql_fetch_row($result)) {
$col = 'A';
foreach($row as $cell) {
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell);
$col++;
}
//add this line to get the unitario*quantidade result
//in the last column
//here I'm assuming that quantitade and unitario are placed
//in column A and C in your excel, but this may be wrong, so
//replace them with the proper values
$objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,"=A$row*C$row");
$rowNumber++;
}