PHPExcel:为什么这不起作用?数据没有填充

时间:2017-11-06 19:15:04

标签: mysql phpexcel

寻求一点帮助。我正在使用PHPExcel使用MySQL表中的数据填充excel电子表格。我已经创建了列标题,添加了属性等,并且所有这些都在电子表格中显示正常。我与数据库的连接很好,但没有数据放入Excel。我已多次查看代码,但尚未找到罪魁祸首。我想要在电子表格的第8行开始数据插入,并希望行增加。我已经包含了PHPExcel.php文件,setActiveSheetINdex(0)。我没有正确设置单元格值吗?任何帮助将不胜感激。

这是我使用的PHPExcel代码。

$query = mysqli_query($db,"SELECT * FROM clients");

    $row = 8;

    while($data = mysqli_fetch_object($query))
    {

        $objPHPExcel->getActiveSheet() 
            ->setCellValue('A'.$row, $data->client_name) 
            ->setCellValue('B'.$row, $data->r_date)
            ->setCellValue('C'.$row, $data->w_reconst)
            ->setCellValue('D'.$row, $data->size_code)
            ->setCellValue('E'.$row, $data->b_techAmount)
            ->setCellValue('F'.$row, $data->i_techAmount)
            ->setCellValue('G'.$row, $data->j_techAmount)
            ->setCellValue('H'.$row, $data->s_techAmount)
            ->setCellValue('I'.$row, $data->m_techAmount)
            ->setCellValue('J'.$row, $data->c_techAmount)
            ->setCellValue('K'.$row, $data->k_techAmount);
    $row++;
    }

1 个答案:

答案 0 :(得分:0)

你必须在档案上写下你的数据

$query = mysqli_query($db, "SELECT * FROM clients");

$row = 8;

while($data = mysqli_fetch_object($query))
{
    $objPHPExcel->getActiveSheet() 
        ->setCellValue('A'.$row, $data->client_name) 
        ->setCellValue('B'.$row, $data->r_date)
        ->setCellValue('C'.$row, $data->w_reconst)
        ->setCellValue('D'.$row, $data->size_code)
        ->setCellValue('E'.$row, $data->b_techAmount)
        ->setCellValue('F'.$row, $data->i_techAmount)
        ->setCellValue('G'.$row, $data->j_techAmount)
        ->setCellValue('H'.$row, $data->s_techAmount)
        ->setCellValue('I'.$row, $data->m_techAmount)
        ->setCellValue('J'.$row, $data->c_techAmount)
        ->setCellValue('K'.$row, $data->k_techAmount);
    $row++;
}

//$filename = 'file name - ' . time() . '.xlsx';
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_end_clean();
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $filename . '"');