2012-09-26 17 views
16

का उपयोग कर एक्सेल फ़ाइल में डेटा को निर्यात करने के लिए कैसे मैंने limesurvey से स्रोत कोड लिया है और एक लिंक पर क्लिक करने के बाद एक्सेल फ़ाइल में डेटा निर्यात करने के लिए PHP lxurvey कोड में PHPExcel लाइब्रेरी को जोड़ा है। वर्तमान में एक्सेल फ़ाइल कुछ डमी डेटा के साथ खुलती है जिसमें कोई समस्या नहीं है। सर्वेक्षण जानकारी में उपयोगकर्ता प्रकार के बाद मुझे वेब सर्वर से गतिशील रूप से डेटा जोड़ने में सक्षम होना चाहिए। मैंने कुछ साइटों को देखा है जो मुझे मिला है लेकिन मुझे बहुत भाग्य मिला है। क्या कोई मेरी मदद कर सकता है?PHPExcel

संपादित

<?php 
$dbhost= "mysql"; //your MySQL Server 
$dbuser = "survey"; //your MySQL User Name 
$dbpass = "password"; //your MySQL Password 
$dbname = "database"; 
//your MySQL Database Name of which database to use this 
$tablename = "questions"; //your MySQL Table Name which one you have to create excel file 
// your mysql query here , we can edit this for your requirement 
$sql = "Select * from $table "; 
//create code for connecting to mysql 
$Connect = @mysql_connect($dbhost, $dbuser, $dbpass) 
or die("Couldn't connect to MySQL:<br>" . mysql_error() . "<br>" . mysql_errno()); 
//select database 
$Db = @mysql_select_db($dbname, $Connect) 
or die("Couldn't select database:<br>" . mysql_error(). "<br>" . mysql_errno()); 
//execute query 
$result = @mysql_query($sql,$Connect) 
or die("Couldn't execute query:<br>" . mysql_error(). "<br>" . mysql_errno()); 

error_reporting(E_ALL); 

require_once '../Classes/PHPExcel.php'; 
$objPHPExcel = new PHPExcel(); 

// Set the active Excel worksheet to sheet 0 

$objPHPExcel->setActiveSheetIndex(0); 

// Initialise the Excel row number 

$rowCount = 1; 


//start of printing column names as names of MySQL fields 

$column = 'A'; 

for ($i = 1; $i < mysql_num_fields($result); $i++) 

{ 
    $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, mysql_field_name($result,$i)); 
    $column++; 
} 

//end of adding column names 
//start while loop to get data 

$rowCount = 2; 

while($row = mysql_fetch_row($result)) 

{ 
    $column = 'A'; 

    for($j=1; $j<mysql_num_fields($result);$j++) 
    { 
     if(!isset($row[$j])) 

      $value = NULL; 

     elseif ($row[$j] != "") 

      $value = strip_tags($row[$j]); 

     else 

      $value = ""; 


     $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, $value); 
     $column++; 
    } 

    $rowCount++; 
} 

// Redirect output to a client’s web browser (Excel5) 
header('Content-Type: application/vnd.ms-excel'); 
header('Content-Disposition: attachment;filename="results.xls"'); 
header('Cache-Control: max-age=0'); 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
$objWriter->save('php://output'); 

उत्तर

35

आप इस कॉपी किया गया है सीधे, तो:

->setCellValue('B2', Ackermann') 

->setCellValue('B2', 'Ackermann') 

जवाब में अपने प्रश्न का होना चाहिए:

जो डेटा आप limesurvey से चाहते हैं उसे प्राप्त करें, और उन सेल मानों को संग्रहीत करने के लिए setCellValue() का उपयोग करें जहां आप इसे स्टोर करना चाहते हैं।

Quadratic.php उदाहरण फ़ाइल/टेस्ट में फ़ाइल प्रारंभिक बिंदु के रूप में मदद कर सकती है: यह इनपुट फॉर्म से डेटा लेता है और इसे Excel कार्यपुस्तिका में कक्षों में सेट करता है।

संपादित

एक बेहद साधारण उदाहरण:

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

// 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); 
// Initialise the Excel row number 
$rowCount = 1; 
// Iterate through each result from the SQL query in turn 
// We fetch each database result row into $row in turn 
while($row = mysql_fetch_array($result)){ 
    // Set cell An to the "name" column from the database (assuming you have a column called name) 
    // where n is the Excel row number (ie cell A1 in the first row) 
    $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['name']); 
    // Set cell Bn to the "age" column from the database (assuming you have a column called age) 
    // where n is the Excel row number (ie cell A1 in the first row) 
    $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['age']); 
    // Increment the Excel row counter 
    $rowCount++; 
} 

// Instantiate a Writer to create an OfficeOpenXML Excel .xlsx file 
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
// Write the Excel file to filename some_excel_file.xlsx in the current directory 
$objWriter->save('some_excel_file.xlsx'); 

संपादित करें # 2

आधार

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

//start of printing column names as names of MySQL fields 
$column = 'A'; 
for ($i = 1; $i < mysql_num_fields($result); $i++) 
{ 
    $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, mysql_field_name($result,$i)); 
    $column++; 
} 
//end of adding column names 

//start while loop to get data 
$rowCount = 2; 
while($row = mysql_fetch_row($result)) 
{ 
    $column = 'A'; 
    for($j=1; $j<mysql_num_fields($result);$j++) 
    { 
     if(!isset($row[$j])) 
      $value = NULL; 
     elseif ($row[$j] != "") 
      $value = strip_tags($row[$j]); 
     else 
      $value = ""; 

     $objPHPExcel->getActiveSheet()->setCellValue($column.$rowCount, $value); 
     $column++; 
    } 
    $rowCount++; 
} 


// Redirect output to a client’s web browser (Excel5) 
header('Content-Type: application/vnd.ms-excel'); 
header('Content-Disposition: attachment;filename="Limesurvey_Results.xls"'); 
header('Cache-Control: max-age=0'); 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 
$objWriter->save('php://output'); 
+0

मार्क से उत्कृष्टता फ़ाइल, मैं द्विघात को देखा .php और मुझे HTML से बजाय MYSQL से मान निकालने की आवश्यकता है। क्षमा करें मैंने स्पष्ट नहीं किया। क्या इसके लिए कोई शुरुआती बिंदु है? –

+0

MySQL से मान निकालने का डेटाबेस क्वेरी निष्पादित करने, पंक्तियों के माध्यम से पुनरावृत्ति करने और सेल में प्रत्येक पंक्ति/कॉलम मान को संग्रहीत करने का एक मामला –

+0

मार्क है, मैंने अपना कोड संपादित किया है। मुझे यकीन नहीं है कि एक्सेल करने के लिए आउटपुट करने के लिए PHPExcel कोड फिट करने के लिए इस कोड को कैसे बदला जाए। –

9

रूप में अपने मौजूदा कोड का उपयोग करना एक ही

<?php 
    $objPHPExcel = new PHPExcel(); 
    $query1 = "SELECT * FROM employee"; 
    $exec1 = mysql_query($query1) or die ("Error in Query1".mysql_error()); 
    $serialnumber=0; 
    //Set header with temp array 
    $tmparray =array("Sr.Number","Employee Login","Employee Name"); 
    //take new main array and set header array in it. 
    $sheet =array($tmparray); 

    while ($res1 = mysql_fetch_array($exec1)) 
    { 
    $tmparray =array(); 
    $serialnumber = $serialnumber + 1; 
    array_push($tmparray,$serialnumber); 
    $employeelogin = $res1['employeelogin']; 
    array_push($tmparray,$employeelogin); 
    $employeename = $res1['employeename']; 
    array_push($tmparray,$employeename); 
    array_push($sheet,$tmparray); 
    } 
    header('Content-type: application/vnd.ms-excel'); 
    header('Content-Disposition: attachment; filename="name.xlsx"'); 
    $worksheet = $objPHPExcel->getActiveSheet(); 
    foreach($sheet as $row => $columns) { 
    foreach($columns as $column => $data) { 
     $worksheet->setCellValueByColumnAndRow($column, $row + 1, $data); 
    } 
    } 

    //make first row bold 
    $objPHPExcel->getActiveSheet()->getStyle("A1:I1")->getFont()->setBold(true); 
    $objPHPExcel->setActiveSheetIndex(0); 
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); 
    $objWriter->save(str_replace('.php', '.xlsx', __FILE__)); 
?> 
0

मैं वर्तमान में डाउनलोड करने के लिए googling की एक श्रृंखला के बाद अपने प्रोजेक्ट में इस सुविधा का उपयोग करने के लिए नीचे दिए गए पूर्ण उदाहरण प्रयास करें एसक्यूएल बयान

// $sql = sql query e.g "select * from mytablename" 
    // $filename = name of the file to download 
     function queryToExcel($sql, $fileName = 'name.xlsx') { 
       // initialise excel column name 
       // currently limited to queries with less than 27 columns 
     $columnArray = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"); 
       // Execute the database query 
       $result = mysql_query($sql) or die(mysql_error()); 

       // Instantiate a new PHPExcel object 
       $objPHPExcel = new PHPExcel(); 
       // Set the active Excel worksheet to sheet 0 
       $objPHPExcel->setActiveSheetIndex(0); 
       // Initialise the Excel row number 
       $rowCount = 1; 
    // fetch result set column information 
       $finfo = mysqli_fetch_fields($result); 
// initialise columnlenght counter     
$columnlenght = 0; 
       foreach ($finfo as $val) { 
// set column header values     
    $objPHPExcel->getActiveSheet()->SetCellValue($columnArray[$columnlenght++] . $rowCount, $val->name); 
       } 
// make the column headers bold 
       $objPHPExcel->getActiveSheet()->getStyle($columnArray[0]."1:".$columnArray[$columnlenght]."1")->getFont()->setBold(true); 

       $rowCount++; 
       // Iterate through each result from the SQL query in turn 
       // We fetch each database result row into $row in turn 

       while ($row = mysqli_fetch_array($result, MYSQL_NUM)) { 
        for ($i = 0; $i < $columnLenght; $i++) { 
         $objPHPExcel->getActiveSheet()->SetCellValue($columnArray[$i] . $rowCount, $row[$i]); 
        } 
        $rowCount++; 
       } 
// set header information to force download 
       header('Content-type: application/vnd.ms-excel'); 
       header('Content-Disposition: attachment; filename="' . $fileName . '"'); 
       // Instantiate a Writer to create an OfficeOpenXML Excel .xlsx file   
       // Write the Excel file to filename some_excel_file.xlsx in the current directory     
       $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
       // Write the Excel file to filename some_excel_file.xlsx in the current directory 
       $objWriter->save('php://output'); 
      } 
1
$this->load->library('excel'); 
$file_name = 'Demo'; 
$arrHeader = array('Name', 'Mobile'); 
$arrRows = array(0=>array('Name'=>'Jayant','Mobile'=>54545), 1=>array('Name'=>'Jayant1', 'Mobile'=>44454), 2=>array('Name'=>'Jayant2','Mobile'=>111222), 3=>array('Name'=>'Jayant3', 'Mobile'=>99999)); 
$this->excel->getActiveSheet()->fromArray($arrHeader,'','A1'); 
$this->excel->getActiveSheet()->fromArray($arrRows); 
header('Content-Type: application/vnd.ms-excel'); //mime type 
header('Content-Disposition: attachment;filename="'.$file_name.'"'); //tell browser what's the file name 
header('Cache-Control: max-age=0'); //no cache 
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5'); 
$objWriter->save('php://output'); 
+1

हालांकि यह कोड समस्या को हल करने में मदद कर सकता है, यह _why_ और/या _how_ को प्रश्न का उत्तर नहीं देता है। इस अतिरिक्त संदर्भ को प्रदान करने से इसके दीर्घकालिक शैक्षणिक मूल्य में काफी सुधार होगा। स्पष्टीकरण जोड़ने के लिए कृपया अपना उत्तर संपादित करें, जिसमें सीमाएं और मान्यताओं को लागू किया गया है। –