简体   繁体   中英

How can I set an excel cell value to string.empty using interop?

I have used below way to set the excel cell value:

newSheet.Cells[23,2].Value2 = "";
newSheet.Cells[23,2].Value = "";

But strangely, when I get the value again, it always return null, why is that?

var tt = newSheet.Cells[23,2].Value; //null
var tt2 = newSheet.Cells[23,2].Value2; //null
var tt3 = newSheet.Cells[23,2].Text; //this is ""!

I found an excel which successfully set the cell value to "" on Cell[1,2]. You can check the attachment below. How does this happen? Any ideas? Am I setting the value in a wrong way?

MagicCell.xlsx

If the cell value is text then the Value2 will return type string, if the cell value is number then the Value2 will return type double. if the cell is empty the return type is COM object null.

So Value will be null too. I think (can't check right now) l, if you check any blank cell's Text (even if you didn't set it to “”) it will return an empty string.

Thanks to @Mátray Márk, his answer suddenly strikes me that the cell value type might do the trick.

So I set the:

newSheet.Cells[23,2].NumberFormat = "@";

And boom, the string.empty works!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM