excel as database android code example

Example: download mysql database to excel in android studio

<?php

$dbhost= "localhost"; //your MySQL Server 
$dbuser = "root"; //your MySQL User Name 
$dbpass = ""; //your MySQL Password 
$dbname = "smkkimmanuel2"; 

//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 pendaftaran "; 
//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()); 

error_reporting(E_ALL);

// Create your database query
$query = "SELECT * FROM pendaftaran";  
// Execute the database query
$result = mysql_query($query) or die(mysql_error());

// Instantiate a new PHPExcel object
 require_once '/PHPExcel_1.8.0_doc/Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel(); 
// Set the active Excel worksheet to sheet 0
$objPHPExcel->setActiveSheetIndex(0); 
// Set document properties
$objPHPExcel->getProperties()->setCreator("Wanda")
			->setLastModifiedBy("Wanda")
			->setTitle("Office 2007 XLSX Test Document")
			->setSubject("Office 2007 XLSX Test Document")
			->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
			->setKeywords("office 2007 openxml php")
			->setCategory("Test result file");


// 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)){ 
    $objPHPExcel->getActiveSheet()->SetCellValue('A'.$rowCount, $row['no_pendaftaran']); 
    $objPHPExcel->getActiveSheet()->SetCellValue('B'.$rowCount, $row['jurusan']); 
    // Increment the Excel row counter
    $rowCount++; 
} 

// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="daftar siswa mendaftar.xls"');
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');


$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;

Tags:

Sql Example