PHP到Excel,粗体 [英] PHP to Excel, bold font

查看:99
本文介绍了PHP到Excel,粗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了通过php将一些数据从excel传输到excel,我正在使用此函数来创建标签;

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?

我不想使用任何库,因为我只需要一个简单的函数.

推荐答案

我会说使用一个库,因为(正如Marc B所说的)如果您使用格式/字体,那么它就不再是非常简单的"Excel文件".

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

您甚至都没有说出所需的BIFF版本,所以我假设BIFF5是因为您使用的标签单元标记为0x204

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

重要元素是pack语句中的0x0值:

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

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

您需要在"Workbook Global Substream"中创建一个Font xf记录,然后将0x0值设置为该字体记录的xf标识符+16.

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.

您没有显示足够多的代码来确定需要在何处添加新字体记录,但是字体记录的类型为0x0031.您应该已经在编写默认的字体记录(xf = 0),因此您需要修改代码的这一部分以创建第二个字体记录,其xf为1,这意味着您的0x0必须为0x11

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.

对于粗体,此Font记录需要在偏移量6处值为0x02BC.供以后参考,如果以后要添加其他字体样式,斜体字在偏移量2处需要使用0x0002的位掩码,而删除线需要在0x0008处使用的掩码在偏移量2处.如果要更改字体颜色,偏移量4指向颜色索引,而偏移量8标识上标/下标,偏移量10标识下划线类型.偏移量11、12和14标识字体系列,字符集和字体名称的大小,然后是字体名称本身.

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.

您可以在 http://msdn.microsoft.com/zh-CN/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.

这篇关于PHP到Excel,粗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆