PHPEXCEL数据导出案例

    <?php
    header("Content-type: application/octet-stream;charset=utf-8");
    require_once('PHPExcel.php');
    //require_once 'Classes/PHPExcel/Reader/Excel2007.php';
    //require_once 'Classes/PHPExcel/Reader/Excel5.php';
    //include_once 'Classes/PHPExcel/IOFactory.php';
    //require_once('/var/www/html/agent/agentfunctions.php');
    $objPHPExcel = new PHPExcel();

    //$sql = "select * from contactinfo limit 0,100";
    //$result = getMySqlData($sql,"IPCCP");

    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(8);
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
    $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
    $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
    $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
    $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setAutoSize(true);
    $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
    $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
    $objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A1', '联系记录 时间:' . date('Y-m-d H:i:s'))
    ->setCellValue('A2', '编号')
    ->setCellValue('B2', '企业名称')
    ->setCellValue('C2', '企业联系人')
    ->setCellValue('D2', '主题')
    ->setCellValue('E2', '创建时间')
    ->setCellValue('F2', '负责人')
    ->setCellValue('G2', '内容')
    ->setCellValue('H2', '最后修改人')
    ->setCellValue('I2', '最后修改日期');

    for($i=3;$i<=10;$i++){
    $objPHPExcel->getActiveSheet(0)->setCellValue('A' . $i,"汉字文字");
    // $objPHPExcel->getActiveSheet(0)->setCellValue('B' . $i,iconv("utf-8", "gb2312",$result[$i-3][2]));
    // $objPHPExcel->getActiveSheet(0)->setCellValue('C' . $i,iconv("utf-8", "gb2312",$result[$i-3][3]));
    // $objPHPExcel->getActiveSheet(0)->setCellValue('D' . $i,iconv("utf-8", "gb2312",$result[$i-3][4]));
    // $objPHPExcel->getActiveSheet(0)->setCellValue('E' . $i,iconv("utf-8", "gb2312",$result[$i-3][5]));
    // $objPHPExcel->getActiveSheet(0)->setCellValue('F' . $i,iconv("utf-8", "gb2312",$result[$i-3][6]));
    // $objPHPExcel->getActiveSheet(0)->setCellValue('G' . $i,iconv("utf-8", "gb2312",$result[$i-3][7]));
    // $objPHPExcel->getActiveSheet(0)->setCellValue('H' . $i,iconv("utf-8", "gb2312",$result[$i-3][8]));
    // $objPHPExcel->getActiveSheet(0)->setCellValue('I' . $i,iconv("utf-8", "gb2312",$result[$i-3][9]));
    // $objPHPExcel->getActiveSheet()->getStyle('A' . $i . ':I' . $i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
    // $objPHPExcel->getActiveSheet()->getStyle('A' . $i . ':I' . $i)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
    // $objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(16);
    // $i++ ;
    }
    $objPHPExcel->getActiveSheet()->setTitle('联系记录');
    $objPHPExcel->setActiveSheetIndex(0);
    $filename = date('Y-m',time())."_联系记录";
    ob_end_clean() ;
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename='.$filename.'.xls');
    header('Cache-Control: No-cache');
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');

    ?>

    转载请注明:RAIN MAN » PHPEXCEL数据导出案例

    喜欢 0

还没有人抢沙发呢~