2011-01-06 50 views

回答

1

首先,您需要能夠讀取Excel數據。哪個版本的Excel? xls或xlsx? 我建議使用PHPExcel這個庫,雖然有替代品。

include 'PHPExcel/IOFactory.php'; 

$inputFileName = './sampleData/example1.xls'; 
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName); 

$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true); 

將爲您提供標準PHP數組中的工作表數據。

0

,如果你能使用CSV文件,可以使用 fgetcsv和fputcsv:http://www.php.net/manual/fr/function.fgetcsv.php

檢查該樣品。每個條目

<?php 

define('INPUT_FILENAME', '/path/to/your/csv/file'); 

define('OUTPUT_FILENAME', '/path/to/your/csv/file'); 
define('SEPARATOR', ','); 
define('ENCLOSURE', '"'); 

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    if (isset($_POST['entries'])) { 
     $entries = $_POST['entries']; 
    } 

    // do the validation stuff here ... 

    if ($ok) { 
     // save the entries into the DESTINATION file 

     $handle = fopen(OUTPUT_FILENAME, 'w'); 

     foreach ($entries as $entry) { 
      $fields = array(
       $entry['name'], 
       $entry['salary'] 
      ); 
      fputcsv($handle, $fields, SEPARATOR, ENCLOSURE); 
     } 

     exit('done'); 
    } 
} 

if (!isset($entries)) { 
    // fetch the entries 
    $entries = array(); 
    if (($handle = fopen(INPUT_FILENAME, 'r')) !== false) { 
     while (($row = fgetcsv($handle, 1000, SEPARATOR, ENCLOSURE)) !== false) { 
      list($name, $salary) = $row; 
      $entries[] = array(
       'name' => $name, 
       'salary' => $salary 
      ); 
     } 
     fclose($handle); 
    } 
} 

// display the form 

print '<form action="#" method="post">'; 

foreach ($entries as $i => $entry) { 
    print '<fieldset>'; 
    print '<label>Name'; 
    printf('<input type="text" name="entries[%s]name" value="%s" />', $i, htmlspecialchars($entry['name'])); 
    print '</label>'; 
    print '<label>Name'; 
    printf('<input type="text" name="entries[%s]salary" value="%s" />', $i, htmlspecialchars($entry['salary'])); 
    print '</label>'; 
    print '</fieldset>'; 
} 
print '<input type="submit" />'; 
print '</form>'; 

而且一種形式:

<?php 

define('INPUT_FILENAME', '/path/to/your/csv/file'); 

define('OUTPUT_FILENAME', '/path/to/your/csv/file'); 
define('SEPARATOR', ','); 
define('ENCLOSURE', '"'); 

$entries = array(); 

// Allways prefetch entries 
if (($handle = fopen(INPUT_FILENAME, 'r')) !== false) { 
    while (($row = fgetcsv($handle, 1000, SEPARATOR, ENCLOSURE)) !== false) { 
     list($name, $salary) = $row; 
     $entries[] = array(
      'name' => $name, 
      'salary' => $salary 
     ); 
    } 
    fclose($handle); 
} 

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    $position = null; 
    if (isset($_POST['position'])) { 
     $position = (int) $_POST['position']; 
    } 

    $name = null; 
    if (isset($_POST['name'])) { 
     $name = $_POST['name']; 
    } 

    $salary = null; 
    if (isset($_POST['salary'])) { 
     $salary = $_POST['salary']; 
    } 


    // do the validation stuff here ... 

    if ($ok) { 
     // merge the entry into the list 
     $entries[$position] = array(
      'name' => $name, 
      'salary' => $salary 
     ); 

     // save the entries into the DESTINATION file 

     $handle = fopen(OUTPUT_FILENAME, 'w'); 

     foreach ($entries as $entry) { 
      $fields = array(
       $entry['name'], 
       $entry['salary'] 
      ); 
      fputcsv($handle, $fields, SEPARATOR, ENCLOSURE); 
     } 

     exit('done'); 
    } 
} 

// display 

foreach ($entries as $i => $entry) { 
    // one form per entry 
    print '<form action="#" method="post">'; 
    print '<fieldset>'; 
    print '<label>Name'; 
    printf('<input type="text" name="name" value="%s" />', htmlspecialchars($entry['name'])); 
    print '</label>'; 
    print '<label>Name'; 
    printf('<input type="text" name="salary" value="%s" />', htmlspecialchars($entry['salary'])); 
    print '</label>'; 
    printf('<input type="hidden" name="position" value="%s"', $i); 
    print '</fieldset>'; 
    print '<input type="submit" />'; 
    print '</form>'; 
} 
+0

感謝兄弟多數民衆贊成偉大的解決方案。其他請請如果你能我怎麼能打開每一行的新表格。 – 2011-01-06 10:42:01

0

將文件保存爲一個網頁,然後編寫PHP腳本的一點點吧。 如果您不想使用mysql,請使用Sqlite來存儲值。 Sqlite將會像文件一樣。

相關問題