php PHPExcel setCellValueByColumnAndRow 不将数据写入电子表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11468606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHPExcel setCellValueByColumnAndRow not writing data to spreadsheet
提问by m_grif
i'm outputting data from a mysql database to an excel workbook using PHPExcel. My workbook has 3 sheets and most of it is working fine. I'm having problems with the last section of output to the third sheet. What I am trying to do is output a table with row headers and column headers the values of which are taken from a mysql table and then a figure per row/column combination also taken from the mysql table based on what that row/column header is. The row and column headers are written to the file as they should be but the inner table figures are not. When I echo the output to screen all the data is appearing and the row/ column iterations are incrementing as they should just the setCellValueByColumnAndRow appears to not be setting the values in the worksheet. The section of code that i'm having trouble with is below. Can anyone see where the problem is in my code?
我正在使用 PHPExcel 将数据从 mysql 数据库输出到 excel 工作簿。我的工作簿有 3 张纸,其中大部分工作正常。我在输出到第三张纸的最后一部分时遇到问题。我想要做的是输出一个带有行标题和列标题的表,其值取自 mysql 表,然后根据行/列标题从 mysql 表中获取每行/列组合的数字. 行和列标题按原样写入文件,但内部表格数字不是。当我回显输出到屏幕时,所有数据都出现并且行/列迭代正在增加,因为它们应该只是 setCellValueByColumnAndRow 似乎没有设置工作表中的值。我遇到问题的代码部分如下。
$objPHPExcel->setActiveSheetIndex(2);
while($srow = mysql_fetch_assoc($query_company))
{
$newarray[] = $srow['entity'];
}
$row4 = 2;
$col4 = 1;
while($trow = mysql_fetch_row($query_ctry))
{
$country = $trow[0];
while($comp = each($newarray))
{
$company = $comp[1];
$total = mysql_query("SELECT noparts FROM totalslist WHERE country = '$country' AND entity = '$company'") or die (mysql_error());
if ($numrows = mysql_num_rows($total) == 0)
{
$totalres = 0;
}
else
{
$result3 = mysql_fetch_row($total);
$totalres = $result3[0];
}
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col4, $row4, $totalres);
$col4++;
}
reset($newarray);
$row4++;
$col4 = 1;
}
采纳答案by m_grif
**Solution! Actually I solved this problem by changing the code slightly. I was writing the row and column headers first and then adding the inner table values after. I changed it so that I added the headers and data as I incremented through the rows and columns as shown in code below. Might help someone else.
**解决方案!实际上我通过稍微更改代码解决了这个问题。我首先编写行和列标题,然后添加内部表值。我更改了它,以便在我通过行和列递增时添加标题和数据,如下面的代码所示。可能会帮助别人。
$objPHPExcel->createSheet(2);
$objPHPExcel->setActiveSheetIndex(2);
$objPHPExcel->getActiveSheet()->setTitle('PivotTable');
$query_ctry = mysql_query("SELECT DISTINCT country FROM totalslist ORDER BY country");
$numctry = mysql_num_rows($query_ctry);
$query_company = mysql_query("SELECT DISTINCT entity FROM totalslist ORDER BY entity");
$numcomp = mysql_num_rows($query_company);
while($srow = mysql_fetch_assoc($query_company))
{
$newarray[] = $srow['entity'];
}
$row3 = 2;
$col3 = 1;
while($trow = mysql_fetch_row($query_ctry))
{
$country = $trow[0];
while($comp = each($newarray))
{
$company = $comp[1];
$total = mysql_query("SELECT noparts FROM totalslist WHERE country = '$country' AND entity = '$company'");
if ($numrows = mysql_num_rows($total) == 0)
{
$totalres = 0;
}
else
{
$result3 = mysql_fetch_row($total);
$totalres = $result3[0];
}
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, $row3, $country);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col3, 1, $company);
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col3, $row3, $totalres);
$col3++;
}
reset($newarray);
$row3++;
$col3 = 1;
}
// Save Excel file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit();

