 |
|
|
 |
 |
|
class PHPExcel
Code (PHP)
$old_sheet = '';
for($i=0; $i<15; $i++){
$sheet_ID = floor( $i / 5 );
if( $old_sheet !== $sheet_ID ){
$excel->setActiveSheetIndex( $sheet_ID );
$ln=1; $old_sheet=$sheet_ID;
}else $ln++;
$excel->getActiveSheet()->SetCellValue('A'.$ln, $i+1 );
}
|
 |
 |
 |
 |
Date :
2014-09-27 08:49:05 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ถ้าผมมีข้อมูลใน DB เป็นหลักร้อยๆ id แต่ผมต้องการให้แต่ละ sheet มีแค่ 5 id และให้ไปเพิ่มข้อมูลใน sheetใหม่ ต่อจาก id ของ sheet ก่อนหน้าครับ
สมมุติมีข้อมูล 200คน แล้ว export เป็น excal ก็จะได้ทั้งหมด 40sheet ในแต่ละ sheet มี 5คน ตามลำดับ
จะมีวิธีเขียนอย่างไรครับ คิดไม่ออกจริงๆครับ
|
ประวัติการแก้ไข 2014-09-27 09:00:16 2014-09-27 09:00:36 2014-09-27 09:03:24
 |
 |
 |
 |
Date :
2014-09-27 08:59:55 |
By :
youboom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณ คับ BIrD , Chaidhanan
|
 |
 |
 |
 |
Date :
2014-09-27 09:01:56 |
By :
youboom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวอย่างของผมเป็น Class PHPExcel ครับ ต้องมี class นี้อยู่ด้วย
สำหรับ ตัวคลาส หาโหลดได้จาก thaicreate นี่แหล่ะครับ เสริชหาดูครับ
ผมไม่ได้ใช้ new Com() นะครับ
Code (PHP)
require_once 'ClassExcel/PHPExcel.php';
include 'ClassExcel/PHPExcel/IOFactory.php';
include 'ClassExcel/PHPExcel/Writer/Excel2007.php';
$excel=new PHPExcel();
$excel->getProperties()->setCreator("Chaidhanan Pisalsenadhanachote");
$excel->getProperties()->setLastModifiedBy("Chaidhanan Pisalsenadhanachote");
$excel->getProperties()->setTitle("Report 5 rosyblue XLSX");
$excel->getProperties()->setSubject("Report 5 rosyblue");
$excel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
$old_sheet = '';
for($i=0; $i<15; $i++){
$sheet_ID = floor( $i / 5 );
if( $old_sheet !== $sheet_ID ){
$excel->setActiveSheetIndex( $sheet_ID );
$excel->getActiveSheet()->setTitle('report' . ($i+1) . '_'.($i+6));
$ln=1; $old_sheet=$sheet_ID;
}else $ln++;
$excel->getActiveSheet()->SetCellValue('A'.$ln, $i+1 );
}
$fileName = 'report_'.date('YmdHis').'.xlsx';
$objWriter = new PHPExcel_Writer_Excel2007($excel);
$objWriter->save($fileName);
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename=' . $fileName );
header('Content-Length: ' . filesize($fileName));
header('Content-Transfer-Encoding: binary');
ob_clean();
flush();
readfile($fileName);
unlink($fileName);
|
ประวัติการแก้ไข 2014-09-27 13:06:08
 |
 |
 |
 |
Date :
2014-09-27 13:03:44 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาโค๊ดของ จขกท มาดัดแปลง ผิดพลาดประการใดขออภัยไว้ด้วย ไม่เคยใช้ครับ
Code (PHP)
<html>
<head>
<title>Report Excel.Application</title>
<meta http-equiv="Content-Type" content="text/html; charset=tis-620">
</head>
<body>
<?php
require('conn_mysql_sd.php');
$strSQL = "SELECT * FROM data_sd";
mysql_query("SET NAMES TIS620");
$objQuery = mysql_query($strSQL);
if($objQuery)
{
//*** Get Document Path ***//
$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"])));
//*** Excel Document Root ***//
$strFileName = "MyXls/MyExcel.xlsx";
//*** Connect to Excel.Application ***//
$xlApp = new COM("Excel.Application");
$xlBook = $xlApp->Workbooks->Add();
$old_sheet=0; $item=0;
while($objResult = mysql_fetch_array($objQuery))
{
$sheet_ID = floor( $item / 5 ); $item++;
if($old_sheet !=$sheet_ID){
$xlBook->Worksheets( $sheet_ID )->Select;
//*** Width & Height ***//
$xlApp->ActiveSheet->Range("A1:A1")->ColumnWidth = 5.57;
$xlApp->ActiveSheet->Range("B1:B1")->ColumnWidth = 5.29;
$xlApp->ActiveSheet->Range("C1:C1")->ColumnWidth = 8.14;
$xlApp->ActiveSheet->Range("D1:D1")->ColumnWidth = 11.29;
$old_sheet=$sheet_ID; $intRows=3;
}else $intRows++;
//*** Detail ***//
$xlApp->ActiveSheet->Cells($intRows,1)->Value = $objResult["id"];
$xlApp->ActiveSheet->Cells($intRows,2)->Value = $objResult["name"];
$xlApp->ActiveSheet->Cells($intRows,3)->Value = $objResult["surname"];
$xlApp->ActiveSheet->Cells($intRows,4)->Value = $objResult["province"];
}
@unlink($strFileName); //*** Delete old files ***//
$xlBook->SaveAs($strPath."/".$strFileName); //*** Save to Path ***//
//$xlBook->SaveAs(realpath($strFileName)); //*** Save to Path ***//
//*** Close & Quit ***//
$xlApp->Application->Quit();
$xlApp = null;
$xlBook = null;
$xlSheet1 = null;
}
mysql_free_result($objQuery);
mysql_close($conn);
?>
Excel Created <a href="<?php echo $strFileName?>">Click here</a> to Download.
</body>
</html>
|
 |
 |
 |
 |
Date :
2014-09-27 13:20:01 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
while($objResult = mysql_fetch_array($objQuery))
{
$sheet_ID = floor( $item / 5 ); $item++;
if($old_sheet !=$sheet_ID){
if($sheet_ID>3) $xlBook->Sheets->Add; //*** Sheet Default=3 Add New 1 Sheet=4 ***//
|
 |
 |
 |
 |
Date :
2014-09-28 09:57:35 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ PHPExcel ดีกว่า Excel.Application น่ะครับ
|
 |
 |
 |
 |
Date :
2014-09-28 10:05:10 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
com('Excel.application') มันรันได้ตาม version ของ excel ที่ install เอาไว้
แต่ รันได้บน window server เท่านั้น
PHPExcel ตอนนี้ใช้ได้แค่ excel 2007 เท่านั้น แต่ก็ใช้บน linux ได้ด้วย
ถ้าใช้ 2010 ก็ต้องเซฟ เป็น 2007 ก่อน ถึงจะใช้งานได้ โดยรวมแล้ว ถือว่าได้ประโยชน์มากกว่า
แต่เลือกใช้เอาตาม environment ของเครื่อง ดีกว่าครับ
|
 |
 |
 |
 |
Date :
2014-09-28 10:23:18 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณ สำหรับคำแนะนำครับ 
|
 |
 |
 |
 |
Date :
2014-09-30 08:37:06 |
By :
youboom |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|