问题
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.
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.
For example:
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('7');
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?
回答1:
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:
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.
- Inner width in character units (e.g. 8.43 this is probably what you are familiar with in Excel)
- Full width in pixels (e.g. 64 pixels)
- 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.
(my emphasis in bold)
回答2:
Add 0.71 to the excel cell width value and give that value to the
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
eg: A Column width = 3.71 (excel value)
give column width = 4.42
will give the output file with same cell width.
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(4.42);
回答3:
Before set width of any column, first you need to disable auto size
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(false);
After that, you need to set a width
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(40);
Calculation of width that I found in MS Excel: MS Excel minus 0.71 from our defined size. Example: When you define width 40 in code that MS Excel set width 39.29. So good way to put an exact required size of any column, first you set a required width in MS Excel then adds 0.71 into that and set in your code.
After set width, you can again enable auto size.
回答4:
try this:- add below line before setting width:-
$objPHPExcel->setActiveSheetIndex(0);
来源:https://stackoverflow.com/questions/14162013/setting-column-width-with-phpexcel-not-working