php 使用 PHPExcel 设置电子表格单元格的宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6788472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:14:42  来源:igfitidea点击:

Setting width of spreadsheet cell using PHPExcel

phpphpexcelphpspreadsheet

提问by user198003

I'm trying to set the width of a cell in an Excel document generated with PHPExcel with:

我试图在用 PHPExcel 生成的 Excel 文档中设置单元格的宽度:

$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn('C')->setWidth('10');
$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn('C')->setAutoSize(false);

but that does not works.

但这不起作用。

What is the method that I need to call here?

我需要在这里调用的方法是什么?

回答by Jahmic

It's a subtle difference, but this works fine for me:

这是一个微妙的区别,但这对我来说很好用:

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

Notice, the difference between getColumnDimensionByColumnand getColumnDimension

请注意,之间的区别getColumnDimensionByColumngetColumnDimension

Also, I'm not even setting AutoSize and it works fine.

另外,我什至没有设置 AutoSize 并且它工作正常。

回答by Rolland

setAutoSize method must come before setWidth:

setAutoSize 方法必须在 setWidth 之前:

$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn('C')->setAutoSize(false);
$objPHPExcel->getActiveSheet()->getColumnDimensionByColumn('C')->setWidth('10');

回答by King Alawaka

hi i got the same problem.. add 0.71 to excel cell width value and give that value to the

嗨,我遇到了同样的问题.. 将 0.71 添加到 excel 单元格宽度值并将该值赋予

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

eg: A Column width = 3.71 (excel value)

例如:A 列宽 = 3.71(excel 值)

give column width = 4.42

给列宽 = 4.42

will give the output file with same cell width.

将给出具有相同单元格宽度的输出文件。

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

hope this help

希望这有帮助

回答by shurbks

Tha is because getColumnDimensionByColumnreceives the column index (an integer starting from 0), not a string.

这是因为getColumnDimensionByColumn接收列索引(从 0 开始的整数),而不是字符串。

The same goes for setCellValueByColumnAndRow

这同样适用于setCellValueByColumnAndRow

回答by Du Luong

autoSize for column width set as bellow. It works for me.

列宽的 autoSize 设置如下。这个对我有用。

$spreadsheet->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);

回答by Magor Menessy

This worked for me:

这对我有用:

$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(false);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10);

be sure to add setAuzoSize(false), beforethe setWidth();as Rolland mentioned

一定要加setAuzoSize(false)之前setWidth();作为罗兰提到

回答by Go0se

The correct way to set the column width is by using the line as posted by Jahmic, however it is important to note that additionally, you have to apply styling afteradding the data, and not before, otherwise on some configurations, the column width is not applied

设置列宽的正确方法是使用 Jahmic 发布的行,但重要的是要注意,此外,您必须添加数据之后应用样式,而不是之前,否则在某些配置中,列宽是没有申请