G
G
GPK19292015-04-28 21:30:35
PHP
GPK1929, 2015-04-28 21:30:35

How to implement Excel parsing?

Good afternoon, community, it became necessary to implement the export / import of the database to / from Excel, I started searching and found the PHPExcel library, then I tried to figure it out in the context of the task, and then a problem arose, the task is as follows: there is a database with tables products and options_product and it is necessary put both tables on one EXEL sheet, while they should be mixed, a row from the products table is followed by several options_product, so is it possible to implement this so that later, for example, changing the prices of the product, upload it back to the site? It's just that when I started to understand, I got the impression that each database table should be created on a separate sheet and it is impossible to interfere with tables on one sheet? Can anyone come across a similar situation and tell me, maybe another library should be used? Or I misunderstood something.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis, 2015-04-28
@GPK1929

There is a good import lib .

H
Hakhagmon, 2015-04-30
@Hakhagmon

I did this a long time ago, might be useful

class CSV {
    //private $_csv_file = null;
  public $_csv_file = null;
    public function __construct($csv_file) {
        if (file_exists($csv_file)) { 
            $this->_csv_file = $csv_file; 
        }
        else throw new Exception("Файл \"$csv_file\" не найден"); 
    }
    public function getCSV() {
        $handle = fopen($this->_csv_file, "r");
        $array_line_full = array();
        while (($line = fgetcsv($handle, 0, ";")) !== FALSE) { 
            $array_line_full[] = $line; 
        }
        fclose($handle); 
        return $array_line_full;
    }
}

try {
    $csv = new CSV("upload.csv"); 
  
    echo "<h2>Содержимое файла " . $csv->_csv_file . "</h2>";
    $get_csv = $csv->getCSV();
  $i = 1;
    foreach ($get_csv as $value) {
        
    echo '<form id="form' . $i . '" name="form' . $i . '" action="/admin.php?mod=csvupload-action" method="post">';
    echo '<div><label for="f0">Название:</label><input type="text" name="f0" value="' . $value[0] . '"></div>';		
        echo '<div><label for="f1">Категория:</label><input type="text" name="f1" value="' . $value[1] . '"></div>';
        echo '<div><label for="f2">Вид образовательной программы:</label><input type="text" name="f2" value="' . $value[2] . '"></div>';		
    echo '<div><label for="f3">Дата События:</label><input type="text" name="f3" value="' . $value[3] . '"></div>';
    echo '<div><label for="f4">Конец События:</label><input type="text" name="f4" value="' . $value[4] . '"></div>';
    echo '<div><label for="f5">Краткое описание (shortstory):</label><!--textarea rows="10" name="f5">' . htmlspecialchars($value[5]) . '</textarea--><input type="text" name="f5" value="' . htmlspecialchars($value[5]) . '"></div>';
    echo '<div><label for="f6">Полное описание (fullstory):</label><!--textarea rows="10" name="f6">' . htmlspecialchars($value[6]) . '</textarea--><input type="text" name="f6" value="' . htmlspecialchars($value[6]) . '"></div>';
    echo '<div><label for="f8">Форма обучения:</label><input type="text" name="f8" value="' . $value[8] . '"></div>';
    echo '<div><label for="f9">Направление:</label><input type="text" name="f9" value="' . $value[9] . '"></div>';
    echo '<div><label for="f10">Стоимость:</label><input type="text" name="f10" value="' . $value[10] . '"></div>';
    echo '<div><label for="f11">Продолжительность  ч.:</label><input type="text" name="f11" value="' . $value[11] . '"></div>';
    echo '<div><label for="f12">Сроки обучения д.:</label><input type="text" name="f12" value="' . $value[12] . '"></div>';
    echo '<div><label for="f13">Время проведения занятий:</label><input type="text" name="f13" value="' . $value[13] . '"></div>';
    echo '<div><label for="f14">Телефон:</label><input type="text" name="f14" value="' . $value[14] . '"></div>';
    echo '<div><label for="f15">E-mail:</label><input type="text" name="f15" value="' . $value[15] . '"></div>';
    echo '<div><label for="f16">Слушатели:</label><input type="text" name="f16" value="' . $value[16] . '"></div>';
    echo '<div><label for="f17">Место обучения:</label><input type="text" name="f17" value="' . $value[17] . '"></div>';
    echo '<div><label for="f18">Адрес:</label><input type="text" name="f18" value="' . $value[18] . '"></div>';
    echo '<div><label for="f19">Выпускной документ:</label><input type="text" name="f19" value="' . $value[19] . '"></div>';
    echo '<div><label for="f20">Учебный план (ссылка):</label><input type="text" name="f20" value="' . $value[20] . '"></div>';
    echo '<button>Сохранить</button></form>';

    $i++;
    }
}
catch (Exception $e) {
    echo "Ошибка: " . $e->getMessage();
}

echofooter();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question