我正在从数据库中检索信息并使用PHPExcel将其导出到excel。检索信息似乎工作正常,但列在Excel工作表中复制。 我使用以下方法来检索信息:
public function monthlyKm($startDate, $endDate, $userid)
{
$query = "SELECT a.`travelDate`,MIN(a.`openning`) AS minimum, MAX(a.`closing`) AS maximum, MAX(a.`closing`)- MIN(a.`openning`) AS diff, b.`destination`
FROM kilologs a
INNER JOIN users u ON u.`userid` = a.`userid`
INNER JOIN destination b ON a.`destid` = b.`destid`
WHERE (a.`travelDate` BETWEEN CAST('".$startDate."' AS DATE) AND CAST('".$endDate."' AS DATE))
AND a.userid = $userid
GROUP BY a.`travelDate`, a.`destid`";
$query_set = mysql_query($query) or die(mysql_error());
return $query_set;
}
信息应按以下方式显示:
但这就是我得到的:
这是生成excelsheet的代码:
$result_set = $kiloLog->monthlyKm( $startDate,$endDate,$userid);
if(isset($_POST['download']))
{
require_once 'src/PHPExcel.php';
try{
$sheet = new PHPExcel();
$sheet->getActiveSheet()->getSheetView()->setZoomScale(75);
//set Metadata
$sheet->getProperties()->setCreator('www.bitsofttech.co.za')
->setLastModifiedBy('www.bitsofttech.co.za')
->setTitle('Kilometer Logs')
->setKeywords('kilos logged report');
//set default settings
$sheet->getDefaultStyle()->getAlignment()->setVertical(
PHPExcel_Style_Alignment::VERTICAL_TOP);
$sheet->getDefaultStyle()->getAlignment()->setHorizontal(
PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$sheet->getDefaultStyle()->getFont()->setName('Calibri');
$sheet->getDefaultStyle()->getFont()->setSize(12);
//Get reference to active spreadsheet in workbook
$sheet->setActiveSheetIndex(0);
$activeSheet = $sheet->getActiveSheet();
//Set Print Options
$activeSheet->getPageSetup()->setOrientation(
PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE)
->setFitToWidth(1)
->setFitToHeight(0);
$activeSheet->getHeaderFooter()->setOddHeader('&C&B&16' .
$sheet->getProperties()->getTitle())
->setOddFooter('&CPage &P of &N');
//Populate the sheet with data
$row = mysql_fetch_assoc($result_set);
print_r($row);
$colHeaders = array_keys($row);
$col = 'A';
$rowNum = 1;
//set the column headings
$activeSheet->setCellValue('A1','Date');
$activeSheet->getColumnDimension('A')->setAutoSize(true);
$activeSheet->setCellValue('B1','Opening');
$activeSheet->getColumnDimension('B')->setAutoSize(true);
$activeSheet->setCellValue('C1','Closing');
$activeSheet->getColumnDimension('C')->setAutoSize(true);
$activeSheet->setCellValue('D1','Total');
$activeSheet->getColumnDimension('D')->setAutoSize(true);
$activeSheet->setCellValue('E1','Destination');
$activeSheet->getColumnDimension('E')->setAutoSize(true);
//Populate the individual cells
do{
$col = 'A';
$rowNum++;
foreach($row as $value){
$activeSheet->setCellValue($col++ . $rowNum, $value);
}
}while($row= mysql_fetch_array($result_set));
$activeSheet->getStyle('A2:A' .$rowNum)->getAlignment()
->setHorizontal( PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$activeSheet->getStyle('B2:B' .$rowNum)->getAlignment()
->setHorizontal( PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$activeSheet->getStyle('C2:C' .$rowNum)->getAlignment()
->setHorizontal( PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$activeSheet->getStyle('E2:E' .$rowNum)->getAlignment()
->setHorizontal( PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$activeSheet->getStyle('D2:D' .$rowNum)->getAlignment()
->setHorizontal( PHPExcel_Style_Alignment::HORIZONTAL_RIGHT)
->setWrapText(true);
//Give the spreadsheet a title
$activeSheet->setTitle('Monthly Logs');
//Generate the Excel file and download
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Monthlylogs.xlsx"');
header('Cache-Control: max-age=0');
$writer = PHPExcel_IOFactory::createWriter($sheet, 'Excel2007');
ob_end_clean();
$writer->save('php://output');
exit();
}catch(Exception $e){
$error = $e->getMessage();
echo $error;
}
答案 0 :(得分:1)
Errrm ...你的意思是列(即垂直线;数据集中的值)重复,对吧?因为我找不到任何重复的行(即水平线;数据集)。
你的问题出在你的循环中:
$row = mysql_fetch_assoc($result_set);
// ...
do{
// ...
foreach($row as $value){
// use $value
}
} while ($row = mysql_fetch_array($result_set));
请注意,您使用 mysql_fetch_assoc 初始化$row
,但是为了迭代,您可以调出 mysql_fetch_array 。这是方法签名和documentation on mysql_fetch_array的一些解释:
数组mysql_fetch_array(resource $ result [,int $ result_type = MYSQL_BOTH])
...
通过使用 MYSQL_BOTH (默认),您将获得一个包含关联索引和数字索引的数组。使用 MYSQL_ASSOC ,您只能获得关联索引(因为mysql_fetch_assoc()有效),使用 MYSQL_NUM ,您只能获得数字索引(因为mysql_fetch_row()有效)。
<强> TL; DR 强>
答案在下方更新:
实际上有两个问题:
问题:第一行数据(excel第2行)与以下行不同。
原因:不同的API调用循环初始化和继续。
修复:在两个地方使用相同的API调用。
问题:第3行(数据行2及更高版本)之后的每个excel行都有两次值。
原因: mysql_fetch_array()
默认情况下会为每个值创建两个索引:一个数字和一个关联(即列名称)。
修复:使用不同的API调用或指定$result_type
。
解决代码示例中包含的两个问题:
while (($row = mysql_fetch_row($result_set)) !== FALSE) {
// ...
foreach ($row as $value) {
// ...
}
}