Potrzebujesz zaimplementować import csv lub xls do aplikacji utworzonej przy użyciu CodeIgniter. Czy jest na to jakaś biblioteka? Wszelkie sugestie doceniane.Biblioteka importu CSV dla CodeIgniter
Odpowiedz
Oto prosty sposób na zrobienie tego. Nie wiem, co ludzie robią, ale używam tego
To mój CSV czytelnik biblioteki
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class CSVReader {
var $fields; /** columns names retrieved after parsing */
var $separator = ';'; /** separator used to explode each line */
var $enclosure = '"'; /** enclosure used to decorate each field */
var $max_row_size = 4096; /** maximum row size to be used for decoding */
function parse_file($p_Filepath) {
$file = fopen($p_Filepath, 'r');
$this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
$keys_values = explode(',',$this->fields[0]);
$content = array();
$keys = $this->escape_string($keys_values);
$i = 1;
while(($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false) {
if($row != null) { // skip empty lines
$values = explode(',',$row[0]);
if(count($keys) == count($values)){
$arr = array();
$new_values = array();
$new_values = $this->escape_string($values);
for($j=0;$j<count($keys);$j++){
if($keys[$j] != ""){
$arr[$keys[$j]] = $new_values[$j];
}
}
$content[$i]= $arr;
$i++;
}
}
}
fclose($file);
return $content;
}
function escape_string($data){
$result = array();
foreach($data as $row){
$result[] = str_replace('"', '',$row);
}
return $result;
}
}
?>
i sposób kontroler
function readExcel()
{
$this->load->library('csvreader');
$result = $this->csvreader->parse_file('Test.csv');
$data['csvData'] = $result;
$this->load->view('view_csv', $data);
}
A to widok
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width = "10%">ID</td>
<td width = "20%">NAME</td>
<td width = "20%">SHORT DESCRIPTION</td>
<td width = "30%">LONG DESCRIPTION</td>
<td width = "10%">STATUS</td>
<td width = "10%">PARENTID</td>
</tr>
<?php foreach($csvData as $field){?>
<tr>
<td><?php echo $field['id']?></td>
<td><?php echo $field['name']?></td>
<td><?php echo $field['shortdesc']?></td>
<td><?php echo $field['longdesc']?></td>
<td><?php echo $field['status']?></td>
<td><?php echo $field['parentid']?></td>
</tr>
<?php }?>
</table>
Numer referencyjny: Here
Jest to ulepszona wersja Raheel Shan zaakceptowany odpowiedzi - I edycja jego odpowiedź, ale moja edycja została odrzucona, jednak jest to ważna zmiana ...
Podczas analizowania każdy wiersz danych, to nie jest mądre, aby korzystać explode()
na przecinek, ponieważ nie obsługuje ciągów zawierających cudzysłowy, które zawierają przecinki. Eksplodować łamie te ciągi znaków na podciągi i daje dodatkowe elementy tablicy w $values
, więc ten test się nie powiedzie:
if (count($keys) == count($values)) {
Zamiast PHP ma metodę specjalnie zaprojektowanym do obsługi tego; str_getcsv(). Odpowiednio zaktualizowałem oryginalny kod odpowiedzi.
Biblioteka CSV Reader:
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class CSVReader {
var $fields;/** columns names retrieved after parsing */
var $separator = ';';/** separator used to explode each line */
var $enclosure = '"';/** enclosure used to decorate each field */
var $max_row_size = 4096;/** maximum row size to be used for decoding */
function parse_file($p_Filepath) {
$file = fopen($p_Filepath, 'r');
$this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
$keys = str_getcsv($this->fields[0]);
$i = 1;
while (($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false) {
if ($row != null) { // skip empty lines
$values = str_getcsv($row[0]);
if (count($keys) == count($values)) {
$arr = array();
for ($j = 0; $j < count($keys); $j++) {
if ($keys[$j] != "") {
$arr[$keys[$j]] = $values[$j];
}
}
$content[$i] = $arr;
$i++;
}
}
}
fclose($file);
return $content;
}
}
?>
Controller metoda:
function readExcel() {
$this->load->library('csvreader');
$result = $this->csvreader->parse_file('Test.csv');
$data['csvData'] = $result;
$this->load->view('view_csv', $data);
}
A to jest widok:
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="10%">ID</td>
<td width="20%">NAME</td>
<td width="20%">SHORT DESCRIPTION</td>
<td width="30%">LONG DESCRIPTION</td>
<td width="10%">STATUS</td>
<td width="10%">PARENTID</td>
</tr>
<?php foreach ($csvData as $field) { ?>
<tr>
<td><?php echo $field['id'] ?></td>
<td><?php echo $field['name'] ?></td>
<td><?php echo $field['shortdesc'] ?></td>
<td><?php echo $field['longdesc'] ?></td>
<td><?php echo $field['status'] ?></td>
<td><?php echo $field['parentid'] ?></td>
</tr>
<?php } ?>
</table>
Dobrze mi nie przeszkadza, po prostu zmodyfikowane ajmedway użytkownika biblioteka zawierająca ograniczniki, na wypadek gdybyś chciał uzyskać dane, powiedzmy TSV lub pliki rozdzielone potokami. Jeśli chcesz mieć zwykły stary plik CSV, jest w porządku.
class CSVReader {
var $fields;/** columns names retrieved after parsing */
var $separator = ';';/** separator used to explode each line */
var $enclosure = '"';/** enclosure used to decorate each field */
var $max_row_size = 4096;/** maximum row size to be used for decoding */
function parse_file($p_Filepath, $delimiter = FALSE) {
$file = fopen($p_Filepath, 'r');
$this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
if ($delimiter==FALSE)
{
$keys = str_getcsv($this->fields[0]);
$i = 1;
while (($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false) {
if ($row != null) { // skip empty lines
$values = str_getcsv($row[0]);
if (count($keys) == count($values)) {
$arr = array();
for ($j = 0; $j < count($keys); $j++) {
if ($keys[$j] != "") {
$arr[$keys[$j]] = $values[$j];
}
}
$content[$i] = $arr;
$i++;
}
}
}
}
else{
$keys = str_getcsv($this->fields[0],$delimiter);
$i = 1;
while (($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false) {
if ($row != null) { // skip empty lines
$values = str_getcsv($row[0],$delimiter);
if (count($keys) == count($values)) {
$arr = array();
for ($j = 0; $j < count($keys); $j++) {
if ($keys[$j] != "") {
$arr[$keys[$j]] = $values[$j];
}
}
$content[$i] = $arr;
$i++;
}
}
}
}
fclose($file);
return $content;
}}?>
Jest to niepoprawne, jeśli ($ delimiter = FALSE) podczas ustawiania ogranicznika $ na FALSE i nie sprawdzania jego statusu. Powinno to być, jeśli ($ delimiter == FALSE) – Johnish
whoops! Przepraszam. Błąd nowicjusza. Dzięki @Johnish. Skoryguję to natychmiast. –
Jest to ulepszona wersja dla pustych linii i dodatkowych spacji i kart ...
klasa CsvReader {
function csv_to_array($Filepath)
{
//Get csv file content
$csvData = file_get_contents($Filepath);
//Remove empty lines
$csvData = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\r\n", $csvData);
//String convert in array formate and remove double quote(")
$array = array();
$array = $this->escape_string(preg_split('/\r\n|\r|\n/', $csvData));
$new_content_in_array = array();
if($array)
{
//Get array key
$array_keys = array();
$array_keys = array_filter(array_map('trim', explode(';',$array[0])));
//Get array value
$array_values = array();
for ($i=1;$i<count($array);$i++)
{
if($array[$i])
{
$array_values[] = array_filter(array_map('trim', explode(';',$array[$i])));
}
}
//Convert in associative array
if($array_keys && $array_values)
{
$assoc_array = array();
foreach ($array_values as $ky => $val)
{
for($j=0;$j<count($array_keys);$j++){
if($array_keys[$j] != "" && $val[$j] != "" && (count($array_keys) == count($val)))
{
$assoc_array[$array_keys[$j]] = $val[$j];
}
}
$new_content_in_array[] = $assoc_array;
}
}
}
return $new_content_in_array;
}
function escape_string($data){
$result = array();
foreach($data as $row){
$result[] = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "", str_replace('"', '',$row));
}
return $result;
}
} >
połączeń w kontrolerze:
$this->load->library('csvreader');
$import_csv_data = $this->csvreader->csv_to_array($path);
print_r($import_csv_data);
exit;
- 1. importu CSV do phpMyAdmin
- 2. Cassandra CQL3 importu CSV
- 3. Wgrywanie CSV do Codeigniter
- 4. importu CSV Tablica tekstu do PostgreSQL 9,2
- 5. Codeigniter REST CSV import do mysql
- 6. importu CSV zaktualizować tylko jedną kolumnę w tabeli
- 7. Kreator importu tabeli mysql nie może zaimportować pliku csv
- 8. Najlepsza biblioteka dla sesji PHP
- 9. Prawidłowa ścieżka dla #importu frameworków iPhone'a (ALAssetsLibrary.h)
- 10. Jak zmodyfikować tabelę adresu importu dla czasu pracy Załadowana biblioteka DLL
- 11. Alternatywa dla pliku CSV?
- 12. Rewriterule dla CodeIgniter nie działa
- 13. Routing w CodeIgniter dla (: any)
- 14. Nginx reguła przepisywania dla CodeIgniter
- 15. phpmyadmin: Plik delegowanej karty importu
- 16. Jak określić inną ścieżkę pliku dla zapisanego importu programu Excel
- 17. Biblioteka SSH dla Java
- 18. Biblioteka rysunków dla Ruby
- 19. Biblioteka wykresów dla Qt
- 20. Biblioteka rejestratorów dla iOS
- 21. Biblioteka QR dla MonoTouch?
- 22. Biblioteka SSH2 dla ios
- 23. Biblioteka AS3 dla Socket.io
- 24. Biblioteka VoIP dla Androida
- 25. Biblioteka WebP dla java?
- 26. Biblioteka SNMP dla Androida
- 27. Biblioteka SASL dla .net
- 28. Biblioteka JSON dla C#
- 29. Biblioteka dla IMAP IDLE
- 30. Biblioteka UUID dla C?
Thx Raheelu, czy zaimplementowałeś to, żeby również wprowadzić csv do bazy danych? –
tak po prostu przekaż '$ result' do tabeli bazy danych lub utwórz tablicę zgodnie z Twoimi wymaganiami. –