ช่วยดูให้หน่อยครับว่าพลาดตรงไหน export mysql to excel
Code (PHP)
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_query("SET NAMES utf8", $conn);
mysql_query("USE num_new");
$sql = "SELECT * FROM student";
$result = mysql_query($sql);
if($result){
//*** Get Document Path ***//
$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp
//*** Excel Document Root ***//
$strFileName = "MyXls/MyExcel.xls";
//*** Connect to Excel.Application ***//
$xlApp = new COM("Excel.Application");
$xlBook = $xlApp->Workbooks->Add();
//*** Create Sheet 1 ***//
$xlBook->Worksheets(1)->Name = "My Student";
$xlBook->Worksheets(1)->Select;
//*** Width & Height (A1:A1) ***//
$xlApp->ActiveSheet->Range("A1:A1")->ColumnWidth = 25.0;
$xlApp->ActiveSheet->Range("B1:B1")->ColumnWidth = 18.8;
$xlApp->ActiveSheet->Range("C1:C1")->ColumnWidth = 13.2;
$xlApp->ActiveSheet->Range("D1:D1")->ColumnWidth = 30.0;
//*** Report Title ***//
$xlApp->ActiveSheet->Range("A1:F1")->BORDERS->Weight = 1;
$xlApp->ActiveSheet->Range("A1:F1")->MergeCells = True;
$xlApp->ActiveSheet->Range("A1:F1")->Font->Bold = True;
$xlApp->ActiveSheet->Range("A1:F1")->Font->Size = 20;
$xlApp->ActiveSheet->Range("A1:F1")->HorizontalAlignment = -4108;
$xlApp->ActiveSheet->Cells(1,1)->Value = "รายชื่อนักศึกษาที่สมัคร";
//*** Header ***//
$xlApp->ActiveSheet->Cells(3,1)->Value = "ชื่อ";
$xlApp->ActiveSheet->Cells(3,1)->Font->Bold = True;
$xlApp->ActiveSheet->Cells(3,1)->VerticalAlignment = -4108;
$xlApp->ActiveSheet->Cells(3,1)->HorizontalAlignment = -4108;
$xlApp->ActiveSheet->Cells(3,1)->BORDERS->Weight = 1;
$xlApp->ActiveSheet->Cells(3,2)->Value = "สกุล";
$xlApp->ActiveSheet->Cells(3,2)->Font->Bold = True;
$xlApp->ActiveSheet->Cells(3,2)->VerticalAlignment = -4108;
$xlApp->ActiveSheet->Cells(3,2)->HorizontalAlignment = -4108;
$xlApp->ActiveSheet->Cells(3,2)->BORDERS->Weight = 1;
$xlApp->ActiveSheet->Cells(3,3)->Value = "สาขา";
$xlApp->ActiveSheet->Cells(3,3)->Font->Bold = True;
$xlApp->ActiveSheet->Cells(3,3)->VerticalAlignment = -4108;
$xlApp->ActiveSheet->Cells(3,3)->HorizontalAlignment = -4108;
$xlApp->ActiveSheet->Cells(3,3)->BORDERS->Weight = 1;
$xlApp->ActiveSheet->Cells(3,4)->Value = "สมัครระดับ";
$xlApp->ActiveSheet->Cells(3,4)->Font->Bold = True;
$xlApp->ActiveSheet->Cells(3,4)->VerticalAlignment = -4108;
$xlApp->ActiveSheet->Cells(3,4)->HorizontalAlignment = -4108;
$xlApp->ActiveSheet->Cells(3,4)->BORDERS->Weight = 1;
//***********//
$intRows = 4;
while($objResult = mysql_fetch_array($result))
{
//*** Detail ***//
$xlApp->ActiveSheet->Cells($intRows,1)->Value = $objResult["name"];
$xlApp->ActiveSheet->Cells($intRows,1)->BORDERS->Weight = 1;
$xlApp->ActiveSheet->Cells($intRows,1)->HorizontalAlignment = -4108;
$xlApp->ActiveSheet->Cells($intRows,2)->Value = $objResult["surname"];
$xlApp->ActiveSheet->Cells($intRows,2)->BORDERS->Weight = 1;
$xlApp->ActiveSheet->Cells($intRows,3)->Value = $objResult["level"];
$xlApp->ActiveSheet->Cells($intRows,3)->BORDERS->Weight = 1;
$xlApp->ActiveSheet->Cells($intRows,4)->Value = $objResult["major_regis"];
$xlApp->ActiveSheet->Cells($intRows,4)->HorizontalAlignment = -4108;
$xlApp->ActiveSheet->Cells($intRows,4)->BORDERS->Weight = 1;
$intRows++;
}
@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_close($conn);
?>
แต่มันออกมาเป็นภาษาแบบนี้ครับขอโทษด้วยครับที่รบกวน
Tag : - - - -
Date :
2010-02-25 14:21:02
By :
tonnant
View :
1399
Reply :
9
database เป็น utf-8 ด้วยครับ
Date :
2010-02-25 15:04:12
By :
gh
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_query("SET NAMES tis620", $conn);
mysql_query("USE num_new");
$sql = "SELECT * FROM student";
$result = mysql_query($sql);
if($result){
//*** Get Document Path ***//
$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp
//*** Excel Document Root ***//
$strFileName = "MyXls/MyExcel.xls";
//*** Connect to Excel.Application ***//
$xlApp = new COM("Excel.Application");
$xlBook = $xlApp->Workbooks->Add();
//*** Create Sheet 1 ***//
$xlBook->Worksheets(1)->Name = "My Student";
$xlBook->Worksheets(1)->Select;
//*** Width & Height (A1:A1) ***//
$xlApp->ActiveSheet->Range("A1:A1")->ColumnWidth = 25.0;
$xlApp->ActiveSheet->Range("B1:B1")->ColumnWidth = 18.8;
$xlApp->ActiveSheet->Range("C1:C1")->ColumnWidth = 13.2;
$xlApp->ActiveSheet->Range("D1:D1")->ColumnWidth = 30.0;
//*** Report Title ***//
$xlApp->ActiveSheet->Range("A1:F1")->BORDERS->Weight = 1;
$xlApp->ActiveSheet->Range("A1:F1")->MergeCells = True;
$xlApp->ActiveSheet->Range("A1:F1")->Font->Bold = True;
$xlApp->ActiveSheet->Range("A1:F1")->Font->Size = 20;
$xlApp->ActiveSheet->Range("A1:F1")->HorizontalAlignment = -4108;
$xlApp->ActiveSheet->Cells(1,1)->Value = "รายชื่อนักศึกษาที่สมัคร";
//*** Header ***//
$xlApp->ActiveSheet->Cells(3,1)->Value = "ชื่อ";
$xlApp->ActiveSheet->Cells(3,1)->Font->Bold = True;
$xlApp->ActiveSheet->Cells(3,1)->VerticalAlignment = -4108;
$xlApp->ActiveSheet->Cells(3,1)->HorizontalAlignment = -4108;
$xlApp->ActiveSheet->Cells(3,1)->BORDERS->Weight = 1;
$xlApp->ActiveSheet->Cells(3,2)->Value = "สกุล";
$xlApp->ActiveSheet->Cells(3,2)->Font->Bold = True;
$xlApp->ActiveSheet->Cells(3,2)->VerticalAlignment = -4108;
$xlApp->ActiveSheet->Cells(3,2)->HorizontalAlignment = -4108;
$xlApp->ActiveSheet->Cells(3,2)->BORDERS->Weight = 1;
$xlApp->ActiveSheet->Cells(3,3)->Value = "สาขา";
$xlApp->ActiveSheet->Cells(3,3)->Font->Bold = True;
$xlApp->ActiveSheet->Cells(3,3)->VerticalAlignment = -4108;
$xlApp->ActiveSheet->Cells(3,3)->HorizontalAlignment = -4108;
$xlApp->ActiveSheet->Cells(3,3)->BORDERS->Weight = 1;
$xlApp->ActiveSheet->Cells(3,4)->Value = "สมัครระดับ";
$xlApp->ActiveSheet->Cells(3,4)->Font->Bold = True;
$xlApp->ActiveSheet->Cells(3,4)->VerticalAlignment = -4108;
$xlApp->ActiveSheet->Cells(3,4)->HorizontalAlignment = -4108;
$xlApp->ActiveSheet->Cells(3,4)->BORDERS->Weight = 1;
//***********//
$intRows = 4;
while($objResult = mysql_fetch_array($result))
{
//*** Detail ***//
$xlApp->ActiveSheet->Cells($intRows,1)->Value = $objResult["name"];
$xlApp->ActiveSheet->Cells($intRows,1)->BORDERS->Weight = 1;
$xlApp->ActiveSheet->Cells($intRows,1)->HorizontalAlignment = -4108;
$xlApp->ActiveSheet->Cells($intRows,2)->Value = $objResult["surname"];
$xlApp->ActiveSheet->Cells($intRows,2)->BORDERS->Weight = 1;
$xlApp->ActiveSheet->Cells($intRows,3)->Value = $objResult["level"];
$xlApp->ActiveSheet->Cells($intRows,3)->BORDERS->Weight = 1;
$xlApp->ActiveSheet->Cells($intRows,4)->Value = $objResult["major_regis"];
$xlApp->ActiveSheet->Cells($intRows,4)->HorizontalAlignment = -4108;
$xlApp->ActiveSheet->Cells($intRows,4)->BORDERS->Weight = 1;
$intRows++;
}
@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_close($conn);
?>
</body>
</html>
Date :
2010-02-25 15:17:44
By :
DownsTream
ขอบคุณมากครับ ดึงข้อมูลจากฐานข้อมูลเป็นภาษาไทยแล้วครับแต่ภาษาไทยตรงหัวข้อ
มันยังเหมือนเดิมครับ แต่ก็ขอบคุณมากครับ
Date :
2010-02-25 21:00:33
By :
tonnant
เย้ แก้ไขได้แล้วครับ
Date :
2010-02-25 21:06:28
By :
tonnant
ดีใจด้วย ครับ
Date :
2010-02-25 21:37:07
By :
DownsTream
พอดีผมเพิ่มฟิลของ รหัสประจำตัวประชาชน
แต่ที่มันแสดงใน excel มันไม่แสดงตัวเลข 13 หลัก
มันแสดง 1.9206E+12
ช่วยหน่อยครับ ขอโทษนะครับ หาในโค๊ดก็ไม่รู้จะแก้ตรงไหนครับ
Date :
2010-02-26 10:47:46
By :
tonnant
ขอถามต่อจากกระทู้นี้นะคะ มันขึ้น
Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft Office Excel<br/><b>Description:</b> Unable to get the Add property of the Workbooks class' in C:\AppServ\www\statistical_bulletin\mf_by_sp_action.php:773 Stack trace: #0 C:\AppServ\www\statistical_bulletin\mf_by_sp_action.php(773): variant->Add() #1 {main} thrown in C:\AppServ\www\statistical_bulletin\mf_by_sp_action.php on line 773
ไม่ทราบว่าจะต้องแก้ยังงัยคะ ช่วยตอบด้วยนะคะ
//*** Connect to Excel.Application ***//
$xlApp = new COM("Excel.Application");
$xlBook = $xlApp->Workbooks->Add();
อันนี้เป็นโค๊ดบรรทัด 773ที่มีปัญหาค่ะ
Date :
2010-02-26 13:05:14
By :
Aea
ขอบคุณมากครับ ดึงข้อมูลจากฐานข้อมูลเป็นภาษาไทยแล้วครับแต่ภาษาไทยตรงหัวข้อ
มันยังเหมือนเดิมครับ แต่ก็ขอบคุณมากครับ
...................................................................................................................................................
ไม่ทราบว่าแก้ยังไงครับผมไม่รู้แก้ไงผมก็เป็นเหมือนกันที่หัว Colum เป็น ภาษาไรไม่รู้
Date :
2011-01-14 15:26:28
By :
ซันจิคุง
Load balance : Server 05