我尝试使用PHPExcel从PHP生成Excel电子表格,并且我遇到了一种奇怪的行为。我的数据来自一个mysql数据库,一旦我收集了我需要的所有内容并将所有信息存储到一个对象数组中,我尝试通过在一个循环中调用setCellValue来创建我的Excel,这是我的代码:
for($i=0;$i<count($art);$i++) {
$r=$i+2;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$r, ''.utf8_encode($art[$i]->field1))
->setCellValue('B'.$r, ''.utf8_encode($art[$i]->field2))
->setCellValue('C'.$r, ''.utf8_encode($art[$i]->field3))
->setCellValue('D'.$r, ''.utf8_encode($art[$i]->field4))
->setCellValue('E'.$r, ''.utf8_encode($art[$i]->field5))
->setCellValue('F'.$r, ''.utf8_encode($art[$i]->field6));
}
输出结果是一个不可读的电子表格填充了随机字符,甚至连坐标都没有得到尊重。最奇怪的是,如果我将这种方法称为六次而不是使用for循环,它就能完美地运行。此代码提供了完美的输出:
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A2', ''.utf8_encode($art[0]->field1))
->setCellValue('B2', ''.utf8_encode($art[0]->field2))
->setCellValue('C2', ''.utf8_encode($art[0]->field3))
->setCellValue('D2', ''.utf8_encode($art[0]->field4))
->setCellValue('E2', ''.utf8_encode($art[0]->field5))
->setCellValue('F2', ''.utf8_encode($art[0]->field6));
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A3', ''.utf8_encode($art[1]->field1))
->setCellValue('B3', ''.utf8_encode($art[1]->field2))
->setCellValue('C3', ''.utf8_encode($art[1]->field3))
->setCellValue('D3', ''.utf8_encode($art[1]->field4))
->setCellValue('E3', ''.utf8_encode($art[1]->field5))
->setCellValue('F3', ''.utf8_encode($art[1]->field6));
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', ''.utf8_encode($art[2]->field1))
->setCellValue('B4', ''.utf8_encode($art[2]->field2))
->setCellValue('C4', ''.utf8_encode($art[2]->field3))
->setCellValue('D4', ''.utf8_encode($art[2]->field4))
->setCellValue('E4', ''.utf8_encode($art[2]->field5))
->setCellValue('F4', ''.utf8_encode($art[2]->field6));
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A5', ''.utf8_encode($art[3]->field1))
->setCellValue('B5', ''.utf8_encode($art[3]->field2))
->setCellValue('C5', ''.utf8_encode($art[3]->field3))
->setCellValue('D5', ''.utf8_encode($art[3]->field4))
->setCellValue('E5', ''.utf8_encode($art[3]->field5))
->setCellValue('F5', ''.utf8_encode($art[3]->field6));
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A6', ''.utf8_encode($art[4]->field1))
->setCellValue('B6', ''.utf8_encode($art[4]->field2))
->setCellValue('C6', ''.utf8_encode($art[4]->field3))
->setCellValue('D6', ''.utf8_encode($art[4]->field4))
->setCellValue('E6', ''.utf8_encode($art[4]->field5))
->setCellValue('F6', ''.utf8_encode($art[4]->field6));
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A7', ''.utf8_encode($art[5]->field1))
->setCellValue('B7', ''.utf8_encode($art[5]->field2))
->setCellValue('C7', ''.utf8_encode($art[5]->field3))
->setCellValue('D7', ''.utf8_encode($art[5]->field4))
->setCellValue('E7', ''.utf8_encode($art[5]->field5))
->setCellValue('F7', ''.utf8_encode($art[5]->field6));
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A8', ''.utf8_encode($art[6]->field1))
->setCellValue('B8', ''.utf8_encode($art[6]->field2))
->setCellValue('C8', ''.utf8_encode($art[6]->field3))
->setCellValue('D8', ''.utf8_encode($art[6]->field4))
->setCellValue('E8', ''.utf8_encode($art[6]->field5))
->setCellValue('F8', ''.utf8_encode($art[6]->field6));
我确实遗漏了一些东西,但我无法弄清楚它是什么。有什么建议吗?