Tuesday, 25 October 2016

How To Generate Reports In Excel In iReport


 Coding Untuk Button :




$path1=module_url("ireport/foldertest","test.php?p1={$variable1}&p2={$variable2}");
$url1='javascript:window.open("'.$path1.'","myreport");return false;';


 Coding Untuk File (test.php) :

include_once("../../mytools/excel/writer.php");
include_once("../../mytools/mytools.php");
include_once("../../../config.inc.php");
global $config_db;

/* Connecting, selecting database */
$link = mysql_connect($config_db["default"]["host"], $config_db["default"]["user"], $config_db["default"]["password"]) or die("Could not connect : " . mysql_error());
mysql_select_db($config_db["default"]["db"]) or die("Could not select database");

// Creating a workbook
$workbook = new Spreadsheet_Excel_Writer();

/* Sending HTTP headers, this will popup a file save/open
dialog in the browser when this file is run
*/
$workbook->send('test.xls');
$worksheet1 =& $workbook->addWorksheet('worksheet 1');

$header =& $workbook->addFormat();
$header->setBold();        // Make it bold
$header->setHAlign('center');    // Align text to center

$getvar1=$_GET['p1']; //variable 1
$getvar2=$_GET["p2"]; //variable 2

$strSQL="SELECT nokp, nama FROM tpegawai;"; // select field nokp and nama from table tpegawai
$record = doQuery($strSQL);

$worksheet1->write(0, 0, "TITLE",$header);
$worksheet1->mergeCells(0, 0, 0, 5);

$k=1;
#setheader

$setheader=array("BIL","NO.KAD PENGENALAN ","NAMA PEGAWAI"); // set column header

for ($j=0, $_j=count($setheader); $j<$_j;$j++){   
$worksheet1->write(3, $j, $setheader[$j], $header, $header2);
}

$bil=1;

#set size column
//function setColumn($firstcol, $lastcol, $width, $format = null, $hidden = 0, $level = 0)
$worksheet1->setColumn(1, 1, 14, null,0,0);
$worksheet1->setColumn(2, 5, 30, null,0,0);
$worksheet1->setColumn(9, 11, 30, null,0,0);
$worksheet1->setColumn(15, 15, 20, null,0,0);
$worksheet1->setColumn(16, 16, 20, null,0,0);

//set center pada kandungan
$worksheetformat =& $workbook->addFormat();
$worksheetformat->setHAlign('center');    // Align text to center
$worksheetformatleft =& $workbook->addFormat();
$worksheetformatleft->setHAlign('left');    // Align text to left

$i=0;
for ($i=0;$i<count($record);$i++){
   
    $setbil=$i+4;
    $worksheet1->write($setbil, 0, $i+1,$worksheetformat);
   
    if (strlen($record[$i]['nokp'])==12){
        $first=substr($record[$i]['nokp'],0,6);
        $second=substr($record[$i]['nokp'],6,2);
        $third=substr($record[$i]['nokp'],8,4);
        $nokp=$first."-".$second."-".$third;
    }else{
        $nokp=$record[$i]['nokp'];
    }
    $worksheet1->write($setbil, 1, $nokp,$worksheetleft);
    $worksheet1->write($setbil, 2, strtoupper($record[$i]['nama']),$worksheetformatleft);
}


function mysql_fetch_all($res) {
   while($row=mysql_fetch_array($res)) {
       $return[] = $row;
   }
   return $return;
}

function doQuery( $query, $res_key = NULL ){

$get_res = @mysql_query( $query ) or die(mysql_error());

if( $res = mysql_fetch_assoc( $get_res ) )
{
do{
if( $res_key !== NULL ){
$result[ $res[ $res_key ] ] = $res;
}else{
$result[] = $res;
};
}while( $res = mysql_fetch_assoc( $get_res ) );
}
else
{
$result = false;
};
return $result;
};

$workbook->close();
?>

~ che wahida ~

0 comments:

Post a Comment