Code Monkey home page Code Monkey logo

Comments (24)

rap2hpoutre avatar rap2hpoutre commented on May 18, 2024 8

My code was wrong, could you try this:

 public function import(Request $request)
    {
        $path = $request->file('file')->store('excel-files');
        $products = (new FastExcel)->import(storage_path('app/' . $path), function ($reader) {
            return Product::create([
                'title' => $reader['title'],
                'slug' => $reader['slug'],
                'imageOne' => $reader['imageOne'],
                'imageTwo' => $reader['imageTwo'],
                'imageThree' => $reader['imageThree'],
                'short_description' => $reader['short_description'],
                'description' => $reader['description'],
                'stock' => $reader['stock'],
                'price' => $reader['price'],
                'installation_price' => $reader['installation_price'],
                'meta_description' => $reader['meta_description'],
                'meta_tags' => $reader['meta_tags'],
                'discounted_price' => $reader['discounted_price'],
                'start_discounted' => $reader['start_discounted'],
                'end_discounted' => $reader['end_discounted'],
                'demo_link' => $reader['demo_link'],
                'status' => $reader['status'],
                'category_id' => $reader['category_id']
            ]);
        });

        Session::flash('success', 'Your products imported successfully.');
    }

The only line I changed is this one:

$products = (new FastExcel)->import(storage_path('app/' . $path), function ($reader) {

It should work!

from fast-excel.

robertnicjoo avatar robertnicjoo commented on May 18, 2024 2

the code gives

Could not open for reading! File does not exist.

I also tried:

$path = "file:///C:/Users/username/Downloads/new/new-products.csv";

and i got the same error:

Could not open for reading! File does not exist.

as the result of saving my file to storage I have txt file in my storage folder included this content , , , , , ,

from fast-excel.

rap2hpoutre avatar rap2hpoutre commented on May 18, 2024 1

@robertnicjoo Sorry for late answer (vacations).

Could you test with this CSV file: https://github.com/rap2hpoutre/fast-excel/blob/master/tests/test2.csv? It's the one used in my unit tests (and the unit tests actually works).

I tested your CSV (https://github.com/rap2hpoutre/fast-excel/blob/master/tests/test18.csv) and added a unit test (https://github.com/rap2hpoutre/fast-excel/blob/master/tests/FastExcelTest.php#L163) and it still works.

So, I don't think it's a problem about your CSV content.

I think the problem may be about your file extension. Could you rename your file to something.csv? And could you double check the uploaded file does exists in the folder you open?

from fast-excel.

mountainguan avatar mountainguan commented on May 18, 2024 1

@rap2hpoutre yeah,I finally go to use laravel-excel 2.1 version.

from fast-excel.

veggigit avatar veggigit commented on May 18, 2024 1

@robertnicjoo u can use base_path() for mayor control.
https://laravel.com/docs/5.7/helpers#paths

from fast-excel.

mikizdr avatar mikizdr commented on May 18, 2024 1

File is stored properly and it exists. Maatwebexcel can read and handle that file but FastExcel can't. It says:

Could not open the file ....path + filename.xlsx for reading!

Path is under /storage/app/uploads/.... but for maatweb I'm using just /uploads/date/... and it works, with this package not. I tried everything but I don't know if it uses relative or abs path or some other conventions.
BTW: I'm trying this package to see if it can handle a huge xlsx file for importing to the db faster and more effectively.

from fast-excel.

klukiyan avatar klukiyan commented on May 18, 2024 1

This issue is closed long time ago. But just in case somebody else will be coming accross it.
I had the same error message "Couldn't open for reading". After trying with firstly save file on server and then open - I discovered that Large files were simply not saved. There was php_max_upload_filesize = 2MB on my server's ini. Changed it to 2Gb and it started working very nice

from fast-excel.

rap2hpoutre avatar rap2hpoutre commented on May 18, 2024

Thanks for your contribution!

You could try this (save the file in storage, then import the file):

 public function import(Request $request)
    {
        $path = $request->file('file')->store('excel-files');
        $products = (new FastExcel)->import($request->file($path), function ($reader) {
            return Product::create([
                'title' => $reader['title'],
                'slug' => $reader['slug'],
                'imageOne' => $reader['imageOne'],
                'imageTwo' => $reader['imageTwo'],
                'imageThree' => $reader['imageThree'],
                'short_description' => $reader['short_description'],
                'description' => $reader['description'],
                'stock' => $reader['stock'],
                'price' => $reader['price'],
                'installation_price' => $reader['installation_price'],
                'meta_description' => $reader['meta_description'],
                'meta_tags' => $reader['meta_tags'],
                'discounted_price' => $reader['discounted_price'],
                'start_discounted' => $reader['start_discounted'],
                'end_discounted' => $reader['end_discounted'],
                'demo_link' => $reader['demo_link'],
                'status' => $reader['status'],
                'category_id' => $reader['category_id']
            ]);
        });

        Session::flash('success', 'Your products imported successfully.');
    }

See: box/spout#448, box/spout#286, box/spout#379 and box/spout#294.

If it does not work, could you try using a full path for your file? Something like "C:/.../.../file/form.xlsx"
There must be something wrong with how Spout handles path on Windows.

from fast-excel.

robertnicjoo avatar robertnicjoo commented on May 18, 2024

it says:

Could not open C:\laragon\www\mynewsite\storage\app/excel-files/WXgZu1J2fgSVDP2pkoaI8Dh0Z7fqUq40ymuiFQTC.txt for reading! File does not exist.

here is what inside that file

title,slug,imageOne,imageTwo,imageThree,short_description,description,stock,price,installation_price,meta_description,meta_tags,discounted_price,start_discounted,end_discounted,demo_link,status,category_id,created_at,updated_at
dh,dghdgh,product-1503124196.png,,,<p>no have short description</p>,dghdhdhdghdh,200,1000000,,,"dhdhdh,dhdfh,dhfh",,,,,enable,1,19/08/2017 13:09,26/08/2017 13:02
,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,

pretty much my csv file + these extra contents ,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,

issue with this txt file is, it contains my csv file header as well title,slug,imageOne,imageTwo,imageThree,s........

from fast-excel.

pankajvasnanitech123 avatar pankajvasnanitech123 commented on May 18, 2024

I want to know if the user is uploading the excel file and I dont want to store the file in storage. Is there a solution for this ?

from fast-excel.

rap2hpoutre avatar rap2hpoutre commented on May 18, 2024

@pankajvasnanitech123 Thank you for your contribution! Could you open a new issue for your question?

from fast-excel.

Harti avatar Harti commented on May 18, 2024

I had the same issue with files not opening. At first I thought it was because I still had the file open, but then I realized I was using .xls which is a tad different from .xlsx and not supported by Spout.

If you are looking to import XLS, look into using unoconv to convert the file to XLSX first.

@rap2hpoutre thanks for making this lib! With your main competitor having dropped importing with his 3.0 version, this is pretty much the only viable solution.

Importing your test CSV file, however, I'm getting the following error (PHP 7.2.4, package v0.2):

php artisan forestmanager:import storage/test2.csv -v

   ErrorException  : array_combine(): Both parameters should have an equal number of elements

  at /var/www/vendor/rap2hpoutre/fast-excel/src/Importable.php:69
    65|                     }
    66|                     if ($count_header > $count_row = count($row)) {
    67|                         $row = array_merge($row, array_fill(0, $count_header - $count_row, null));
    68|                     }
  > 69|                     $collection[] = $callback ? $callback(array_combine($headers, $row)) : array_combine($headers, $row);
    70|                 }
    71|             } else {
    72|                 foreach ($sheet->getRowIterator() as $row) {
    73|                     $collection[] = $row;

  Exception trace:

  1   array_combine()
      /var/www/vendor/rap2hpoutre/fast-excel/src/Importable.php:69

  2   Rap2hpoutre\FastExcel\FastExcel::import("storage/test2.csv", Object(Closure))
      /var/www/app/Console/Commands/ImportAppDatabase.php:50

Command:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Rap2hpoutre\FastExcel\FastExcel;

class ImportAppDatabase extends Command
{

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'forestmanager:import {file}';

    ...

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $file = $this->argument('file');

        $import = (new FastExcel)->import($file, function($line)
        {
             // left empty for the time being
        });
    }
}

from fast-excel.

rap2hpoutre avatar rap2hpoutre commented on May 18, 2024

@Harti Thank you for contributing!

If you are looking to import XLS, look into using unoconv to convert the file to XLSX first.

I did not know this application, thanks! (still I will not include it in the package, since it's an executable file)

About your CSV test, I don't understand why you have a problem because it's already unit tested here: https://github.com/rap2hpoutre/fast-excel/blob/master/tests/FastExcelTest.php#L46

public function testImportCsv()
{
    $original_collection = $this->collection();

    $collection = (new FastExcel())->import(__DIR__.'/test2.csv');
    $this->assertEquals($original_collection, $collection);

    // ...
}

from fast-excel.

Harti avatar Harti commented on May 18, 2024

@rap2hpoutre did you unit-test it under PHP 7.2?

They changed a lot of core function parameters so that they are no longer duck-typed but have to implement certain core interfaces. For the count() function, this would be Countable - count(null) will cause a PHP warning.

From the error message, it looks like something's off with array_combine() here. Not about duck types but more of a new nitpick. I can't investigate this, all I know is that your test2.csv file throws an error for me.

from fast-excel.

rap2hpoutre avatar rap2hpoutre commented on May 18, 2024

@Harti

did you unit-test it under PHP 7.2?

Yes!

➜  laravel-fast-excel git:(master) php -v
PHP 7.2.1 (cli) (built: Jan  5 2018 17:34:14) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies
➜  laravel-fast-excel git:(master) phpunit
PHPUnit 7.0.2 by Sebastian Bergmann and contributors.

.........                                                           9 / 9 (100%)

Time: 275 ms, Memory: 10.00MB

OK (9 tests, 12 assertions)

Could you try to run the unit tests?

from fast-excel.

mountainguan avatar mountainguan commented on May 18, 2024

I also meet the same error.

from fast-excel.

rap2hpoutre avatar rap2hpoutre commented on May 18, 2024

Hi @mountainguan, thank you for contributing!

I also meet the same error.

What do you mean by "the same error"? I feel like there are different errors in this issue, and I'm not sure which one is yours!

  1. if it's this one, could you try the code posted here?
  2. If it's this one, could you try to run the unit tests?

from fast-excel.

mountainguan avatar mountainguan commented on May 18, 2024

@rap2hpoutre could not read xls file...

from fast-excel.

rap2hpoutre avatar rap2hpoutre commented on May 18, 2024

@mountainguan Ok! This lib can not read XLS files (only XLSX, CSV and ODT). The lib is based on Spout can handle only a few format (box/spout#213).

from fast-excel.

mikizdr avatar mikizdr commented on May 18, 2024

Why publish not well tested packages? If you want the audience test your package it would be polite to say that in description. Otherwise a lot of people implement package, then dive into problems, debugging, brainstorming and waist a lot of time trying to get result but finally without success. Your work is good and go on with that but have a respect to other times and trust.

from fast-excel.

rap2hpoutre avatar rap2hpoutre commented on May 18, 2024

@mikizdr Sorry for hurting you. What is wrong? What about your actual problem?

from fast-excel.

rap2hpoutre avatar rap2hpoutre commented on May 18, 2024

With FastExcel, there is no pre-conf nor magic in specifying file path. You are free to import from any path you want, so you just have to write it:

(new FastExcel)->import('/full/path/to/my/file.xlsx');
// or ...
(new FastExcel)->import('../relative/path/from/where/you/are.xlsx');
// or ...
(new FastExcel)->import(storage_path('app/uploads/date/file.xlsx')); // You want this one
// or ...
(new FastExcel)->import(base_path('data/file.xlsx'));

Let me know if it fixes your problem

from fast-excel.

rap2hpoutre avatar rap2hpoutre commented on May 18, 2024

@mikizdr Let me know if I can close this issue :)

from fast-excel.

bony1993 avatar bony1993 commented on May 18, 2024

@rap2hpoutre i am using this package for excel import. now i need to view the title of excel file, how this possible with this package?

from fast-excel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.