0

is it possible to convert excel file to tab delimited text file using php?

is this even possible? if not then thank you for your time :)

I am exporting a CSV or excel file file and want tp convert it to tab delimited text file to be uploaded to google merchant. since manually converting it might be time consuming.

Xavier
  • 3,919
  • 3
  • 27
  • 55
magicianiam
  • 1,474
  • 7
  • 33
  • 70
  • 1
    Yes it is, and there's a mass of libraries available to help you do so. In addition to PHPExcel (https://github.com/PHPOffice/PHPExcel), there's a list here - http://stackoverflow.com/questions/3930975/alternative-for-php-excel Which have you looked at? – Mark Baker May 23 '13 at 19:50
  • i did google if at first but then i found this: http://www.convertspot.com/excel2textcomsample.htm, which then brought me here since, I don't think it is efficient to install an application first just to convert a file. – magicianiam May 23 '13 at 19:54
  • Converting .xlsx files is easier as the new formats are XML based. – StampyCode May 23 '13 at 19:54
  • @TimStamp do you have any link that has a good explanation on converting .xlsx files to tab delimted text format using php? – magicianiam May 23 '13 at 19:56
  • @MarkBaker they all show exporting a file to excel format which I already am capable of doing, the only problem for me now is automatically converting it to tab delimted text format, though if i have missed it in the said post do tell me. thank you – magicianiam May 23 '13 at 19:59
  • Load the file using either the Excel5 or Excel2007 Reader, save using the CSV writer - it's about 5 lines of code – Mark Baker May 23 '13 at 20:00
  • @magicianIam the .xlsx file is actually a zip archive, unzip it to a directory and have a look at the files - it includes various XML files for different sheets in the spreadsheet. I can't find a good site explaining this, best way to learn is to make a simple spreadsheet and pull it apart! – StampyCode May 23 '13 at 20:09

2 Answers2

5
require_once 'PHPExcel/IOFactory.php';
$objPHPExcel = PHPExcel_IOFactory::load("myExcelFile.xlsx");

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->setDelimiter("\t");
$objWriter->save('myOutputFile.csv');
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • This is not working for me.i have included all the files.But its not showing any error also.Please help me @Mark Baker – lalithkumar Apr 20 '17 at 06:07
  • So if it isn't working, what is it actually doing? Do you have error reporting enabled for PHP? What is in the file that's created if not the expected content? Have you checked the logs for error messages? – Mark Baker Apr 20 '17 at 06:59
  • i got the problem.I am uploading the file and using it in to load function like this $_FILES['file']['tmpname'] or file_get_contents($_FILES['file']['tmpname'])..Uploaded excel data is not coming.if i given any filename like you its working.When load uploaded file not.@Mark Baker – lalithkumar Apr 20 '17 at 07:17
  • Thanks got solution from your list out of alternative of PHPExcel.http://stackoverflow.com/questions/3930975/alternative-for-php-excel – lalithkumar Apr 20 '17 at 08:45
1

Am I right thinking that a tab delimited text file is identical to CSV just with tabs in place of commas?

If so...

If there are no commas within your data, they are only separating the values, then a simple string replace should do it for you.

$tab_delimited = str_replace(",", "\t", $csv_text);

Otherwise preg_replace will be able to help you.

Steve
  • 1,853
  • 16
  • 30
  • yes it is somewhat similar to CSV, but I was expecting something special for it that requires some long code or conversion code, not just a str_replace. though this did cross my mind will try this for now. thank you – magicianiam May 23 '13 at 20:04