PHP 导出Excel 的方法

/**
 * 导出Excel
 * @author david
 * @data 2018/10/18
 */
function export_goods_diff($exportData)
{
    header("Content-Type:text/html;charset=utf-8");
    include 'excel/Classes/PHPExcel/IOFactory.php';
    $file_name = auto_charset("商品入库单差异对比导出", "gb2312", "utf8");

    $objPHPExcel = new PHPExcel();
    $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getActiveSheet()->setTitle("商品列表");

    $objPHPExcel->setActiveSheetIndex(0);
    $objPHPExcel->getDefaultStyle()->getFont()->setName('Arial')->setSize(10);
    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(20);


    $objPHPExcel->getActiveSheet()
        ->setCellValue('A1', '单据编号')
        ->setCellValue('B1', '入库单号')
        ->setCellValue('C1', '合同单号')
        ->setCellValue('D1', '发放仓')
        ->setCellValue('E1', '收方')
        ->setCellValue('F1', '商品SKU')
        ->setCellValue('G1', '条形码')
        ->setCellValue('H1', '应入库数量')
        ->setCellValue('I1', '已入库数量')
        ->setCellValue('J1', '差异');

    foreach ($exportData as $k => $v) {

        $field1 = 'A'.($k+2);
        $objPHPExcel->getActiveSheet()->setCellValueExplicit($field1, $v['djbh'],
            PHPExcel_Cell_DataType::TYPE_STRING);

        $field1 = 'B'.($k+2);
        $objPHPExcel->getActiveSheet()->setCellValueExplicit($field1, $v['entryOrderCode'],
            PHPExcel_Cell_DataType::TYPE_STRING);

        $field2 = 'C'.($k+2);
        $objPHPExcel->getActiveSheet()->setCellValueExplicit($field2, $v['purchaseOrderCode'],
            PHPExcel_Cell_DataType::TYPE_STRING);

        $field3 = 'D'.($k+2);
        $objPHPExcel->getActiveSheet()->setCellValueExplicit($field3, $v['issue'],
            PHPExcel_Cell_DataType::TYPE_STRING);

        $field4 = 'E'.($k+2);
        $objPHPExcel->getActiveSheet()->setCellValueExplicit($field4,$v['consignee'],
            PHPExcel_Cell_DataType::TYPE_STRING);
        $field4 = 'F'.($k+2);
        $objPHPExcel->getActiveSheet()->setCellValueExplicit($field4,$v['barcode'],
            PHPExcel_Cell_DataType::TYPE_STRING);
        $field4 = 'G'.($k+2);
        $objPHPExcel->getActiveSheet()->setCellValueExplicit($field4,$v['barcode'],
            PHPExcel_Cell_DataType::TYPE_STRING);
        $field4 = 'H'.($k+2);
        $objPHPExcel->getActiveSheet()->setCellValueExplicit($field4,$v['planQty'],
            PHPExcel_Cell_DataType::TYPE_STRING);
        $field4 = 'I'.($k+2);
        $objPHPExcel->getActiveSheet()->setCellValueExplicit($field4,$v['count'],
            PHPExcel_Cell_DataType::TYPE_STRING);
        $field4 = 'J'.($k+2);
        $objPHPExcel->getActiveSheet()->setCellValueExplicit($field4,$v['diff'],
            PHPExcel_Cell_DataType::TYPE_STRING);
    }
    // exit;
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    // 到浏览器

    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header('Content-Disposition:inline;filename="' . $file_name . '.xls"');
    header("Content-Transfer-Encoding: binary");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Pragma: no-cache");
    $objWriter->save('php://output');
    //$objWriter->save('e:\\temp\\'.$file_name.".xls");

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值