PHP to Excel, Bold

To transfer some data from excel through php, I use this function to create 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 to the regular font.

Now I was wondering what 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 simple function.

+2
source share
3 answers

I would say that I use the library because (as Marc B said) "If you use formatting / fonts, then this is no longer a" very simple "Excel file."

You don’t even say which version of BIFF you need, so I will count BIFF5 because you use the 0x204 tag cell marker

A significant element is the value 0x0 in your package statement:

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

You will need to create a Font xf entry in the Global Substream Workbook, and then set the value 0x0 for the xf identifier for this font entry, +16.

You do not show enough of your code to determine where you need to add a new font entry, but the font entries are of type 0x0031. You should already write a default entry (xf = 0), so you will need to change this section of your code to create a second font entry with xf of 1, which would mean that your 0x0 should be 0x11.

For bold, this font entry requires a value of 0x02BC at offset 6. For later use, if you want to add an additional font style later, italics require a 0x0002 bitmask at offset 2, and for strikethrough, a 0x0008 bitmask at offset 2 is required. Offset 4 indicates by the color index, if you want to change the font color, while an offset of 8 indicates a superscript / index, and an offset of 10 indicates an underline type. Offsets 11, 12, and 14 define the font family, character set and font name size, and then the name of the font itself.

Detailed information on all parameters can be found at http://msdn.microsoft.com/en-us/library/cc313154(v=office.12).aspx

As you can, perhaps, begin to appreciate, it is not as simple and straightforward as you would like to believe. Therefore, most of us use libraries when working with complex binary formats, and do not try to write all this ourselves.

+6
source

Tell us about the secret: create an HTML table and write it to a file with the .XLS extension. When Excel opens it, it reads the formatting, like IE, but now you have a spreadsheet. This means that you can apply any formatting. Combine this with PHP and you are a golden boy in the web development department for a week.

+5
source

I use the following format, I use all 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 sent to this file.

+1
source

All Articles