How to Convert .xls file to .csv file in php code

Reply

Join Date: Jun 2009
Posts: 17
Reputation: sandipan.rcciit is an unknown quantity at this point 
Solved Threads: 0
sandipan.rcciit sandipan.rcciit is offline Offline
Newbie Poster

How to Convert .xls file to .csv file in php code

 
0
  #1
29 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 524
Reputation: Will Gresham is on a distinguished road 
Solved Threads: 86
Sponsor
Will Gresham's Avatar
Will Gresham Will Gresham is offline Offline
Posting Pro
 
0
  #2
29 Days Ago
Last edited by Will Gresham; 29 Days Ago at 8:26 am.
AJAX is not a programming language, scripting language or any other sort of language.
It is acheived by using JavaScript http functions.
So, AJAX = JavaScript.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 527
Reputation: network18 is an unknown quantity at this point 
Solved Threads: 61
network18 network18 is offline Offline
Posting Pro
 
0
  #3
29 Days Ago
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 -
  1. /* Get the excel.php class here: http://www.phpclasses.org/browse/package/1919.html */
  2. require_once("../classes/excel.php");
  3. $inputFile=$argv[1];
  4. $xlsFile=$argv[2];
  5.  
  6. if( empty($inputFile) || empty($xlsFile) ) {
  7. die("Usage: ". basename($argv[0]) . " in.csv out.xls\n" );
  8. }
  9.  
  10. $fh = fopen( $inputFile, "r" );
  11. if( !is_resource($fh) ) {
  12. die("Error opening $inputFile\n" );
  13. }
  14.  
  15. /* Assuming that first line is column headings */
  16. if( ($columns = fgetcsv($fh, 1024, "\t")) == false ) {
  17. print( "Error, couldn't get header row\n" );
  18. exit(-2);
  19. }
  20. $numColumns = count($columns);
  21.  
  22. /* Now read each of the rows, and construct a
  23.   big Array that holds the data to be Excel-ified: */
  24. $xlsArray = array();
  25. $xlsArray[] = $columns;
  26. while( ($rows = fgetcsv($fh, 1024, "\t")) != FALSE ) {
  27. $rowArray = array();
  28. for( $i=0; $i<$numColumns;$i++ ) {
  29. $key = $columns[$i];
  30. $val = $rows[$i];
  31. $rowArray["$key"] = $val;
  32. }
  33. $xlsArray[] = $rowArray;
  34. unset($rowArray);
  35. }
  36. fclose($fh);
  37.  
  38. /* Now let the excel class work its magic. excel.php
  39.   has registered a stream wrapper for "xlsfile:/"
  40.   and that's what triggers its 'magic': */
  41. $xlsFile = "xlsfile:/".$xlsFile;
  42. $fOut = fopen( $xlsFile, "wb" );
  43. if( !is_resource($fOut) ) {
  44. die( "Error opening $xlsFile\n" );
  45. }
  46. fwrite($fOut, serialize($xlsArray));
  47. fclose($fOut);
  48.  
  49. exit(0);
"The discipline of writing something down is the first step towards making it happen."

follow me on twitter
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC