0

I need to export the output of php script to xls or csv file. Output file must be formatted (font size, font weight, borders etc).

I have two options:

  1. content variable as string with html tags (formatted text)
  2. an array with data ready to be formatted

The easiest way I think is to export variable containing html code strict to xls file which I just did. The only problem is when I download xls file and open it, Excel alerts me that opened file format is different that file extension.

Headers in my php script:

header("Content-Type: application/xmls.ms-excel");
header("Content-Disposition: attachment;filename=test.xls");
header("Content-Transfer-Encoding: binary");

Charset is set to UTF-8.

Also I'm able to use array variable with data that I need to put in cells, but:

  1. I don't know how to format cells (fonts, borders etc)
  2. I don't know how to put data in exact cell
  3. I'm not able to use PEAR and Spreadsheet_Excel_Writer

Any further help will be appreciated.

p3le
  • 49
  • 10
  • You'll need a library, even if it's just Eli Dickinson's to generate SpreadsheetML... Aside from my own PHPExcel library (linked in Telgin's answer), I maintain a list of options here http://stackoverflow.com/questions/3930975/alternative-for-php-excel/3931142#3931142 – Mark Baker Jan 10 '13 at 00:01

1 Answers1

1

Just dumping HTML out isn't going to work, unfortunately. If you really need the spreadsheet to be formatted, CSV isn't an option since it doesn't store formatting information.

That only leaves XLS as a file format, which is a complicated binary format. Trying to export to it directly is impractical, which is why a few PHP libraries have been written to take care of this for you. I was recently bonked over the head with PHPExcel, which is supposedly not all that difficult to use. My experience with Excel exporters is that they definitely do have a learning curve to them however.

You don't say why you can't use PEAR, but if you're unable to use any external libraries at all, then you're probably out of luck.

Telgin
  • 1,614
  • 10
  • 10
  • I'm able to use classes or any php scripts, but I can't modify essential server files like php.ini because I don't have permission to do that. What I found, PEAR needs to be configured in php.ini too, so I'm looking for something else. Any 'excel writer' class with example or documentation should help to solve my problem. I've exported pure html table to xls file ignoring excel alert and everything is looking good. But I suppose that isn't a good solution. – p3le Jan 10 '13 at 00:29