 |
อยากได้โค๊ดตัวอย่าง Upload Excel to MySQL Database เป็นฟอร์ม Browse ครับ |
|
 |
|
|
 |
 |
|
ขอบคุณครับ พี่วิน
แต่ผมสงสัยไฟล์ myData.xls กับ myData.xlsx นี้ครับ
ถามนิดนึงครับ ทำไมผมลองแก้ไขโค๊ดเพื่อที่จะ เปิด หรือ insert ไฟล์ myData.xlsx (Excel 2007) แต่ก็ไม่ได้สักทีครับ
แต่ถ้าเป็นไฟล์ myData.xls (Excel 2003) จะใช้งานได้ปกติครับ
ผมทดลองจากไฟล์ PHPExcelRead.php กับไฟล์ PHPExcelReadToMySQL.php
|
 |
 |
 |
 |
Date :
2012-08-17 22:00:11 |
By :
nattkhanesha |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันมี Version สำหรับ xlsx กับ xls อยู่น่ะครับ
|
 |
 |
 |
 |
Date :
2012-08-17 22:52:16 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แล้วผมลองแก้ไข Error ZipArchive ก็ยังติดปัญหาอยู่ครับ
Fatal error: Class 'ZipArchive' not found in D:\AppServ\www\PHPExcel\Classes\PHPExcel\Reader\Excel2007.php on line
|
 |
 |
 |
 |
Date :
2012-08-17 23:41:27 |
By :
nattkhanesha |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ติดตั้ง php_zip.dll
|
 |
 |
 |
 |
Date :
2012-08-17 23:55:20 |
By :
...... |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมลองทำตามแล้วครับ เปิด extension ก็ยังไม่ได้ครับ
Requirements
------------
The following requirements should be met prior to using PHPExcel:
* PHP version 5.2.0 or higher
* PHP extension php_zip enabled *)
* PHP extension php_xml enabled
* PHP extension php_gd2 enabled (if not compiled in)
*) php_zip is only needed by PHPExcel_Reader_Excel2007, PHPExcel_Writer_Excel2007,
PHPExcel_Reader_OOCalc. In other words, if you need PHPExcel to handle .xlsx or .ods
files you will need the zip extension, but otherwise not.
|
 |
 |
 |
 |
Date :
2012-08-18 00:24:14 |
By :
nattkhanesha |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอถามไฟล์ PHPExcelReadToMySQL.php ครับ
ผมติดปัญหา AA1 เพราะว่ามีหลายรายการ แล้วจะต้องแก้ไขโค๊ดตรงไหนครับ ขอบคุณ 
Row 1 Inserted...
Row 2 Inserted...
Row 3 Inserted...
Row 4 Inserted...
Row 5 Inserted...
Duplicate entry 'AA1' for key 1
|
 |
 |
 |
 |
Date :
2012-08-18 13:49:43 |
By :
nattkhanesha |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอถามเพิ่มเติมครับ พี่วิน
1. โค๊ดไฟล์ PHPExcelReadToMySQL.php จะ Insert ได้แค่ 5 Column ใช่ป่าวครับ แล้วถ้าผมจะเพิ่ม Column เป็น 10 หรือ 20 Column ได้ป่าวครัย 
2. แล้วถ้าตัวอักษรในไฟล์ Excel มีจำนวนที่มาก จะเจอปัญหาแบบนี้ครับ มีวิธีแก้ไขป่าวครับ 
Fatal error: Allowed memory size of 25165824 bytes exhausted (tried to allocate 18480 bytes) in D:\AppServ\www\PHPExcel\Classes\PHPExcel\Calculation.php on line 2327
Code (PHP)
<?php
/** PHPExcel */
require_once 'Classes/PHPExcel.php';
/** PHPExcel_IOFactory - Reader */
include 'Classes/PHPExcel/IOFactory.php';
$inputFileName = "myData.xls";
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($inputFileName);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$headingsArray = $objWorksheet->rangeToArray('A1:'.$highestColumn.'1',null, true, true, true);
$headingsArray = $headingsArray[1];
$r = -1;
$namedDataArray = array();
for ($row = 2; $row <= $highestRow; ++$row) {
$dataRow = $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,null, true, true, true);
if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {
++$r;
foreach($headingsArray as $columnKey => $columnHeading) {
$namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey];
}
}
}
//echo '<pre>';
//var_dump($namedDataArray);
//echo '</pre><hr />';
//*** Connect to MySQL Database ***//
$objConnect = mysql_connect("localhost","root","123456") or die(mysql_error());
$objDB = mysql_select_db("excel_db");
$i = 0;
foreach ($namedDataArray as $result) {
$i++;
$strSQL = "";
$strSQL .= "INSERT INTO customer ";
$strSQL .= "(CustomerID,Name,Email,CountryCode,Budget,Used) ";
$strSQL .= "VALUES ";
$strSQL .= "('".$result["CustomerID"]."','".$result["Name"]."' ";
$strSQL .= ",'".$result["Email"]."','".$result["CountryCode"]."' ";
$strSQL .= ",'".$result["Budget"]."','".$result["Used"]."') ";
mysql_query($strSQL) or die(mysql_error());
echo "Row $i Inserted...<br>";
}
mysql_close($objConnect);
?>
|
ประวัติการแก้ไข 2012-08-18 14:49:55
 |
 |
 |
 |
Date :
2012-08-18 14:46:23 |
By :
nattkhanesha |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันเพิ่มได้หมดและครับ จริงๆ ใช้ PHPExcel ตัวใหม่จะสามารถอ่าน excel 2007 ได้ง่ายสุด
|
 |
 |
 |
 |
Date :
2012-08-18 15:07:33 |
By :
ikikkok |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
งั้นผมขอตัวอย่าง Excel 2007 ได้ป่าวครับ คุณ PlaKriM 
|
 |
 |
 |
 |
Date :
2012-08-18 16:26:47 |
By :
nattkhanesha |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใน Demo มันมี Version ของ 2007 อยู่ครับ ลองเปิดดูครับ
|
 |
 |
 |
 |
Date :
2012-08-19 07:45:25 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อิอิ
|
 |
 |
 |
 |
Date :
2012-08-20 00:46:57 |
By :
bongbankbb |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|