用PHPExcel设置列宽不起作用 [英] Setting Column Width with PHPExcel Not Working

查看:680
本文介绍了用PHPExcel设置列宽不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to format the cell widths of a document that I am creating with PHPExcel, yet when I use the getColumnDimension('A')->setWidth(7) method, it is not being produced accurately in the generated xlsx file.

我在Excel中设置了文档格式,以便可以找到所需的单元格值,然后得到两个值,一个以Excel单位为单位,另一个以英寸为单位.

I formatted the document in Excel so that I could find the values of the cells that I want and I get two values, one in Excel Units and the other in Inches.

例如:

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

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

will end up giving me a column width of 6.17 instead of 7. I have already tried using both strings and int values for, nothing changes. Any ideas?

推荐答案

这是因为MS Excel会将图形本身调整为自己的内部单位:开发人员文档中有整节内容(第4.6.28节,标题为设置列的宽度")来解释这一点.

This is because MS Excel adjusts the figure itself to its own internal units: there's a whole section in the developer documentation (section 4.6.28, entitled "Setting a column’s width") that explains this.

在该部分的侧栏中引用:

Quoting from the sidebar in that section:

PHPExcel中列宽的度量不完全对应 达到您可能在Microsoft Office Excel中惯用的程度.柱子 宽度很难在Excel中处理,并且有几种 测量列宽.

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. 以字符单位表示的内部宽度(例如,8.43,这可能是您在Excel中所熟悉的)
  2. 全宽(以像素为单位)(例如64像素)
  3. 全角字符单位(例如9.140625,值-1表示 未设置宽度)
  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始终以3)以字符为单位的全宽"运行 实际上是存储在任何Excel文件中的唯一值,因此 最可靠的措施. 不幸的是,Microsoft Office Excel不会 向您介绍此措施.取而代之的是计算度量1)和2) 打开文件时由应用程序执行,并且这些值是 在各种对话框和工具提示中进行介绍.

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.

字符宽度单位是字符中"0"(零)字形的宽度. 工作簿的默认字体.因此,列宽以字符为单位 两个不同工作簿中的单元只有在具有 相同的默认工作簿字体.

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.

如果您有一些Excel文件,并且需要知道 措施3),您可以使用PHPExcel读取Excel文件并回显 检索到的值.

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.

(我的粗体为粗体字)

这篇关于用PHPExcel设置列宽不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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