tags:

views:

258

answers:

4

For transferring some data from to excel via php I am using this function for the creation of labels;

function xls_label($row, $col, $value, $bold )  
{       
     echo pack("ssssss", 0x204, 8 + strlen($value), $row, $col, 0x0, strlen($value)); 
     echo $value;  
}

This adds a label in regular font.

Now I was wondering what do I need to add to this function to make the font of the label bold?


I do not want to use any library since I just need this one simple function.

A: 

I was also interested in how this can be done

Mmargorp
that's not an answer
aaronasterling
I think its harsh to put vote down to the users like this.. they are posting like this because they dont have enough reputation to comment...
kvijayhari
@kvijayhari: There's a reason for that. If new users are meant to be allowed to comment, then the SO staff wouldn't have designed the site to require 50 rep to post comments. StackOverflow isn't a message board, and the rep requirements are designed to keep the site quality up. This answer shows precisely why new users aren't allowed to post comments. If he'd read the FAQ and learned what SO is about, then he wouldn't have been voted down. Besides, the answer scores are meant to indicate the quality of the answer. So a -1 for a non-answer is appropriate.
Lèse majesté
+2  A: 

Tell you a secret: Make an HTML table and write it into a file with a .XLS extension. When Excel opens it, it reads the formatting the way IE would, but now you have a spreadsheet. That means you can apply whatever formatting you please. Connect this with PHP, and you're the golden boy in the web dev department for a week.

Ian
First; I know about the save HTML as .xls hack, but Excel is one of the few application converting html to a spreadsheet and you still get a warning its not correct markup. Second; Not an answer to my question.
MrThys
I don't know what "but Excel is one of the few application converting html to a spreadsheet" is supposed to mean, but maybe you should word your question better then. This solution does in fact let you transfer data from PHP to Excel (or Calc) and bold the font. It's the most straightforward way to do it without using any kind of library.
Lèse majesté
By "but Excel is one of the few application converting html to a spreadsheet", I mean that for example Google Docs or OpenOffice don't convert/open the html table given in a document with the .xls extension.
MrThys
Actually, it _does_ work with OpenOffice (that's why I mentioned "Calc"--OO.o's spreadsheet program). Perhaps you were not using valid HTML. And if you need it to work in other programs, then you ought to specify that in the question.
Lèse majesté
A: 

I use the following format, i use all the html tags like bold , font etc..

<?php
$fn=$_POST[fname];
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$fn.xls");
header("Pragma: no-cache");
header("Expires: 0");

$c=$_POST[con];
print "$c";
?>

the post variable is generated in some other files and submitted to this file..

kvijayhari
Uh, what does this have to do with the question?
BoltClock
@Boltclock He asked "Now I was wondering what do I need to add to this function to make the font of the label bold?" so i told that use all the html content in a php string and post it to the above code to download a .xls file .. I think its always tells about the answer to the question.. If u haven't read it cleanly then it would look like that only...
kvijayhari
+2  A: 

I'd say use a library, because (as Marc B has said) "If you're using formatting/fonts, then it's no longer a "very simple" Excel file."

You don't even say what BIFF version you need, so I'll assume BIFF5 because you're using a label cell marker of 0x204

The significant element is the 0x0 value in your pack statement:

echo pack("ssssss", 0x204, 8 + strlen($value), $row, $col, 0x0, strlen($value));

You'll need to create a Font xf record in the "Workbook Global Substream", then set the 0x0 value to the xf identifier for that font record, +16.

You don't show enough of your code to identify where you'd need to add the new font record, but font records have a type of 0x0031. You should already be writing the default font record (xf = 0), so you'll need to modify this section of your code to create a second font record with an xf of 1, which would mean that your 0x0 would need to be 0x11.

For bold, this Font record needs a value of 0x02BC at offset 6. For future reference, in case you want to add further font styling subsequently, italic requires a bitmask of 0x0002 at offset 2, while strikethrough requires a bitmask of 0x0008 at offset 2. Offset 4 points to the colour index, if you want to change font colour, while offset 8 identifies superscript/subscript, and offset 10 identifies underline type. Offsets 11, 12 and 14 identify the font family, character set and size of the font name, followed by the font name itself.

You can find full details of all the options at http://msdn.microsoft.com/en-us/library/cc313154(v=office.12).aspx

As you can perhaps begin to appreciate, this is not as simple and straightforward as you might like to believe. That's why most of us use libraries when working with complex binary format rather than trying to write it all ourselves.

Mark Baker