• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
不要清闲
博客园    首页    新随笔    联系   管理    订阅  订阅

phpExcel导出excel的类,每步都有说明

 1 require_once WEB_PATH . '/lib/PHPExcel/PHPExcel.php';
 2 require_once WEB_PATH . '/lib/PHPExcel/PHPExcel/IOFactory.php';
 3 
 4 class ExcelExport{
 5     public static function export($orderList){
 6         $columnArr = array('A'=>20, 'B'=>20, 'C'=>'auto', 'D'=>'auto', 'E'=>'auto', 'F'=>'auto', 'G'=>'auto', 'H'=>6, 'I'=>'auto', 'J'=>10, 'K'=>10, 'L'=>10, 'M'=>10, 'N'=>10);
 7 
 8         $objPHPExcel = new PHPExcel();
 9         //设置属性
10         $objPHPExcel->getProperties()->setCreator("gaoshikao@qq.com")
11                                      ->setLastModifiedBy("gaoshikao@qq.com")
12                                      ->setTitle("")
13                                      ->setSubject("")
14                                      ->setDescription("")
15                                      ->setKeywords("")
16                                      ->setCategory("");
17 
18         //设置标题
19         $objPHPExcel->setActiveSheetIndex(0)
20                     ->setCellValue('A1', '编号')
21                     ->setCellValue('B1', '单号')
22                     ->setCellValue('C1', '姓名')
23                     ->setCellValue('D1', '街道')
24                     ->setCellValue('E1', '城市')
25                     ->setCellValue('F1', '州')
26                     ->setCellValue('G1', '邮编')
27                     ->setCellValue('H1', '国家')
28                     ->setCellValue('I1', '联系电话')
29                     ->setCellValue('J1', '中文品名')
30                     ->setCellValue('K1', '英文品名')
31                     ->setCellValue('L1', '内件件数')
32                     ->setCellValue('M1', '商品重量')
33                     ->setCellValue('N1', '申报金额');
34 
35         //设置列宽,邮编和电话为文本格式
36         foreach($columnArr as $letter => $width){
37             if($width == 'auto')$objPHPExcel->getActiveSheet()->getColumnDimension($letter)->setAutoSize(true);
38             else $objPHPExcel->getActiveSheet()->getColumnDimension($letter)->setWidth($width);
39         }
40 
41         //填充数据
42         $currRow = 2;
43         foreach($orderList as $key => $order){
44             if($order['goods_count'] < 1)continue;
45 
46             $objPHPExcel->setActiveSheetIndex(0)
47                 ->setCellValueExplicit('A'.$currRow, $order['sn'], PHPExcel_Cell_DataType::TYPE_STRING)
48                 ->setCellValue('B'.$currRow, '')
49                 ->setCellValue('C'.$currRow, $order['username'])
50                 ->setCellValue('D'.$currRow, $order['street'].(empty($order['street2']) ? '' : "\n".$order['street2']))
51                 ->setCellValue('E'.$currRow, $order['city'])
52                 ->setCellValue('F'.$currRow, $order['state'])
53                 ->setCellValueExplicit('G'.$currRow, $order['zipcode'], PHPExcel_Cell_DataType::TYPE_STRING)
54                 ->setCellValue('H'.$currRow, $order['nation'])
55                 ->setCellValueExplicit('I'.$currRow, $order['phone'], PHPExcel_Cell_DataType::TYPE_STRING)
56                 ->setCellValue('J'.$currRow, '衣服')
57                 ->setCellValue('K'.$currRow, 'clothes')
58                 ->setCellValue('L'.$currRow, $order['goods_count'])
59                 ->setCellValue('M'.$currRow, '')
60                 ->setCellValue('N'.$currRow, '');
61 
62             $currRow++;
63         }
64 
65         //文档名称
66         $dateTip = date('Y-m-d');
67         $objPHPExcel->getActiveSheet()->setTitle($dateTip);
68 
69 
70         //设置第1个标签页为默认
71         $objPHPExcel->setActiveSheetIndex(0);
72 
73 
74         // Redirect output to a client’s web browser (Excel5)
75         $filename = '订单-' . $dateTip . '.xls';
76         header('Content-Type: application/vnd.ms-excel');
77         header('Content-Disposition: attachment;filename="'.$filename.'"');
78         header('Cache-Control: max-age=0');
79         // If you're serving to IE 9, then the following may be needed
80         header('Cache-Control: max-age=1');
81 
82         // If you're serving to IE over SSL, then the following may be needed
83         header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
84         header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
85         header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
86         header ('Pragma: public'); // HTTP/1.0
87 
88         $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
89         $objWriter->save('php://output');
90         exit;
91     }
92 }

 

posted on 2014-01-10 15:53  不要清闲  阅读(246)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3