php 使用 phpexcel 如何设置列格式?

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

with phpexcel how do I set column formats?

phpphpexcel

提问by Bluemagica

I am using an existing Excel file, with the first row containing plain text, to be used later as template. I then changed a few of the column formats to currency, accounting, date...e.t.c. Now I am loading in this file in phpExcel and populating data from my database to this file and saving it with a new name. But on the saved file, all cells in the newly created rows seem to be marked general, though the original plain text row still seems to have the correct format assigned.

我正在使用现有的 Excel 文件,第一行包含纯文本,稍后用作模板。然后我将一些列格式更改为货币、会计、日期等。现在我正在 phpExcel 中加载这个文件,并将数据从我的数据库填充到这个文件中,并用新名称保存它。但是在保存的文件上,新创建的行中的所有单元格似乎都被标记了general,尽管原始纯文本行似乎仍然分配了正确的格式。

My Code:

我的代码:

$xl_tmplt = ((isset($_REQUEST['excel_template']) && $_REQUEST['excel_template'] != "") 
    ? $_REQUEST['excel_template'] 
    : "report_tab1.xlsx");

require_once 'includes/classes/PHPExcel/PHPExcel.php';

$cacheMethod   = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
$cacheSettings = array('memoryCacheSize' => '8MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);

$tmp_workbook = PHPExcel_IOFactory::load($tmp_folder_path . '/templates/' . $xl_tmplt);
$tmp_workbook->setActiveSheetIndex(0);

$sheet = $tmp_workbook->getActiveSheet();
$sheet->fromArray($arr, '', 'A2');

$objWriter = PHPExcel_IOFactory::createWriter($tmp_workbook, 'Excel2007');
$objWriter->save($tmp_folder_path . "output/$filename");

$result['type'] = "success";
$result['msg']  = "The file was successfully created.";

Any idea how I can load in an excel file with predefined column formats and use it to generate a new excel? Or change/set the format of a column or cell range during generation?

知道如何加载具有预定义列格式的 excel 文件并使用它生成新的 excel 吗?或者在生成过程中更改/设置列或单元格范围的格式?

PS: I am not too good with excel so I might have set the column format in the wrong way. basically I clicked on the column letter which selects the column, and then changed the format in the dropdown above. If this is wrong, please let me know.

PS:我不太擅长excel,所以我可能以错误的方式设置了列格式。基本上我点击了选择列的列字母,然后在上面的下拉列表中更改了格式。如果这是错误的,请告诉我。

采纳答案by Amit

Please check below link for example of setting Row and Cell style

请检查以下链接,例如设置行和单元格样式

http://phpexcel.codeplex.com/workitem/7333

http://phpexcel.codeplex.com/workitem/7333

UPDATE: For formatting Numbers, Text, Date and Currency, please check below link

更新:有关格式化数字、文本、日期和货币,请查看以下链接

PHPExcel Formatting

PHPExcel 格式