gpt4 book ai didi

php - 使用 PHPExcel 设置列宽不起作用

转载 作者:可可西里 更新时间:2023-10-31 23:57:12 27 4
gpt4 key购买 nike

我正在尝试格式化我用 PHPExcel 创建的文档的单元格宽度,但是当我使用 getColumnDimension('A')->setWidth(7) 方法时,生成的 xlsx 中没有准确生成它文件。

我在 Excel 中设置了文档格式,以便可以找到我想要的单元格的值,我得到了两个值,一个以 Excel 单位为单位,另一个以英寸为单位。

例如:

$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('7');

最终会给我一个 6.17 而不是 7 的列宽。我已经尝试过同时使用字符串和 int 值,没有任何变化。有什么想法吗?

最佳答案

这是因为 MS Excel 会根据自己的内部单位调整图形本身:开发人员文档中有一整节(第 4.6.28 节,标题为“设置列宽”)对此进行了解释。

从该部分的边栏中引用:

The measure for column width in PHPExcel does not correspond exactly to the measure you may be used to in Microsoft Office Excel. Column widths are difficult to deal with in Excel, and there are several measures for the column width.

  1. Inner width in character units (e.g. 8.43 this is probably what you are familiar with in Excel)
  2. Full width in pixels (e.g. 64 pixels)
  3. Full width in character units (e.g. 9.140625, value -1 indicates unset width)

PHPExcel always operates with 3) "Full width in character units" which is in fact the only value that is stored in any Excel file, hence the most reliable measure. Unfortunately, Microsoft Office Excel does not present you with this measure. Instead measures 1) and 2) are computed by the application when the file is opened and these values are presented in various dialogues and tool tips.

The character width unit is the width of a '0' (zero) glyph in the workbooks default font. Therefore column widths measured in character units in two different workbooks can only be compared if they have the same default workbook font.

If you have some Excel file and need to know the column widths in measure 3), you can read the Excel file with PHPExcel and echo the retrieved values.

(我强调的是粗体)

关于php - 使用 PHPExcel 设置列宽不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14162013/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com