แสดงบทความที่มีป้ายกำกับ set thai font แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ set thai font แสดงบทความทั้งหมด

วันอาทิตย์ที่ 25 พฤศจิกายน พ.ศ. 2555

php excel class

<?php
$a = array('A','B','C');
$fs = array('field1','field2','field3');
$cs = array(
    array('aaa1','bbb1','ccc1ทดสอบ'),
    array('aaa2','bbb2','ccc2'),
    array('aaa3','bbb3','ccc3'),
);
require_once './excel/PHPExcel.php';
$e = new PHPExcel();
$e->getProperties()->setCreator("num")
        ->setLastModifiedBy("num")
        ->setTitle("Report")
        ->setSubject("Report")
        ->setDescription("Report")
        ->setKeywords("report")
        ->setCategory("report");

$default_border = array(
    'style' => PHPExcel_Style_Border::BORDER_THIN,
    'color' => array('rgb'=>'1006A3')
);
$style_header = array(
    'borders' => array(
        'bottom' => $default_border,
        'left' => $default_border,
        'top' => $default_border,
        'right' => $default_border,
    ),
    'fill' => array(
        'type' => PHPExcel_Style_Fill::FILL_SOLID,
        'color' => array('rgb'=>'E1E0F7'),
    ),
    'font' => array(
        'bold' => true,
        'name' => 'CordiaUPC',
        'size' => 30,
    )
);
$sh = $e->setActiveSheetIndex(0);
$sh->getStyle('A1:C4')->applyFromArray( $style_header );
$sh->getColumnDimension('C')->setWidth(20);
foreach($fs as $i=>$f)
        $sh->setCellValue($a[$i].'1', $f);
$row = 2;
foreach($cs as $c){
        $vals = array_values($c);
        foreach($fs as $i=>$f)
                $sh->setCellValue($a[$i].$row, $vals[$i]);
        $row++;
}
$e->getActiveSheet()->setTitle('Report');
$e->setActiveSheetIndex(0);
$writer = PHPExcel_IOFactory::createWriter($e, 'Excel5');
$writer->save('test.xls');
?>



PHPExcel.php ดาวนโหลดได้ที่ http://phpexcel.codeplex.com/

วันพฤหัสบดีที่ 14 ตุลาคม พ.ศ. 2553

set folder thai font ให้กับ jpgraph เพื่อให้แสดง font ภาษาไทย

สามารถ Download นำไฟล์ไปทดสอบได้ทันที (ไฟล์ thai_001.php และ thai_002.php)
Download
ถ้าไม่สามารถใช้งานได้แสดงว่ามีปัญหาเกี่ยวกับ configuration ของ php ในไฟล์ php.ini ละครับ

jpgraph_ttf.inc.php
/* Placeholders for defined fonts */
FF_USERFONT1 => array(
FS_NORMAL =>'CORDIA.TTF',
FS_BOLD =>'CORDIA.TTF',
FS_ITALIC =>'CORDIA.TTF',
FS_BOLDITALIC =>'CORDIA.TTF' ),

========
ไฟล์แสดง graph

จุดสำคัญคือตรงนี้ครับสมมุติว่าไฟล์ font *.ttf อยู่ใน folder scripts ซึ่งอยู่ระดับรองจากไฟล์โค้ด php
และอย่าลืมว่า string ภาษาไทย ที่ส่งไปให้กับ function ของ jpgraph จะต้องเป็น utf8

define('TTF_DIR',dirname(__FILE__).'/scripts/'); //กำหนดตำแหน่ง folder ที่เก็บ font ttf
require_once ('../scripts/jpgraph/jpgraph/jpgraph.php');
require_once ('../scripts/jpgraph/jpgraph/jpgraph_bar.php');



... โค้ดบางส่วนถูกตัดทอนออกไป

$graph = new Graph(550,320);
$graph->img->ttf->SetUserFont1('CODIA.TTF','CODIA.TTF','CODIA.TTF','CODIA.TTF'); //เทียบเท่ากับกำหนดค่าใน jpgraph_ttf.inc.php
$graph->SetScale('intint');
$graph->SetShadow();
$graph->SetMargin(60,30,40,160);
$bplot = new BarPlot($dataY);
$bplot->SetWidth(0.2);
$bplot->SetFillColor('orange');
$graph->Add($bplot);
$graph->title->Set('การแจ้งซ่อม : จำแนกตามสาขา : '.range_display($start,$end));
$ttf = FF_USERFONT1;
$graph->title->SetFont($ttf,FS_NORMAL,14);
$graph->yaxis->title->SetFont($ttf,FS_NORMAL,14);
$graph->xaxis->title->SetFont($ttf,FS_NORMAL,14);
$graph->xaxis->SetFont($ttf,FS_NORMAL,14);
$graph->xaxis->SetTickLabels($labelY);
$graph->xaxis->SetLabelAngle(70);
$graph->xaxis->HideLastTickLabel();


link: โค้ดช่วยแสดง object ของ jpgraph ที่สามารถ set font ได้