自定义 PHPExcel 单元格格式仅在双击后显示

发布于 2024-09-09 16:01:48 字数 405 浏览 9 评论 0原文

我在使用 PHPExcel 设置自定义日期格式时遇到了一个奇怪的问题:我正在将 sql 格式的日期写入单元格,并使用 setFormatCode 将其格式设置为“d/m/y”。当我在 Excel 2007 中打开它时,它显示原始日期格式,直到我双击该单元格,然后它才更新为我想要的格式。 有没有办法让它自己刷新而不需要我帮忙?

$sheet->setCellValueByColumnAndRow($column, $row, '2010-07-16');
$sheet->getStyleByColumnAndRow($column, $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYSLASH);

I encountered a weird problem setting a custom date format with PHPExcel: I'm writing an sql formatted date to a cell and setting it's format with setFormatCode to 'd/m/y'. When I open it in Excel 2007, it shows the orignial date format until I double click the cell and then it's updated to my desired format.
Is there a way to have it refreshing itself without me helping?

$sheet->setCellValueByColumnAndRow($column, $row, '2010-07-16');
$sheet->getStyleByColumnAndRow($column, $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYSLASH);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囍笑 2024-09-16 16:01:48

除非您使用 PHPExcel 的“高级值绑定器”,否则 $sheet->setCellValueByColumnAndRow($column, $row, '2010-07-16'); 会将值存储为字符串,而不是日期,因此您在下一行中设置的日期格式在应用于字符串时毫无意义,直到您在 Excel 中读取结果文件并强制刷新...然后 Excel 本身会修复您的错误。

为了确保该值首先正确存储,您需要将其存储为日期/时间戳/数字而不是字符串,然后设置格式掩码以确保将其视为日期/时间戳而不是数字价值。

使用 strtotime() 将字符串转换为 PHP 日期,然后使用 PHPExcel 的内置日期转换方法:

$PHPDateValue = strtotime('2010-07-16');
$ExcelDateValue = PHPExcel_Shared_Date::PHPToExcel($PHPDateValue);
$sheet->setCellValueByColumnAndRow($column, $row, $ExcelDateValue); 
$sheet->getStyleByColumnAndRow($column, $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYSLASH); 

或者使用内置方法直接将日期格式的字符串转换为 Excel 日期时间值,

$dateString = '2010-07-16';
$ExcelDateValue = PHPExcel_Shared_Date::stringToExcel($dateString);
$sheet->setCellValueByColumnAndRow($column, $row, $ExcelDateValue); 
$sheet->getStyleByColumnAndRow($column, $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYSLASH); 

或者使用计算引擎函数库的 DATEVALUE() 函数:

$dateString = '2010-07-16';
$ExcelDateValue = PHPExcel_Calculation_Functions::DATEVALUE($dateString);
$sheet->setCellValueByColumnAndRow($column, $row, $ExcelDateValue); 
$sheet->getStyleByColumnAndRow($column, $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYSLASH); 

或者,选项 4 是使用 PHPExcel 的“高级值绑定器”

执行以下静态调用

PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_AdvancedValueBinder() );

要启用此功能,请在实例化工作簿对象之前 ,或者从文件加载它

然后 PHPExcel 将识别您的值是日期,并处理到 Excel 日期/时间戳的转换并自动格式化它

$dateString = '2010-07-16';
$sheet->setCellValueByColumnAndRow($column, $row, $dateString); 

Unless you're using PHPExcel's "Advanced Value Binder", then $sheet->setCellValueByColumnAndRow($column, $row, '2010-07-16'); will be storing the value as a string, not as a date, so the date format you're setting in the next line is meaningless when applied to a string until you read the resultant file in Excel and force a refresh... Excel itself then fixes your error.

To ensure that the value is correctly stored in the first place, you need to store it as a date/timestamp/number rather than a string, then set the format mask to ensure that it is treated as a date/timestamp rather than a numeric value.

Either convert your string to a PHP date using strtotime(), then use PHPExcel's built in date conversion methods:

$PHPDateValue = strtotime('2010-07-16');
$ExcelDateValue = PHPExcel_Shared_Date::PHPToExcel($PHPDateValue);
$sheet->setCellValueByColumnAndRow($column, $row, $ExcelDateValue); 
$sheet->getStyleByColumnAndRow($column, $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYSLASH); 

or use the built-in method to convert a date-formatted string to an Excel datetime value directly

$dateString = '2010-07-16';
$ExcelDateValue = PHPExcel_Shared_Date::stringToExcel($dateString);
$sheet->setCellValueByColumnAndRow($column, $row, $ExcelDateValue); 
$sheet->getStyleByColumnAndRow($column, $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYSLASH); 

or use the Calculation Engines function library's DATEVALUE() function:

$dateString = '2010-07-16';
$ExcelDateValue = PHPExcel_Calculation_Functions::DATEVALUE($dateString);
$sheet->setCellValueByColumnAndRow($column, $row, $ExcelDateValue); 
$sheet->getStyleByColumnAndRow($column, $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_DMYSLASH); 

or, option 4, is to use PHPExcel's "Advanced Value Binder"

To enable this feature, execute the following static call

PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_AdvancedValueBinder() );

before you instantiate your workbook object, or load it from file

Then PHPExcel will identify that your value is a date, and handle the conversion to an Excel date/timestamp and format it automatically

$dateString = '2010-07-16';
$sheet->setCellValueByColumnAndRow($column, $row, $dateString); 
糖果控 2024-09-16 16:01:48

我想添加一个选项 5...

如果您想将其与 SetValueExplicit 一起使用,您可以这样做:

\\convert bDate to dDate 
$dDate = PHPExcel_Shared_Date::PHPToExcel(strtotime($bDate));

\\must use type numeric
$worksheet->getCell($col_row)->setValueExplicit($dDate,PHPExcel_Cell_DataType::TYPE_NUMERIC);
\\use FORMAT_DATE of your choice i am using "YYYY-MM-DD"
$worksheet->getStyle($col_row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);

请注意,文档和在线中的日期格式似乎已过时。有关所有不同类型的 FORMAT_DATE_XYZ 的完整列表,只需查看库副本中的 PHPExcel\Style\Number_Format.php 文件即可。

仅使用高级值绑定器可能是最简单的,但如果您不能或不想使用,那么希望这些信息对您有用。

I would like to add an option 5...

If you want to use this with SetValueExplicit you would do it this way:

\\convert bDate to dDate 
$dDate = PHPExcel_Shared_Date::PHPToExcel(strtotime($bDate));

\\must use type numeric
$worksheet->getCell($col_row)->setValueExplicit($dDate,PHPExcel_Cell_DataType::TYPE_NUMERIC);
\\use FORMAT_DATE of your choice i am using "YYYY-MM-DD"
$worksheet->getStyle($col_row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDD2);

Please note that the Date Formats in the documentation and online appear to be out of date. For a full list of all the different types of FORMAT_DATE_XYZ just look in the PHPExcel\Style\Number_Format.php file in your copy of the library.

It's probably easiest to just use the Advanced Value Binder but if you can't or don't want to then hopefully this information has been useful to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文