0

hi,

if any one know that how to convert a excel file to csv file with php coding?????
actually i want to make a site where user upload the data as a excel
file format.

and when they clicked button the excel file is changing in csv file first
then the csv file data entered into the database directly.

i know the procedure to upload data from csv to mysql but i dont know how to change the type from excel to csv.

please help me.thank u.

4
Contributors
4
Replies
12
Views
7 Years
Discussion Span
Last Post by whois101
0

This code reads the .xls file and then converts it to the .csv line by line.
But i will suggest to look for any external library or function to do the same, it will be more easy -

/* Get the excel.php class here: http://www.phpclasses.org/browse/package/1919.html */
require_once("../classes/excel.php");
$inputFile=$argv[1];
$xlsFile=$argv[2];
 
if( empty($inputFile) || empty($xlsFile) ) {
    die("Usage: ". basename($argv[0]) . " in.csv out.xls\n" );
}
 
$fh = fopen( $inputFile, "r" );
if( !is_resource($fh) ) {
    die("Error opening $inputFile\n" );
}
 
/* Assuming that first line is column headings */
if( ($columns = fgetcsv($fh, 1024, "\t")) == false ) {
    print( "Error, couldn't get header row\n" );
    exit(-2);
}
$numColumns = count($columns);
 
/* Now read each of the rows, and construct a
    big Array that holds the data to be Excel-ified: */
$xlsArray = array();
$xlsArray[] = $columns;
while( ($rows = fgetcsv($fh, 1024, "\t")) != FALSE ) {
    $rowArray = array();
    for( $i=0; $i<$numColumns;$i++ ) {
        $key = $columns[$i];
        $val = $rows[$i];
        $rowArray["$key"] = $val;
    }
    $xlsArray[] = $rowArray;
    unset($rowArray);
}
fclose($fh);
 
/* Now let the excel class work its magic. excel.php
    has registered a stream wrapper for "xlsfile:/"
    and that's what triggers its 'magic': */
$xlsFile = "xlsfile:/".$xlsFile;
$fOut = fopen( $xlsFile, "wb" );
if( !is_resource($fOut) ) {
    die( "Error opening $xlsFile\n" );
}
fwrite($fOut, serialize($xlsArray));
fclose($fOut);
 
exit(0);
0

This code reads the .xls file and then converts it to the .csv line by line.
But i will suggest to look for any external library or function to do the same, it will be more easy -

Looking at the provided code, it appears to use the CSV file as input and outputs XLS, which is the opposite than what's described. Do you have a script that reads XLS / XLSX and outputs simple CSV (just straight text is good enough).

0

Hi,

it is not necessary to use the intermedia CSV file format.
You can use [snipped - no advertising] to import Excel data
direct into mySQL.

Greetings

Andreas

But that doesn't do the trick to allow to dedup and clean the data before importing it into MySQL. The data provided in in several formats and pretty dirty sometimes. I need CSV more than MySQL, as I already have standard routines (most of the data arrives in CSV or TSV and not in XLS(X))

Edited by Ezzaral: Snipped advertising from quoted text.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.