如何删除Excel工作表中的前两个空行?

时间:2015-06-20 04:27:51

标签: php phpexcel

示例代码下方。它可能会帮助你.. 当我将行号(1)更改为5时,excel表中的七行为空。(LOCATION:setCellValueByColumnAndRow($ col,1,$ field);)

$objPHPExcel = new PHPExcel();  
    $objPHPExcel->getProperties()->setTitle("export")->setDescription("none");  
    $objPHPExcel->setActiveSheetIndex(0);       

    $fields = array('ram','one','two');     
    $col = 0;   
    foreach ($fields as $field)
    {
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
        $col++;
    }

    header('Content-Type: text/csv');
    header('Content-Disposition: attachment;filename="Export.csv"');
    header('Cache-Control: max-age=0'); 
    $objWriter->save('php://output');

2 个答案:

答案 0 :(得分:1)

$objPHPExcel = new PHPExcel();  
$objPHPExcel->getProperties()->setTitle("export")->setDescription("none");  
$objPHPExcel->setActiveSheetIndex(0);       

$field = array('ram','one','two');      
$col = 0;   
foreach ($fields as $field)
{
  if(empty($field)){
    $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1,     $field);
    $col++;
  }
}

header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename="Export.csv"');
header('Cache-Control: max-age=0'); 
$objWriter->save('php://output');

答案 1 :(得分:1)

您正在定义$field但是预先引用$fields .... s会产生很大的不同

$field = array('ram','one','two');      

foreach ($fields as $field)

尝试使用正确的变量名称

定义数组
$fields = array('ram','one','two');