c# - Reading rich text from an Excel cell -
i attempting read excel spreadsheet in cells contain rich text. convert plain text format encodes formatting in way. example, given text:
"here's text with italics in middle of it."
i convert to:
"here's text [i]with italics in middle[/i] of it".
the problem haven't found way extract rich text excel cell. interop.office.excel allows me range , examine style property, tells me whether cell italicized; doesn't give me information of individual characters. best "solution" i've found involves copying contents of cell clipboard , calling clipboard.getdata(system.windows.dataformats.rtf), , it's slow practical.
is there smart way access rich text contained in excel cell i'm missing?
the way access format of individual characters in string use range.characters object
to demonstrate here simple udf in vba italic format describe. can extend detect other formats need
function decodetext(r range) variant dim strdecoded string dim bitalic boolean dim long, j long if typename(r.value) = "string" bitalic = false strdecoded = r j = 1 = 1 len(strdecoded) if not bitalic , r.characters(i, 1).font.italic strdecoded = left(strdecoded, j - 1) & "[i]" & mid(strdecoded, j) bitalic = true j = j + 3 elseif bitalic , not r.characters(i, 1).font.italic strdecoded = left(strdecoded, j - 1) & "[/i]" & mid(strdecoded, j) bitalic = false j = j + 4 end if j = j + 1 next if bitalic strdecoded = strdecoded & "[/i]" decodetext = strdecoded else decodetext = r end if end function
Comments
Post a Comment