Creating Tab-Delimited For Excel with PHP, Specify Col Type

  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 447

Post 3+ Months Ago

I've made download scripts for years of CSV and TSV files for opening with Excel and I've never been too concerned with declaring a column type until now. Typically, the user would download the file and use a text import wizard in Excel in which case they can declare the column types of the data before it gets imported.

However, in this case the file won't need to be downloaded in every instance and I'm trying to avoid my users from having to save the file and import into Excel everytime just to view it in a worksheet.

Exporting my data in tab separated values and applying the correct headers forces Excel to open the data and automatically parses the data into columns, but I have and ID field which looks like 02-1111 which is getting mistaken for a date and consequently gets put in as Feb-11. Is there anything that can be done to declare the column type? Of course, in PHP i just add "\t" between each value and then "\n" at the end of a row. Is there maybe an include file or xml I can place at the top that will set my column types?

  • this213
  • Guru
  • Guru
  • User avatar
  • Posts: 1258
  • Loc: ./

Post 3+ Months Ago

This would be the easiest thing I can think of right off:
PHP Code: [ Select ]
if( preg_match("/^0/", $str) ){
   $str = "'$str";
  1. if( preg_match("/^0/", $str) ){
  2.    $str = "'$str";
  3. }

The apostrophe will disappear if you try to edit the field in excel.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 52 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum

© 1998-2017. Ozzu® is a registered trademark of Unmelted, LLC.