ahmedwalid05 / FastExcel

Fast Excel Reading and Writing in .Net
MIT License
335 stars 98 forks source link

Write & in Excel cell #44

Closed prickelpit96 closed 3 years ago

prickelpit96 commented 4 years ago

I need to write a text that contains a & to excel and it fails due to the fact that a & is a special char in excel and therefore interpreted in a special way. After writing excel opens with an error and has wiped out the whole text. I tried to fix this by using a single leading ' to format the whole text as 'text' but without success.

Can you help me?

Sibz commented 4 years ago

Just a guess, but try &

prickelpit96 commented 4 years ago

Thanks for the input, but as expected didn't this change anything.Excel works different than Html. The & is not just a special char but a char for formulars.

Sibz commented 4 years ago

Excel files are just XML, so if that's going direct to the file it should be correct. I have not worked with this library in a while so can't remember how it handles special characters in this case. You need the ' so maybe ' before the &. Again I am just guessing, I think I would still be wrong as special characters should be handled already.

prickelpit96 commented 4 years ago

I tried the single quote ', wrapped everything in double quotes (with and without single quote), built a formula with =CHAR(38), etc. Nothing helped. At the moment I simply replace '&' by 'and', what is a dirty workaround....

mathieuros commented 4 years ago

HttpUtility.HtmlEncode() will do the job for you on everything you want to write.

prickelpit96 commented 4 years ago

Many thanks for the reply! Works like a charme.

ahmedwalid05 commented 3 years ago

Opened a new issue to fix it.

ahmedwalid05 commented 3 years ago

Fixed in 6b9e78d4733b362ccfbfe12e42b88b85a36e7a3c.