php PHPExcel如何对从mysql表创建的整个文档应用对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12614864/
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-25 03:54:48  来源:igfitidea点击:

PHPExcel how to apply alignment for the whole document created from mysql table

phpphpexcel

提问by user1702273

I used PHPExcellibrary to generate excel files based on the table created by the mysql query. I created multiple tabs with individual data from different queries.

我使用PHPExcel库根据mysql查询创建的表生成excel文件。我使用来自不同查询的单个数据创建了多个选项卡。

I need to align the data in the all the cells in all the tabs (worksheets) to center.

我需要将所有选项卡(工作表)中的所有单元格中的数据对齐到中心。

This is my code:

这是我的代码:

$mysql_xls = new MySqlExcelBuilder($mysql_db,$mysql_user,$mysql_pass);

// Add the SQL statements to the spread sheet

$tab_name = "tabname";
$mysql_xls->add_page($tab_name,$sql_statement,NULL,'A',1);

$phpExcel = $mysql_xls->getExcel();

$phpExcel->setActiveSheetIndex(0); // Set the sheet to the first page (default first page).

I tried the following to align the text in the cells but no change:

我尝试了以下方法来对齐单元格中的文本,但没有任何变化:

$phpExcel->getActiveSheet(0)->getStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

回答by Mark Baker

Option #1

选项1

Set a default style for the entire workbook

为整个工作簿设置默认样式

$objPHPExcel->getDefaultStyle()
    ->getAlignment()
    ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

Option #2

选项#2

Apply the style to a range of cells (the entire worksheet in this case) on each individual worksheet

将样式应用于每个单独工作表上的一系列单元格(在本例中为整个工作表)

$phpExcel->getActiveSheet()
    ->getStyle( $phpExcel->getActiveSheet()->calculateWorksheetDimension() )
    ->getAlignment()
    ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);