PHPExcel动态设置单元格值

时间:2015-08-04 10:18:25

标签: php sql-server phpexcel

这是我获取Excel文件数据的代码......

    require_once 'IOFactory.php';
    $objPHPExcel = PHPExcel_IOFactory::load("NAV USER ACCESS Calculation.xlsx"); 
    $objPHPExcel->setActiveSheetIndex(2);

    $worksheet = $objPHPExcel->getActiveSheet();

    $worksheetTitle     = $worksheet->getTitle();
    $highestRow         = $worksheet->getHighestRow(); // e.g. 10
    $highestColumn      = $worksheet->getHighestColumn(); // e.g 'F'
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $nrColumns = ord($highestColumn) - 64;
    echo '<div id="table-cont">
        <table border="1">';
    for ($row = 1; $row <= $highestRow; ++ $row)
    {
        echo '<tr>';
        for ($col = 0; $col < $highestColumnIndex; ++ $col)
        {
            $cell = $worksheet->getCellByColumnAndRow($col, $row);
            $val = $cell->getValue();
            $val =$cell->getCalculatedValue();
            $val =$cell->getFormattedValue();

            $dataType = PHPExcel_Cell_DataType::dataTypeForValue($val);
            echo '<td>' . $val . '</td>';
        }
        echo '</tr>';
    }
    echo '</table>
        <div id="clock"></div>
        </div>';

这是我将我的表设置为excel的代码,但我不知道什么是错的......

    $serverName="192.168.1.253";
    $conInfo=array("Database"=>"dbName","UID"=>"sa","PWD"=>"xxxxx");
    $con= sqlsrv_connect($serverName,$conInfo);

    require_once 'IOFactory.php';
    $objPHPExcel = PHPExcel_IOFactory::load("NAV USER ACCESS Calculation.xlsx"); 
    $objPHPExcel->setActiveSheetIndex(2);

    $worksheet = $objPHPExcel->getActiveSheet();

    $worksheetTitle     = $worksheet->getTitle();
    $highestRow         = $worksheet->getHighestRow(); // e.g. 10
    $highestColumn      = $worksheet->getHighestColumn(); // e.g 'F'
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    $nrColumns = ord($highestColumn) - 64;
    echo '<div id="table-cont">
        <table border="1">';
    $sql= sqlsrv_query($con,"SELECT * FROM dbo.[Active Session]");
    while($data=sqlsrv_fetch_array($sql))
    {
        for ($row = 1; $row <= $highestRow; ++ $row)
        {
            echo '<tr>';
            for ($col = 0; $col < $highestColumnIndex; ++ $col)
            {
                $cell = $worksheet->setCellValueByColumnAndRow($col, $row);
                $val = $cell->getValue();
                $val =$cell->getCalculatedValue();
                $val =$cell->getFormattedValue();

                $dataType = PHPExcel_Cell_DataType::dataTypeForValue($val);
                echo '<td>' . $val . '</td>';
            }
            echo '</tr>';
        }
    }
    echo '</table>
        <div id="clock"></div>
        </div>';

1 个答案:

答案 0 :(得分:0)

应该像

一样简单
$worksheet = $objPHPExcel->getActiveSheet();

$highestRow = $worksheet->getHighestRow() + 1;
$sql= sqlsrv_query($con,"SELECT * FROM dbo.[Active Session]");
while($data=sqlsrv_fetch_array($sql))
{
    $worksheet->fromArray($data, null, 'A' . $highestRow++);
}

然后再次保存文件