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.

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);

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).

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))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.