Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Excel File Treats Cell Content Starting with +,-,=,@ as Formula #356

Open
LauKL1991 opened this issue Apr 19, 2024 · 2 comments
Open

Excel File Treats Cell Content Starting with +,-,=,@ as Formula #356

LauKL1991 opened this issue Apr 19, 2024 · 2 comments

Comments

@LauKL1991
Copy link

Hi,

I have encountered an issue while using FastExcel for exporting data to Excel files. It seems that when a cell's content starts with certain characters such as +, -, =, @, Excel treats it as a formula rather than string.

This behavior causes unexpected results in the exported Excel file, especially when the content is intended to be interpreted as text rather than a formula. For example, if the cell content is "-12345" or "=ABC", Excel interprets it as a formula rather than a simple text entry.

I believe this behavior is not ideal, as it can lead to data misinterpretation and inconsistencies, particularly in scenarios where the leading characters are part of the data itself and not intended to be treated as formulas. Also, this would crash the Excel file when trying to open the file after export

I kindly request your assistance in addressing this issue. It would be greatly appreciated if FastExcel could provide an option or workaround to ensure that Excel treats cell content starting with +, -, =, @ as strings rather than formulas.

Thank you.

@ariprw
Copy link

ariprw commented Jul 12, 2024

Let's do a little manipulation to find the first character, namely @, we can add the symbol '.

I also tested it when exporting from database in Laravel on create_at column, the resulting data is not correct.

The way to overcome this is to use

use Carbon\Carbon;

Carbon::parse($data->created_at)->translatedFormat('Y-m-d H:i:s')

I tried exporting 3 columns, 200,000 rows, it ran smoothly with a duration of 35s

@gmutinel
Copy link

gmutinel commented Oct 17, 2024

I wrote this trick, every resource for fast excel extends a base resource that contains this function

public function resolve($request = null)
{
	$resolution = parent::resolve($request);

	foreach ($resolution ?? [] as $rowKey => $row) {
		foreach ($row ?? [] as $key => $cellValue) {
			if (Str::length($cellValue) > 1 && Str::startsWith($cellValue, ['=', '+', '-', '@'])) {
				$resolution[$rowKey][$key] = Str::start($cellValue, "'");
			}
		}
	}

	return $resolution;
}

it's been working for a couple of years now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants