首页 > 代码库 > 关于导出Excel

关于导出Excel

导出Excel,是我作为程序员来做比较无头绪的功能,还好有我的小付哥,当我还是个小菜鸟(现在还是小菜鸟),他用超强的学习能力,做功能,带我做功能
不说废话了

1.PHP-Excel,,在调用时要引入Excel5.php,和PHPExcel.php两个文件。

2.直接上代码行不行(这个代码是别人的,俺自己也写不来)

require_once ‘../../PHPExcel-1.7.6/PHPExcel.php‘;    require_once ‘../../PHPExcel-1.7.6/Excel5.php‘;/*2014年1月14日 12:05:49吴文付-------杨玲,哈哈phpexcel默认的中文处理是utf-8 因此在填充单元格时,必须转码mb_convert_encoding(‘货品编码‘, "utf-8", "GB2312")*/            header("Content-Type: application/vnd.ms-excel; charset=GB2312");        // 创建一个处理对象实\x8B    $objExcel = new PHPExcel();        // 创建文件格式写入对象实例, uncomment    $objWriter = new PHPExcel_Writer_Excel5($objExcel);        //设置文档基本属\xA7/**似乎一般情况下用不\xB0**/    $objProps = $objExcel->getProperties();    $objProps->setCreator("wwf");    $objProps->setLastModifiedBy("wwf");    $objProps->setTitle("元器件仓库");    $objProps->setSubject("元器件仓库");    $objProps->setDescription("元器件仓库");    $objProps->setKeywords("元器件仓库");    $objProps->setCategory("元器件仓库");        //*************************************           //设置当前的sheet索引,用于后续的内容操作\x82           //一般只有在使用多个sheet的时候才需要显示调用\x82           //缺省情况下,PHPExcel会自动创建第一个sheet被设置SheetIndex=0           $objExcel->setActiveSheetIndex(0);           $objActSheet = $objExcel->getActiveSheet();                     //设置当前活动sheet的名            $objActSheet->setTitle(mb_convert_encoding(‘元器件库存表‘, "utf-8", "GB2312"));           //设置宽度,这个值和EXCEL里的不同,不知道是什么单位,略小于EXCEL中的宽度       $objActSheet->getColumnDimension(‘A‘)->setWidth(10);     $objActSheet->getColumnDimension(‘B‘)->setWidth(10);     $objActSheet->getColumnDimension(‘C‘)->setWidth(20);     $objActSheet->getColumnDimension(‘D‘)->setWidth(20);     $objActSheet->getColumnDimension(‘E‘)->setWidth(10);         //$objActSheet->getRowDimension(1)->setRowHeight(30);  //高度    //$objActSheet->getColumnDimension(‘D‘)->setWidth(50);     //设置单元格的      //获得日期    $showtime=date("Y-m-d");        $objActSheet->setCellValue(‘A1‘, mb_convert_encoding($showtime.‘元器件库存清单‘, "utf-8", "GB2312"));        //合并单元\xBC       $objActSheet->mergeCells(‘A1:E1‘);        //设置标题单元格样      $objStyleA1 = $objActSheet->getStyle(‘A1‘);           $objStyleA1->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);       $objFontA1 = $objStyleA1->getFont();           $objFontA1->setName(‘宋体‘);           $objFontA1->setSize(18);         $objFontA1->setBold(true);          //设置表格标题栏内    $objActSheet->setCellValue(‘A2‘, ‘ID‘);    $objActSheet->setCellValue(‘B2‘, mb_convert_encoding(‘货品编码‘, "utf-8", "GB2312"));    $objActSheet->setCellValue(‘C2‘, mb_convert_encoding(‘仓库‘, "utf-8", "GB2312"));        $objActSheet->setCellValue(‘D2‘, mb_convert_encoding(‘货品名称‘, "utf-8", "GB2312"));        $objActSheet->setCellValue(‘E2‘, mb_convert_encoding(‘实存数‘, "utf-8", "GB2312"));    //$objActSheet->setCellValue(‘F2‘, mb_convert_encoding(‘货品编码‘, "utf-8", "GB2312"));        //设置表头 单元居中对齐       $objActSheet->getStyle(‘A2‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);    $objActSheet->getStyle(‘B2‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);    $objActSheet->getStyle(‘C2‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);    $objActSheet->getStyle(‘D2‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);    $objActSheet->getStyle(‘E2‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);    //$objActSheet->getStyle(‘F2‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);    //设置表头  单元\xBC 边框       $objActSheet->getStyle(‘A2‘)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );    $objActSheet->getStyle(‘B2‘)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );    $objActSheet->getStyle(‘C2‘)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );    $objActSheet->getStyle(‘D2‘)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );    $objActSheet->getStyle(‘E2‘)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );    //$objActSheet->getStyle(‘F2‘)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );/* * To change this template, choose Tools | Templates * and open the template in the editor. *///链接数据库服务器include "../../include/sql.php";$query = "SELECT ck_id,ck_num,ck_name,ck_num3 FROM rd_cangku";$result = mysql_query($query) or die("Query failed : " . mysql_error());$n=3;while ($line = mysql_fetch_array($result, MYSQL_NUM)) {    $m=‘A‘;        foreach ($line as $col_value) {                //表内\xB9 单元 居中        $objActSheet->getStyle($m.$n)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);                if($m==‘C‘){            $objActSheet->setCellValue($m.$n, mb_convert_encoding(‘元器件仓库‘, "utf-8", "GB2312"));            $m++;             }                                        //这里负责转码 excel默认是        $col_value = mb_convert_encoding($col_value, "utf-8", "GB2312");          $objActSheet->setCellValue($m.$n,$col_value );         $m++;                }    $n++;}        //输出内容           //$outputFileName =time().".xls";      $outputFileName ="cangku.xls";        //如果存在旧文件,则删    if (file_exists($outputFileName)) {        unlink ($outputFileName);  }        //到文           $objWriter->save($outputFileName);        //下面这个,属于链接,自己看嘛也可以<script>location=‘$outputFileName‘</script>
  //这个看自己需要嘛
echo("<a href=http://www.mamicode.com/‘$outputFileName‘ target=‘_blank‘>点击下载</a>");