user1816133
user1816133

Reputation:

Convert excel to csv and import data to php

I need to import data from excel to php, as I heard It's so hard, so I need convert excel file to csv. But I have the problem, I can't convert successfully, when I trying I get error message: errorcsv

My original excel file looks like: original

And after convert to csv: afterconvert

What's the problem? I need to convert and keep the same format.

My php file for csv reading looks like:

<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
?> 

And result how It looks like after reading in php: example

So I need any ideas how to convert xls to csv without loosing format or how to import data from excel file to php. Anyone can help me, please? Thank you.

Upvotes: 1

Views: 6061

Answers (3)

Allie Fitter
Allie Fitter

Reputation: 1807

So I need any ideas how to convert xls to csv without loosing format or how to import data from excel file to php. Anyone can help me, please? Thank you.

CSVs don't have any styling capabilities. Excel, Google Docs, Libre, etc... will separate them into cells, but the file itself is just as it's named--comma-separated (or possibly delimited by some other character) list of values. Typically with double quotes denoting strings and newlines denoting rows. The only way I can come up with accomplishing what you want is to encode your values in HTML.

Upvotes: 0

rub3n_lh
rub3n_lh

Reputation: 49

This PHPExcel library have some problems (memory / time ) loading big files. If your file is not so big, phpExcel is a good solution, else maybe you have to try loading the data in .csv format directly.

Upvotes: 2

Nisarg
Nisarg

Reputation: 3262

As per your request,

@Nisarg Patel as you gave me link to PHPExcel I've tried to use this, but unsuccessfully, I don't know how really I can use It. All 3 folders Classes, Documentation and Examples I need to put to my FTP? So how I need to make that It imported data from test.xls what I need to change? Thank you.

You can use PHPExcel by this.

Download Library of PHPExcel from HERE

Then include IOFactory.php file in your file.

/** PHPExcel_IOFactory */
require_once '../../../includes/PHPExcel/Classes/PHPExcel/IOFactory.php';

Then Make Object

$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load("mytest1.xls");

After this use Standard functions of PHPExcel for fetching data.

Upvotes: 0

Related Questions