phpexcel笔记
和$allColumn = $objSheet->getHighestDataColumn(); //获取表格最大列数,例如:A
$numColumn = $this->columnIndexFromString($allColumn);//转换为数字,例如A转换为1
PHPExcel_Cell::stringFromColumnIndex($str - 1);//转换为字母,例如1转换为A
$allRow = $objSheet->getHighestDataRow(); //获取表格最大行数
$MergeCells = $objSheet->getMergeCells();//获取表中合并单元的信息
$clonedSheet = clone $objPHPExcel->getActiveSheet(); //科隆复制一张sheet表
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);//获取表单所有元素
$noNull = array();
//去掉数组中二维空值
foreach ($allDataInSheet as $key=>$value){
$noNull[$key] = array_filter($value);
}
$dataInSheet = array_filter($noNull);//去掉数组中一维空值
其实最简便办法是遍历表中所有单元,在表中需要插入数据的地方用标识符表示出来,查到就写入数据.
问题二:
复制表格中的一行,然后插入到某行
步骤1:先插入新行
$objPHPExcel->getActiveSheet()->insertNewRowBefore(2,10); //在第2行前面,插入10行。
步骤2:把复制的样式粘贴到新插入的行(rou级)
function copyRowFull(&$ws_from, &$ws_to, $row_from, $row_to) { $ws_to->getRowDimension($row_to)->setRowHeight($ws_from->getRowDimension($row_from)->getRowHeight()); $lastColumn = $ws_from->getHighestColumn(); ++$lastColumn; for ($c = 'A'; $c != $lastColumn; ++$c) { $cell_from = $ws_from->getCell($c.$row_from); $cell_to = $ws_to->getCell($c.$row_to); $cell_to->setXfIndex($cell_from->getXfIndex()); // black magic here $cell_to->setValue($cell_from->getValue()); } }
$ws_fromh 和
$ws_to
代表表格的sheet. $row_from
and $row_to
代表sheet中的行。from和to如字面意思
到此结束。(注意步骤2,因为前面插入了1行,所有$row_from和$row_to 要加1)
步骤参考3:把复制行的样式粘贴进去(单元格级)
$objPHPExcel->getActiveSheet()->duplicateStyle($objPHPExcel->getActiveSheet()->getStyle('A1'),'A2:A10); //把 will copy the style from cell A1 and duplicate it to cells A2, A3, A4.... A10