Tab delimited issue?

Topics: Developer Forum, User Forum
Jun 25, 2009 at 5:57 PM
Edited Jun 25, 2009 at 6:07 PM

I have a file with tabs as the separator. When I try the first simple example at http://openxmldeveloper.org/articles/4606.aspx on the file, it saves it fine as an Excel file but the tabs have been changed to a square with a question mark inside of it.

Do I need to do something to handle this case?

I tried \t and \\t with setDelimiter, hasn't helped.

Developer
Jun 27, 2009 at 12:29 AM

You mean you tried this?

$objReader->setDelimiter("\t");

It should be working. Can you post a full example?

Jun 29, 2009 at 4:24 PM

<?php

/** PHPExcel_IOFactory */

include 'PHPExcel/IOFactory.php';

 

// set headers to redirect output to client browser as a file download

header('Content-Type: application/vnd.openXMLformats-officedocument.spreadsheetml.sheet');

header('Content-Disposition: attachment;filename="myfile.xlsx"');

header('Cache-Control: max-age=0');

 

//-----Create a reader, set some parameters and read in the file-----

$objReader = PHPExcel_IOFactory::createReader('CSV');

$objReader->setDelimiter('\t');

$objReader->setEnclosure('');

$objReader->setLineEnding("\r\n");

$objReader->setSheetIndex(0);

$objPHPExcel = $objReader->load('/home/user/public_html/tab_delimit.txt');

 

//-----Create a Writer and output the file to the browser-----

$objWriter2007 = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

$objWriter2007->save('php://output');  //push out to the client browser

?>

The tabs end up looking like this (the square chars): http://i39.tinypic.com/2zfikqt.png

Developer
Jun 30, 2009 at 3:18 AM

Ouchh... You will need this:

$objReader->setDelimiter("\t");

instead of this:

$objReader->setDelimiter('\t');

Jun 30, 2009 at 4:58 PM

Okay, I tried that. Opening the Excel gives me:

Excel cannot open the file 'myfile.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

So using single quotes gives me a valid Excel file that can be opened, but has a broken delimiter. Using double quotes won't even let me open the file.

Developer
Jun 30, 2009 at 11:15 PM

I'm afraid I would have to see the file because I cannot reconstruct the error. Can you send me a sample CSV file? (erik at phpexcel dot net)