dtvnbe1428 2014-06-06 22:52
浏览 48
已采纳

NO数据使用PHPExcel库加载到Excel文件中

What I need:

  1. on click of Export Tab Data from Database is loaded in Excel file in format such as csv , xls etc..

My Problem :

  • when Execute file No Data is loaded in Excel File .

here is my code snippet :

  error_reporting(E_ALL);
     ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    date_default_timezone_set('UTC');

    mysql_connect('localhost','root','') or die('Cannot connect to database !');
    mysql_select_db("atesting_ess") or die('No database found in mysql !');

   if (PHP_SAPI == 'cli')
    die('This example should only be run from a Web Browser');

        /** Include PHPExcel */
     require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';

         // Create your database query
       $query = "SELECT * FROM asana_tasks";  

       // Execute the database query
        $result = mysql_query($query) or die(mysql_error());

       // Instantiate a new PHPExcel object
       $objPHPExcel = new PHPExcel(); 
      // Set the active Excel worksheet to sheet 0
      $objPHPExcel->setActiveSheetIndex(0); 

        $rowCount = 1; 
        while($row = mysql_fetch_array($result)){ 
    //print_r($row);
    $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A'.$rowCount, $row['Projects_name'])
                ->SetCellValue('B'.$rowCount, $row['Name']) 
                 ->SetCellValue('C'.$rowCount, $row['Priority']); 

             $rowCount++; 
           } 
       // Redirect output to a client?s web browser (Excel2007)
     header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
     header('Content-Disposition: attachment;filename="01simple.xlsx"'); 
     header('Cache-Control: max-age=0');
     // If you're serving to IE 9, then the following may be needed
     header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
  1. i have tried t resolve the by adding dummy data its working Fine code:

       // Add some data
         $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', 'Hello')
        ->setCellValue('B2', 'world!')
        ->setCellValue('C1', 'Hello')
        ->setCellValue('D2', 'world!');
    

展开全部

  • 写回答

1条回答 默认 最新

  • douzhuijing4911 2014-06-06 23:16
    关注

    Since column is A,B,C etc. and not 1,2,3, etc. convert it to a character. The code below works for columns A to Z, for AA,AB etc, this needs to be extended.

    $columnvalue = strtoupper(chr($col) + 96));
    $objPHPExcel->getActiveSheet()->setCellValue($columnvalue . $row, $value);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部