简体   繁体   中英

C# - Read MergedCells range in EPPlus

How to read the value of range that is Merged with EPPlus?

Lets say the range "G15:G18" is merged. How do I retrieve the text inside that range?

I've tried this, but without success:

string txt = ws.Cells["G15:G18"].Value.ToString();

Thanks.

Looking better at the issue, I finally understood that what I was doing was actually bringing a collection of results, where only the first item has a value.

So, basically, this code:

string txt = ws.Cells["G15:G18"].Value.ToString();

would return an array like with the text for all the cells in the range.

But except for the first cell in the array, all cells are empty. Only the first cell hold the Value for the whole range.

What I did is as simple as this:

string val = ws.Cells["G15:G18"].First().Value.ToString();

It worked fine.

Unless I missed the boat, I think it might be even easier than you think... just look for the value for the first cell in the range:

string txt = ws.Cells["G15"].Value.ToString();

Also, if you know it's text or just want the text representation of the cell, you can use the Text property:

string txt = ws.Cells["G15"].Text;

I think this concept transcends EPPlus also -- you can reference it in Excel formulas, and I believe it works this way in Interop as well.

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