使用PHPExcel类完成简单的读取导出

导出转载自:https://jingyan.baidu.com/article/915fc414f4c2e451384b205c.html

读取转载自:出处太多,原作者链接无法辨别,望作者谅解。

读取:

load($filename);
//获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推

$currentSheet=$PHPExcel->getSheet(0);
//获取总列数
$allColumn=$currentSheet->getHighestColumn();
//获取总行数
$allRow=$currentSheet->getHighestRow();
//循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始
for($currentRow=1;$currentRow<=$allRow;$currentRow++){
    //从哪列开始,A表示第一列
    for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){
        //数据坐标
        $address=$currentColumn.$currentRow;
        //读取到的数据,保存到数组$arr中
        $data[$currentRow][$currentColumn]=$currentSheet->getCell($address)->getValue();
    }
}
echo "
";
var_dump($data);
echo "
";

导出:

getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
}

//表格数组
$data = array(
    array('1','小王','男','20','100'),
    array('2','小李','男','20','101'),
    array('3','小张','女','20','102'),
    array('4','小赵','女','20','103')
);
//填充表格信息
for ($i = 2;$i <= count($data) + 1;$i++) {
    $j = 0;
    foreach ($data[$i - 2] as $key=>$value) {
        $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");
        $j++;
    }
}

//创建Excel输入对象
$write = new PHPExcel_Writer_Excel5($excel);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");;
header('Content-Disposition:attachment;filename="testdata.xls"');
header("Content-Transfer-Encoding:binary");
$write->save('php://output');


你可能感兴趣的:(资料,PHP功能块)