利用phpexcel把excel导入数据库和数据库导出excel实现


Posted in PHP onJanuary 09, 2014

利用phpexcel把excel导入数据库和数据库导出excel实现

<?php
/*
*author zhy
*date 2012 06 12
*for excel
*/
date_default_timezone_set("PRC"); 
error_reporting(E_ALL);
error_reporting(0);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
require_once ('../Classes/PHPExcel.php');
require_once("config.php");
require_once("mysql.class.php");
//根据时间生成采购报表
$time = date("a");
$minute = date("i");
$apm  = "";
if($time=='pm'){
    $apm     = $time;
    $stime   = mktime(12,00,00,date('m'),date('d')-1,date('Y'));
    $etime   = mktime(11,59,59,date('m'),date('d'),date('Y'));
}else{
  $apm     = $time;
    $stime   = mktime(12,00,00,date('m'),date('d')-1,date('Y'));
    $etime   = mktime(11,59,59,date('m'),date('d'),date('Y'));
}
//实例化excel类
$objPHPExcel = new PHPExcel();
////////获取文档信息
////////$objProps = $objPHPExcel->getProperties();
///////print_r($objProps);
///////echo "<br/>";
///////$objProps->setDescription("test_123456");
///////print_r($objProps);

$objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A5','商品编码')
                ->setCellValue('B5','货号')
                ->setCellValue('C5','商品名称')
                ->setCellValue('D5','采购量');
//设置选定sheet表名
$objPHPExcel->getActiveSheet()->setTitle('祖名');
//设置字体样式
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setName('Arial')->setSize(25);//////->setUnderline(true);/////->getColor()->setARGB('FFFF0000');///->setBold(true);
//合并单元格 给单元格赋值(数值,字符串,公式)
$objPHPExcel->getActiveSheet()->mergeCells('A1:D3')->setCellValue('A1', 'zhongyi清单');
///////$objPHPExcel->getActiveSheet()->mergeCells('A4:D4')->setCellValue('A4', "=SUM(E4:F4)");
$date_now  = date("Y-m-d");
$objPHPExcel->getActiveSheet()->mergeCells('A4:D4')->setCellValue('A4', "采购日期:".$date_now." ".$apm." ");
//设置单列宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);//$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setRowHeight(50);/
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(44);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
//大边框样式 边框加粗
$lineBORDER = array(
 'borders' => array(
  'outline' => array(
   'style' => PHPExcel_Style_Border::BORDER_THICK,
   'color' => array('argb' => '000000'),
  ),
 ),
);
//表头样式
$head = array(
    'font'    => array(
    'bold'      => true
  ),
 'alignment' => array(
    'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
    'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
   ),
);
//标题样式
$title = array(
    'font'    => array(
    'bold'      => true
    ),
);
//居中对齐
$CENTER = array(
    'alignment' => array(
      'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
      'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
     ),
);
//靠右对齐
$RIGHT = array(
    'alignment' => array(
      'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT,
      'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER
     ),
);
//细边框样式
$linestyle = array(
 'borders' => array(
  'outline' => array(
   'style' => PHPExcel_Style_Border::BORDER_THIN,
   'color' => array('argb' => 'FF000000'),
  ),
 ),
);

$objPHPExcel->getActiveSheet()->getStyle('A1:D3')->applyFromArray($head);///->getAlignment()->getHorizontal('');///->getBorders()->getTop()->setBorderStyle('');
//->setWrapText(true);自动换行
$objPHPExcel->getActiveSheet()->getStyle('A4:D4')->applyFromArray($RIGHT); 
$objPHPExcel->getActiveSheet()->getStyle('A5:D5')->applyFromArray($title); 
//填充色
/////$objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->getStartColor()->setARGB('FFFF0000');/
     
//插入数据
$dsql->Execute('omebrand_list',"select i.goods_id , sum( `nums` ) AS num, i.name,i.addon,i.price,g.bn as b,i.bn as h,
g.goods_id,i.goods_id,i.order_id
FROM `sdb_b2c_order_items` as i,sdb_b2c_goods as g
WHERE i.order_id in (select order_id from sdb_b2c_orders where status ='active' and createtime between $stime and $etime) and i.goods_id=g.goods_id and g.cat_id=173 GROUP BY h");
$m = 0;
  unset($re);
  while($row=$dsql->GetObject('omebrand_list'))
  {   $re[$m] = get_object_vars($row);
  $m++;
  }
$row_count = 5;
$objPHPExcel->setActiveSheetIndex(0)
   ->setCellValue('A6', 12325416541)
            ->setCellValue('B6', 4962132165262)
            ->setCellValue('C6', 121515212515241521)
            ->setCellValue('D6', 96215465415);
foreach($re as $r => $dataRow) {
 $baseRow = 6;
 $row = $baseRow + $r;
 $bn=$dataRow[h];
 $goods_id = $dataRow[goods_id];
   $spec_value = "";
   $aa = unserialize($dataRow[addon]);
   if ($aa['product_attr']){
    foreach ($aa['product_attr'] as $arr_special_info)  {
     $spec_value = $arr_special_info['value'];
    }
   }
   preg_match_all('/\-?\d+\.?\d*/i',$spec_value,$row1);
   $num = $row1[0][0];
   $all = $num*$dataRow[num];
   if($spec_value==''){
    $all=$dataRow['num'];
    //$prce=$dataRow[price];
   }
 $objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A'.$row, $dataRow['b'])
                ->setCellValue('B'.$row, $bn)
             ->setCellValue('C'.$row, $dataRow['name'])
             ->setCellValue('D'.$row, $all);
    $objPHPExcel->getActiveSheet()->getStyle('A'.$row_count)->applyFromArray($linestyle);             
    $objPHPExcel->getActiveSheet()->getStyle('B'.$row_count)->applyFromArray($linestyle);
    $objPHPExcel->getActiveSheet()->getStyle('C'.$row_count)->applyFromArray($linestyle);
    $objPHPExcel->getActiveSheet()->getStyle('D'.$row_count)->applyFromArray($linestyle);              
 $baseRow++;
    $row_count++;
}
$objPHPExcel->getActiveSheet()->getStyle('A'.$row_count)->applyFromArray($linestyle);             
$objPHPExcel->getActiveSheet()->getStyle('B'.$row_count)->applyFromArray($linestyle);
$objPHPExcel->getActiveSheet()->getStyle('C'.$row_count)->applyFromArray($linestyle);
$objPHPExcel->getActiveSheet()->getStyle('D'.$row_count)->applyFromArray($linestyle);  
$objPHPExcel->getActiveSheet()->getStyle('A5:D'.$row_count)->applyFromArray($CENTER);  
$objPHPExcel->getActiveSheet()->getStyle('A1:D'.$row_count)->applyFromArray($lineBORDER);

//设置打印页边距
$objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0);
$objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
$objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0);
$objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0);
//设置纸张类型
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
//设置自动筛选
$objPHPExcel->getActiveSheet()->setAutoFilter('A5:D'.$row_count);
//设置自动换行
$objPHPExcel->getActiveSheet()->getStyle('B6:B'.$row_count)->getAlignment()->setWrapText(true);
//设置格式化数字
$objPHPExcel->getActiveSheet()->getStyle('A6:A'.$row_count)->getNumberFormat()->setFormatCode('0000000000');
//设置安全级别
$md=md5(time());
$md=substr($md,0,8);
$objPHPExcel->getActiveSheet()->getProtection()->setPassword("$md");
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);//
$objPHPExcel->getActiveSheet()->getProtection()->setSort(true);
$objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true);
//添加图片 
/*
$obj=$objPHPExcel->getActiveSheet();
$objDrawing = new PHPExcel_Worksheet_Drawing();   
$objDrawing->setName('wsyImg');   
$objDrawing->setDescription('Image inserted by zhy');   
$objDrawing->setPath('./wsy.jpg');   
$objDrawing->setHeight(50);   
$objDrawing->setCoordinates('H23');   
$objDrawing->setOffsetX(60);   
$objDrawing->setRotation(-10);   /
$objDrawing->getShadow()->setVisible(true);   
$objDrawing->getShadow()->setDirection(-20); / 
$objDrawing->setWorksheet($obj);
*/
//页眉页脚
//$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('zhy'); 
//$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('end'); 

$objPHPExcel->setActiveSheetIndex(0);
$tname=date('Y-m-dH',time());
$tnam=iconv('UTF-8','GBK','祖名订单');
$tname=$tnam.$tname;

// Excel 2007保存
//$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
//$objWriter->save(str_replace('.php', '.xlsx', __FILE__)); 
// Excel 5保存 
//$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); 
//$objWriter->save(str_replace('.php', '.xls', __FILE__)); 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', __FILE__));

//$url = "/data/home/htdocs/ec/public/files/".date("Y")."/".date("Ym")."/";
createDir($url);
function createDir($dir) {
 if  (!is_dir ($dir )) {
  mkdir($dir, 0777, true);
  chmod($dir, 0777);  
  chown( $dir, 'daemon' );
  chgrp( $dir, 'daemon' );   
 }
}
$name='forexmple_excel';
rename(str_replace('.php', '.xls', __FILE__), $name.'.xls');

?>
PHP 相关文章推荐
PHP MSSQL 存储过程的方法
Dec 24 PHP
DOM XPATH获取img src值的query
Sep 23 PHP
用PHP来计算某个目录大小的方法
Apr 01 PHP
php静态文件生成类实例分析
Jan 03 PHP
PHP实现的简单异常处理类示例
May 04 PHP
PHP实现微信模拟登陆并给用户发送消息的方法【文字,图片,图文】
Jun 29 PHP
PHP实现的简单对称加密与解密方法实例小结
Aug 28 PHP
PHP清除缓存的几种方法总结
Sep 12 PHP
PHP设计模式之工厂方法设计模式实例分析
Apr 25 PHP
PHP删除数组中特定元素的两种方法
Feb 28 PHP
PHP实现支持CURL字符串证书传输的方法
Mar 23 PHP
PHP 图片处理
Sep 16 PHP
php将mysql数据库整库导出生成sql文件的具体实现
Jan 08 #PHP
PHP修改session_id示例代码
Jan 08 #PHP
让PHP显示Facebook的粉丝数量方法
Jan 08 #PHP
利用浏览器的Javascript控制台调试PHP程序
Jan 08 #PHP
php获取从百度搜索进入网站的关键词的详细代码
Jan 08 #PHP
PHP IE中下载附件问题解决方法
Jan 07 #PHP
迅速确定php多维数组的深度的方法
Jan 07 #PHP
You might like
PHP合并两个数组的两种方式的异同
2012/09/14 PHP
php实现汉字验证码和算式验证码的方法
2015/03/07 PHP
PHP使用ffmpeg给视频增加字幕显示的方法
2015/03/12 PHP
自定义min版smarty模板引擎MinSmarty.class.php文件及用法
2016/05/20 PHP
在IE模态窗口中自由查看HTML源码的方法
2007/03/08 Javascript
手把手教你自己写一个js表单验证框架的方法
2010/09/14 Javascript
jquery实现输入框动态增减的实例代码
2013/07/14 Javascript
nodejs分页类代码分享
2014/06/17 NodeJs
js实现键盘上下左右键选择文字并显示在文本框的方法
2015/05/07 Javascript
JavaScript对表格或元素按文本,数字或日期排序的方法
2015/05/26 Javascript
谈谈Jquery中的children find 的区别有哪些
2015/10/19 Javascript
Express实现前端后端通信上传图片之存储数据库(mysql)傻瓜式教程(一)
2015/12/10 Javascript
JS基于clipBoard.js插件实现剪切、复制、粘贴
2016/05/03 Javascript
深入解析桶排序算法及Node.js上JavaScript的代码实现
2016/07/06 Javascript
Bootstrap table使用方法总结
2017/05/10 Javascript
js判断文件类型大小并给出提示的实现方法
2018/01/03 Javascript
微信小程序switch组件使用详解
2018/01/31 Javascript
node全局变量__dirname与__filename的区别
2019/01/14 Javascript
vue中使用vue-cli接入融云实现即时通信
2019/04/19 Javascript
Python中import导入上一级目录模块及循环import问题的解决
2016/06/04 Python
在Pycharm中调试Django项目程序的操作方法
2019/07/17 Python
使用 python pyautogui实现鼠标键盘控制功能
2019/08/04 Python
Pandas DataFrame中的tuple元素遍历的实现
2019/10/23 Python
python标准库OS模块函数列表与实例全解
2020/03/10 Python
python3安装OCR识别库tesserocr过程图解
2020/04/02 Python
PyQt实现计数器的方法示例
2021/01/18 Python
支持IE8的纯css3开发的响应式设计动画菜单教程
2014/11/05 HTML / CSS
详解html5 canvas常用api总结(二)--绘图API
2016/12/14 HTML / CSS
Canvas实现保存图片到本地的示例代码
2018/06/28 HTML / CSS
Hotels.com加拿大:领先的在线住宿网站
2018/10/05 全球购物
英国的一家创新礼品和小工具零售商:Menkind
2019/08/24 全球购物
餐厅考勤管理制度
2014/01/28 职场文书
副科级后备干部考察材料
2014/05/15 职场文书
乡镇党的群众路线教育实践活动个人对照检查材料
2014/09/23 职场文书
个人投资合作协议书
2014/10/12 职场文书
初二数学教学反思
2016/02/17 职场文书