PHP to Excel, bold font

2019-01-20 03:54发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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..