Wednesday, April 1, 2015

Export PHP To Excel With PHPExcel II (Font)

Sikarang kita bikin export PHP ke Excel lagi. Bedanyo dari sebelumnya (export PHP Excel sederhana) adalah menambah style font nya. Yuk, buat filenya, sekarang namanya excelb.php. Nanti yang file yang dibuat excelb.xls.

Sama seperti sebelumnya. Tulis kode seperti ini di excelb.php:

error_reporting(E_ALL);
ini_set('include_path', ini_get('include_path').';PHPExcel/');
include 'PHPExcel.php';
include 'PHPExcel/Writer/Excel2007.php';
$objPHPExcel = new PHPExcel();
$fonta= new PHPExcel_Style_Font();
$fonta->setName('Arial');
$fonta->setSize(12);
$objPHPExcel->getProperties()->setCreator("Creator");
$objPHPExcel->getProperties()->setLastModifiedBy("Modificator");
$objPHPExcel->getProperties()->setTitle("Title");
$objPHPExcel->getProperties()->setSubject("Subject");
$objPHPExcel->getProperties()->setDescription("Description");
$objPHPExcel->setActiveSheetIndex(0);
$sheet=$objPHPExcel->getActiveSheet();
$margin=$sheet->getPageMargins();
$mleft= 1.5/2.54;
$mright= 0.6/2.54;
$mtop= 4.5/2.54;
$mbottom= 1.9/2.54;
$margin->setTop($mtop);
$margin->setBottom($mbottom);
$margin->setLeft($mleft);
$margin->setRight($mright);
$objPHPExcel->getActiveSheet(0)->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
$objPHPExcel->getActiveSheet(0)->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
$objPHPExcel->getActiveSheet(0)->getPageSetup()->setFitToPage(true);
$objPHPExcel->getActiveSheet(0)->getPageSetup()->setFitToWidth(1);
$objPHPExcel->getActiveSheet(0)->getPageSetup()->setFitToHeight(0);
$objPHPExcel->getActiveSheet(0)->getColumnDimension('A')->setWidth(10);
$objPHPExcel->getActiveSheet(0)->getColumnDimension('B')->setWidth(35);
$objPHPExcel->getActiveSheet(0)->getColumnDimension('C')->setWidth(30);
$objPHPExcel->getActiveSheet(0)->getColumnDimension('D')->setWidth(20);
$objPHPExcel->getActiveSheet(0)->getColumnDimension('E')->setWidth(20);

Nah setelah itu bikin font style nya seperti ginian:

//Untuk mempertebal tulisan (Bold)
$objPHPExcel->getActiveSheet(0)->getStyle('A2')->getFont()->setBold(true);
//Untuk jenis tulisan dan ukurannya
$objPHPExcel->getActiveSheet(0)->getStyle('B3')->getFont()->setName('Tahoma')->setSize(14);
//Untuk warna tulisan
$objPHPExcel->getActiveSheet(0)->getStyle('C4')->getFont()->getColor()->setRGB('000099');
//Untuk ukuran tulisan dan nambah garis bawah (underline)
$objPHPExcel->getActiveSheet(0)->getStyle('D5')->getFont()->setSize(16)->setUnderline(true);
//Ini gabungan style semua
$objPHPExcel->getActiveSheet(0)->getStyle('E6')->getFont()->setName('Arial')->setSize(18)->setBold(true)->setUnderline(true)->getColor()->setRGB('999900');


Sikarang, tulis di setiap cell nya...

$objPHPExcel->getActiveSheet(0)->SetCellValue('A2', 'Convert');
$objPHPExcel->getActiveSheet(0)->SetCellValue('B3', 'PHP To');
$objPHPExcel->getActiveSheet(0)->SetCellValue('C4', 'Excel');
$objPHPExcel->getActiveSheet(0)->SetCellValue('D5', 'With');
$objPHPExcel->getActiveSheet(0)->SetCellValue('E6', 'PHPExcel');


Bisa juga dibalik. Nulis kode buat isi cell nya terlebih dahulu lalu kode untuk stylenya... Hasilnya sama aja.

Lha terus bikin kode untuk convert:

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="ExcelB.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');  
$objWriter->save('php://output');
exit;

Gimpink bingitz kan... Sekian dulu, nanti nyambung lagi dengan yang laen... Makaciiii...

Monday, March 23, 2015

Export PHP To Excel With PHPExcel

Untuk mengubah ato mengconvert ato menyulap (hehehe) kode PHP ke MS Excel, banyak caranya. Salah satunya dan yang paling mudah dengan bantuan plugin PHPExcel. Sebelumnya download dulu file PHPExcel disini (https://phpexcel.codeplex.com/releases/view/119187). Setelah itu extract file tersebut.

Misalken, kita buat folder exc di htdocs. Taruh file PHPExcel.php dan folder PHPExcel. Dan kita buat file php nya: excel.php

Tulis kode di bawah ini (buat masukin/include/manggil class PHPExcel) di file excel.php:

error_reporting(E_ALL);ini_set('include_path', ini_get('include_path').';PHPExcel/'); 
include 'PHPExcel.php'; 
include 'PHPExcel/Writer/Excel2007.php';

Terusss, tulis lagi kode di bawah ini, untuk manggil class PHPExcel dan bikin font style globalnya:


$objPHPExcel = new PHPExcel(); 
$fonta= new PHPExcel_Style_Font();  
$fonta->setName('Arial');$fonta->setSize(12);  
//yang bikin dokumen ini 
$objPHPExcel->getProperties()->setCreator("Creator"); 
//yang ngubah paling terakhir 
$objPHPExcel->getProperties()->setLastModifiedBy("Modificator"); 
//judul 
$objPHPExcel->getProperties()->setTitle("Title"); 
//subjeknyo 
$objPHPExcel->getProperties()->setSubject("Subject"); 
//deskripsi 
$objPHPExcel->getProperties()->setDescription("Description");

Sekarang, bikin nomer sheet nya (jadi bisa bikin beberapa sheet). Misalnyo sheet 0. Dan atur page setupnya:

$objPHPExcel->setActiveSheetIndex(0); 
$sheet=$objPHPExcel->getActiveSheet(); 
$margin=$sheet->getPageMargins(); 
$mleft= 1.5/2.54; 
$mright= 0.6/2.54; 
$mtop= 4.5/2.54; 
$mbottom= 1.9/2.54; 
$margin->setTop($mtop);  
$margin->setBottom($mbottom); 
$margin->setLeft($mleft); 
$margin->setRight($mright);

Atur page setupnya kalo kepengen lebih lengkap. Contohnye halaman itu landscape ato potrait dll:

$objPHPExcel->getActiveSheet(0)->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT); 
$objPHPExcel->getActiveSheet(0)->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4); 
$objPHPExcel->getActiveSheet(0)->getPageSetup()->setFitToPage(true); 
$objPHPExcel->getActiveSheet(0)->getPageSetup()->setFitToWidth(1); 
$objPHPExcel->getActiveSheet(0)->getPageSetup()->setFitToHeight(0);

Atur lebarnya column. Misalken column A lebarnya 10 px, dst:

$objPHPExcel->getActiveSheet(0)->getColumnDimension('A')->setWidth(10); 
$objPHPExcel->getActiveSheet(0)->getColumnDimension('B')->setWidth(35); 
$objPHPExcel->getActiveSheet(0)->getColumnDimension('C')->setWidth(30); 
$objPHPExcel->getActiveSheet(0)->getColumnDimension('D')->setWidth(20); 
$objPHPExcel->getActiveSheet(0)->getColumnDimension('E')->setWidth(20);

Tulis susuatu di column A:
$objPHPExcel->getActiveSheet(0)->SetCellValue('A2', 'Percobaan');
Lanjutkan!!! Ke column berikutnya:
$objPHPExcel->getActiveSheet(0)->SetCellValue('B3', 'PHP To'); 
$objPHPExcel->getActiveSheet(0)->SetCellValue('C4', 'Excel'); 
$objPHPExcel->getActiveSheet(0)->SetCellValue('D5', 'Dengan'); 
$objPHPExcel->getActiveSheet(0)->SetCellValue('E6', 'PHPExcel');


Setelah itu semua, mari kita bikin agar saat kita manggil localhost/exc/excel.php agar langsung dapet file download Excel.xls:

header('Content-Type: application/vnd.ms-excel'); 
header('Content-Disposition: attachment;filename="Excel.xls"'); 
header('Cache-Control: max-age=0');   
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); 
$objWriter->save('php://output'); 
exit;




Wednesday, March 18, 2015

Sugar - Maroon 5

I'm hurting, baby, I'm broken down
I need your loving, loving
I need it now
When I'm without you
I'm something weak
You got me begging, begging
I'm on my knees

I don't wanna be needing your love
I just wanna be deep in your love
And it's killing me when you're away, ooh, baby,
Cause I really don't care where you are
I just wanna be there where you are
And I gotta get one little taste

Sugar
Yes, please
Won't you come and put it down on me
I'm right here, 'cause I need
Little love and little sympathy
Yeah, you show me good loving
Make it alright
Need a little sweetness in my life
Sugar
Yes, please
Won't you come and put it down on me

My broken pieces
You pick them up
Don't leave me hanging, hanging
Come give me some
When I'm without ya
I'm so insecure
You are the one thing, one thing
I'm living for

I don't wanna be needing your love
I just wanna be deep in your love
And it's killing me when you're away, ooh, baby,
Cause I really don't care where you are
I just wanna be there where you are
And I gotta get one little taste

Sugar
Yes, please
Won't you come and put it down on me
I'm right here
Cause I need
Little love and little sympathy
Yeah, you show me good loving
Make it alright
Need a little sweetness in my life
Sugar! (Sugar!)
Yes, please (Yes, please)
Won't you come and put it down on me

Yeah
I want that red velvet
I want that sugar sweet
Don't let nobody touch it
Unless that somebody's me
I gotta be a man
There ain't no other way
Cause girl you're hotter than a southern California day

I don't wanna play no games
I don't gotta be afraid
Don't give me all that shy shit
No make-up on
That's my

Sugar
Yes, please
Won't you come and put it down on me (down on me!)
I'm right here (right here), 'cause I need ('cause I need)
Little love and little sympathy
So, baby, you show me good loving
Make it alright
Need a little sweetness in my life
Sugar! (Sugar!)
Yes, please (Yes, please)
Won't you come and put it down on me

Sugar
Yes, please
Won't you come and put it down on me
I'm right here, cause I need
Little love and little sympathy
Yeah, you show me good loving
Make it alright
Need a little sweetness in my life
Sugar
Yes, please
Won't you come and put it down on me

Blank Space - Taylor Swift

Nice to meet you, where you been
I could show you incredible things
Magic, madness, heaven, sin
Saw you there and I thought
Oh my God, look at that face
You look like my next mistake
Love's a game, wanna play

New money, suit and tie
I can read you like a magazine
Ain't it funny, rumors fly
And I know you heard about me
So hey, let's be friends
I'm dying to see how this one ends
Grab your passport and my hand
I can make the bad guys good for a weekend

So it's gonna be forever
Or it's gonna go down in flames
You can tell me when it's over
If the high was worth the pain
Got a long list of ex-lovers
They'll tell you I'm insane
'Cause you know I love the players
And you love the game

Cause we're young and we're reckless
We'll take this way too far
It'll leave you breathless
Or with a nasty scar
Got a long list of ex-lovers
They'll tell you I'm insane
But I've got a blank space, baby
And I'll write your name

Cherry lips, crystal skies
I could show you incredible things
Stolen kisses, pretty lies
You're the King, baby, I'm your Queen
Find out what you want
Be that girl for a month
Wait, the worst is yet to come, oh no

Screaming, crying, perfect storms
I can make all the tables turn
Rose garden filled with thorns
Keep you second guessing like
Oh my God, who is she
I get drunk on jealousy
But you'll come back each time you leave
Cause, darling, I'm a nightmare dressed like a daydream

So it's gonna be forever
Or it's gonna go down in flames
You can tell me when it's over
If the high was worth the pain
Got a long list of ex-lovers
They'll tell you I'm insane
'Cause you know I love the players
And you love the game

Cause we're young and we're reckless
We'll take this way too far
It'll leave you breathless
Or with a nasty scar
Got a long list of ex-lovers
They'll tell you I'm insane
But I've got a blank space, baby
And I'll write your name

Boys only want love if it's torture
Don't say I didn't say, I didn't warn ya
Boys only want love if it's torture
Don't say I didn't say, I didn't warn ya

So it's gonna be forever
Or it's gonna go down in flames
You can tell me when it's over
If the high was worth the pain
Got a long list of ex-lovers
They'll tell you I'm insane
'Cause you know I love the players
And you love the game

Cause we're young and we're reckless
We'll take this way too far
It'll leave you breathless
Or with a nasty scar
Got a long list of ex-lovers
They'll tell you I'm insane
But I've got a blank space, baby
And I'll write your name

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Web Hosting