C#在Excel与Word中写入上角标与下角标

C#在Excel与Word中写入上角标与下角标

  • 一、简介
  • 二、实现过程
  • 三、导出PDF字符错位

一、简介

使用C#语言操作Word与Excel文件一般分为两种方式,一种为在项目中引用Microsoft Office组件,一种为使用NPOI等外部组件。两种方式的优缺点这里不多介绍,而包括一些如插入图片、合并单元格、插入公式公式等操作已在本博客其他文章中有所介绍,由于近期遇到新的问题,即文章题目所说需要写入上角标与下角标,实现过程踩了一个不大不小的坑,所以将一些实现细节记录在本文中。

二、实现过程

使用搜索引擎搜索在Excel与Word中写入上角标与下角标,大部分搜索结果的实现方式是通过NPOI方式实现,而本文计划使用Office组件进行文件操作。当搜索无果时打算通过在Excel中开启宏录制,然后手动写入上下角标,操作完成后查看对应宏代码,将宏代码(即VB语言)再转换为C#语言。实际录制之后发现代码较为繁琐,于是继续进行搜索,在一篇文章中发现一种通用方式写入上下角标,即使用转义字符来实现。
如写入SO₄²⁻:“SO\x2084\x00B2\x207B(mg/L)”。其中x2084为 ₄(注意:此处为4的下标) 的Unicode HexEntity,x00B2为 ²(注意:此处为2的上标) 的Unicode HexEntity,x207B为 ⁻(注意:此处为-的上标) 的Unicode HexEntity。
上下角标字符记录如下:
上角标字符

字符名称 字符 Num Entity Hex Entity
SUPERSCRIPT ZERO 8304 \x2070
SUPERSCRIPT ONE ¹ 185 \x00B9
SUPERSCRIPT TWO ² 178 \x00B2
SUPERSCRIPT THREE ³ 179 \x00B3
SUPERSCRIPT FOUR 8308 \x2074
SUPERSCRIPT FIVE 8309 \x2075
SUPERSCRIPT SIX 8310 \x2076
SUPERSCRIPT SEVEN 8311 \x2077
SUPERSCRIPT EIGHT 8312 \x2078
SUPERSCRIPT NINE 8313 \x2079
SUPERSCRIPT PLUS SIGN 8314 \x207A
SUPERSCRIPT MINUS 8315 \x207B
SUPERSCRIPT EQUALS SIGN 8316 \x207C
SUPERSCRIPT LEFT PARENTHESIS 8317 \x207D
SUPERSCRIPT RIGHT PARENTHESIS 8318 \x207E
SUPERSCRIPT LATIN SMALL LETTER I 8305 \x2071
SUPERSCRIPT LATIN SMALL LETTER N 8319 \x207F

下角标字符

字符名称 字符 Num Entity Hex Entity
SUBSCRIPT ZERO 8320 \x2080
SUBSCRIPT ONE 8321 \x2081
SUBSCRIPT TWO 8322 \x2082
SUBSCRIPT THREE 8323 \x2083
SUBSCRIPT FOUR 8324 \x2084
SUBSCRIPT FIVE 8325 \x2085
SUBSCRIPT SIX 8326 \x2086
SUBSCRIPT SEVEN 8327 \x2087
SUBSCRIPT EIGHT 8328 \x2088
SUBSCRIPT NINE 8329 \x2089
SUBSCRIPT PLUS SIGN 8330 \x208A
SUBSCRIPT MINUS 8331 \x208B
SUBSCRIPT EQUALS SIGN 8332 \x208C
SUBSCRIPT LEFT PARENTHESIS 8333 \x208D
SUBSCRIPT RIGHT PARENTHESIS 8334 \x208E
LATIN SUBSCRIPT SMALL LETTER A 8336 \x2090
LATIN SUBSCRIPT SMALL LETTER E 8337 \x2091
LATIN SUBSCRIPT SMALL LETTER O 8338 \x2092
LATIN SUBSCRIPT SMALL LETTER I 7522 \x1D62
LATIN SUBSCRIPT SMALL LETTER R 7523 \x1D63
LATIN SUBSCRIPT SMALL LETTER U 7524 \x1D64
LATIN SUBSCRIPT SMALL LETTER V 7525 \x1D65
LATIN SUBSCRIPT SMALL LETTER X 8339 \x2093
GREEK SUBSCRIPT SMALL LETTER BETA 7526 \x1D66
GREEK SUBSCRIPT SMALL LETTER GAMMA 7527 \x1D67
GREEK SUBSCRIPT SMALL LETTER RHO 7528 \x1D68
GREEK SUBSCRIPT SMALL LETTER PHI 7529 \x1D69
GREEK SUBSCRIPT SMALL LETTER CHI 7530 \x1D6A

其他一些常用字符记录如下:

数学符号字符:

字符名称 字符 Num Entity Hex Entity
LESS THAN < 60 \x3C
GREATER THAN > 62 \x3E
LESS THAN OR EQUAL TO 8804 \x2264
GREATER THAN OR EQUAL TO 8805 \x2265
PLUS OR MINUS ± 177 \xB1
NOT EQUALS 8800 \x2260
DIVISION SIGN ÷ 247 \xF7
TIMES X × 215 \x00D7
MINUS 8722 \x2212
DIVISION SLASH 8725 \x2215
FRACTION SLASH 8260 \x2044
SQUARE ROOT RADICAL 8730 \x221A
CUBE ROOT 8731 \x221B
FOURTH ROOT 8732 \x221C
INFINITY 8734 \x221E
ALEF INFINITY SYMBOL 8501 \x2135
FUNCTION ITALIC F ƒ 402 \x192
PRIME (single quote) 8242 \x2032
DOUBLE PRIME (double quote) 8243 \x2033
TRIPLE PRIME (triple quote) 8244 \x2034
THEREFORE (Triangular Dots) 8756 \x2234
DOT OPERATOR 8901 \x22C5
SUPERSCRIPT TWO ¹ 185 \xB9
SUPERSCRIPT TWO ² 178 \xB2
SUPERSCRIPT THREE ³ 179 \xB3
LEFT ANGLE BRACKET 9001 \x2329
RIGHT ANGLE BRACKET 9002 \x232A
LEFT CEILING BRACKET 8968 \x2308
RIGHT CEILING BRACKET 8969 \x2309
LEFT FLOOR BRACKET 8970 \x230A
RIGHT FLOOR BRACKET 8971 \x230B
CIRCLED PLUS (Direct Sum) 8853 \x2295
CIRCLED TIMES (Vector Product) 8855 \x2297

其他一些符号字符:

字符名称 字符 Num Entity Hex Entity
VULGAR FRACTION 1/4 ¼ 188 \xBC
VULGAR FRACTION 1/2 ½ 189 \xBD
VULGAR FRACTION 3/4 ¾ 190 \xBE
VULGAR FRACTION 1/3 8531 \x2153
VULGAR FRACTION 2/3 8532 \x2154
VULGAR FRACTION 1/5 8533 \x2155
VULGAR FRACTION 2/5 8354 \x2156
VULGAR FRACTION 3/5 8535 \x2157
VULGAR FRACTION 4/5 8536 \x2158
VULGAR FRACTION 1/6 8537 \x2159
VULGAR FRACTION 5/6 8538 \x215A
VULGAR FRACTION 1/8 8539 \x215B
VULGAR FRACTION 3/8 8540 \x215C
VULGAR FRACTION 5/8 8541 \x215D
VULGAR FRACTION 7/8 8342 \x215E
LOWER CASE MU (Mean) μ 956 \x3BC
LOWER CASE SIGMA (Standard Deviation) σ 963 \x3C3
LOWER CASE CHI χ 967 \x3C7
CAPITAL PI N-ARY PRODUCT 8721 \x2211
N-ARY COPRODUCT (upside down capital pi) 8720 \x2210
DEGREE SYMBOL ° 176 \xB0
MICRO MU SYMBOL µ 181 \xB5
SINGLE PRIME (feet, degree minutes) 8242 \2032
DOUBLE PRIME (feet, degree minutes) 8242 \2032
PER MILLE (1/1000th) 8240 \2030
INTEGRAL 8747 \x222B
PARTIAL DIFFERENTIAL 8706 \2202
INCREMENT (Difference or capital Delta) Δ 8710 \x2206
NABLA (Backward Difference, Grad or upside down triangle) 8711 \x2207
DOUBLE INTEGRAL 8748 \x222C
TRIPLE INTEGRAL 8749 \x222D
QUADRUPLE INTEGRAL 10764 \x2A0C
CONTOUR INTEGRAL 8750 \x222E
SURFACE INTEGRAL 8751 \x222F
VOLUME INTEGRAL 8752 \x2230
CLOCKWISE INTEGRAL 8753 \x2231
ANTICCLOCKWISE INTEGRAL 10769 \x2A11
CLOCKWISE CONTOUR INTEGRAL 8754 \x2232
ANTICLOCKWISE CONTOUR INTEGRAL 8755 \x2233
INFINITY (Sideways 8) 8734 \x221E
ALEF INFINITY SYMBOL 8501 \x2135
WEIERSTRASS POWER SET (Script Capital P) 8472 \x2118
IMAGINARY NUMBER (Blackletter I) 8242 \x2032
REAL NUMBER (Blackletter I) 8242 \x2032
DOUBLE-STRUCK REAL NUMBER (Double R) 8477 \x211D
COMPLEX NUMBERS (Double C) 8450 \x2102
NATURAL NUMBERS (Double N) 8469 \x2115
PRIME NUMBERS (Double P) 8473 \x2119
RATIONAL NUMBERS (Double Q) 8474 \x211A
INTEGERS (Double Z) 8484 \x2124
FOR ALL (Upside-down A) 8704 \x2200
COMPLEMENT (Thin C) 8705 \x2201
THERE EXISTS (Backwards E) 8707 \x2203
THERE DOES NOT EXIST (Backwards E with slash) 8708 \x2204
EMPTY SET (O slash) 8709 \x2205
NOT SYMBOL (Corner) ¬ 172 \xAC
TILDE (Alternate Not Symbol) ˜
LOGICAL AND (Wedge or Upside down V Symbol) 8743 \x2227
LOGICAL OR (V Symbol) 8744 \x2228
XOR 8891 \x22BB
NAND 8892 \x22BC
NOR 8893 \x22BD
INTERSECTION (Cap or Upside Down U) 8745 \x2229
UNION (Cup or U Symbol) 8746 \x222A
ELEMENT OF 8712 \x2208
NOT AN ELEMENT OF 8713 \x2209
SMALL ELEMENT OF 8714 \x220A
CONTAINS AS MEMBER 8715 \x220B
DOES NOT CONTAIN AS MEMBER 8716 \x220C
SMALL CONTAINS AS MEMBER 8717 \x220D
SET MINUS 8726 \x2216
SUBSET OF (Sideways U with cap to left) 8834 \x2282
SUPERSET OF (Sideways U with cap to right) 8835 \x2283
NOT A SUBSET OF (Subset with Slash) 8836 \x2284
NOT A SUPERSET OF (Superset with slash) 8837 \x2285
SUBSET OF OR EQUAL TO (Subset with line below) 8838 \x2286
SUPERSET OF OR EQUAL TO (Superset with line below) 8839 \x2287
NEITHER A SUBSET OF NOR EQUAL TO 8840 \x2288
NEITHER A SUPERSET OF NOR EQUAL TO 8841 \x2289
SUBSET OF WITH NOT EQUAL TO 8842 \x228A
SUPERSET OF WITH NOT EQUAL TO 8843 \x228B
DIAMOND OPERATOR 8900 \x22C4
ASYMPTOTICALLY EQUAL TO 8771 \x2243
NOT ASYMPTOTICALLY EQUAL TO 8772 \x2244
MULTISET (U with arrow) 8844 \x228C
MULTISET MULTIPLICATION (U with dot in center) 8845 \x228D
MULTISET UNION (U with plus in center) 8846 \x228E
DOUBLE SUBSET 8912 \x22D0
DOUBLE SUPERSET 8913 \x22D1
DOUBLE INTERSECTION 8914 \x22D2
DOUBLE UNION 8915 \x22D3
N-ARY LOGICAL AND 8896 \x22C0
N-ARY LOGICAL OR 8897 \x22C1
N-ARY INTERSECTION&cu 8898 \x22C2
N-ARY UNION 8899 \x22C3
CURLY LOGICAL OR 8910 \x22CE
CURLY LOGICAL AND 8911 \x22CF
CIRCLED PLUS (Direct Sum) 8853 \x2295
CIRCLED TIMES (Vector Product) 8855 \x2297
CIRCLED MINUS 8854 \x2296
CIRCLED DIVISION SLASH 8856 \x2298
ELEMENT OF WITH LONG HORIZONTAL STROKE 8946 \x22F2
ELEMENT OF WITH VERTICAL BAR AT END OF HORIZONTAL STROKE 8947 \x22F3
SMALL ELEMENT OF WITH VERTICAL BAR AT END OF HORIZONTAL STROKE 8948 \x22F4
ELEMENT OF WITH DOT ABOVE 8949 \x22F5
ELEMENT OF WITH OVERBAR 8950 \x22F6
SMALL ELEMENT OF WITH OVERBAR 8951 \x22F7
ELEMENT OF WITH UNDERBAR 8952 \x22F8
ELEMENT OF WITH TWO HORIZONTAL STROKES 8953 \x22F9
CONTAINS WITH LONG HORIZONTAL STROKE 8954 \x22FA
CONTAINS WITH VERTICAL BAR AT END OF HORIZONTAL STROKE 8955 \x22FB
SMALL CONTAINS WITH VERTICAL BAR AT END OF HORIZONTAL STROKE 8956 \x22FC
CONTAINS WITH OVERBAR 8957 \x22FD
SMALL CONTAINS WITH OVERBAR 8958 \x22FE
NOTATION BAG MEMBERSHIP 8959 \x22FF
Character Name Character Num Hex
RIGHT ANGLE 8735 \x221F
ANGLE 8736 \x2220
MEASURED ANGLE 8737 \x2221
SPHERICAL ANGLE 8738 \x2222
DIVIDES 8739 \x2223
DOES NOT DIVIDE 8740 \x2224
PARALLEL TO 8741 \x2225
NOT PARALLEL TO 8742 \x2226
RIGHT ANGLE WITH ARC 8894 \x22BE
RIGHT TRIANGLE 8895 \x22BF
UP TACK (Perpendicular) 8869 \x22A5
RIGHT TACK 8866 \x22A2
LEFT TACK 8867 \x22A3
DOWN TACK 8868 \x22A4
THEREFORE (Triangular Dots) 8756 \x2234
BECAUSE (Upside down Triangular Dots) 8757 \x2235
PROPORTIONAL TO 8733 \x221D
END OF PROOF (solid rectangle) 8718 \x220E
NOT EQUALS 8800 \x2260
APPROXIMATELY EQUAL 8773 \x2245
ALMOST EQUAL (ASYMPTOTIC) 8776 \x2248
NOT ALMOST EQUAL TO 8777 \x2249
TILDE SIMILAR TO 8764 \x223C
IDENTICAL TO (three lines) 8801 \x2261
NOT IDENTICAL TO 8802 \x2262
STRICTLY EQUIVALENT TO 8803 \x2263
NOT IDENTICAL TO 8802 \x2262
LESS-THAN OVER EQUAL TO 8806 \x2266
GREATER-THAN OVER EQUAL TO 8807 \x2267
LESS-THAN BUT NOT EQUAL TO 8808 \x2268
GREATER-THAN BUT NOT EQUAL TO 8809 \x2269
MUCH LESS-THAN 8810 \x226A
MUCH GREATER-THAN 8811 \x226B
BETWEEN 8812 \x226C
NOT EQUIVALENT TO 8813 \x226D
NOT LESS-THAN 8814 \x226E
NOT GREATER-THAN 8815 \x226F
NEITHER LESS-THAN NOR EQUAL TO 8816 \x2270
NEITHER GREATER-THAN NOR EQUAL TO 8817 \x2271
LESS-THAN OR EQUIVALENT TO 8818 \x2272
GREATER-THAN OR EQUIVALENT TO 8819 \x2273
NEITHER LESS-THAN NOR EQUIVALENT TO 8820 \x2274
NEITHER GREATER-THAN NOR EQUIVALENT TO 8821 \x2275
LESS-THAN OR GREATER-THAN 8822 \x2276
GREATER-THAN OR LESS-THAN 8823 \x2277
NEITHER LESS-THAN NOR GREATERTHAN 8824 \x2278
NEITHER GREATER-THAN NOR LESSTHAN 8825 \x2279
NOT TILDE 8769 \x2241
MINUS TILDE 8770 \x2242
ASYMPTOTICALLY EQUAL TO 8771 \x2243
NOT ASYMPTOTICALLY EQUAL TO 8772 \x2244
APPROXIMATELY BUT NOT ACTUALLY EQUAL TO 8774 \x2246
NEITHER APPROXIMATELY NOR ACTUALLY EQUAL TO 8775 \x2247
NOT ALMOST EQUAL TO 8777 \x2249
ALMOST EQUAL OR EQUAL TO 8778 \x224A
TRIPLE TILDE 8779 \x224B
ALL EQUAL TO 8780 \x224C

三、导出PDF字符错位

使用上述Unicode转义字符方式实现了写入上下角标,但在将Excel转换为PDF时,在Windows10系统下转换后显示正常,在Windows7系统下发现下角标错位,跑到了其他单元格上方,经过一番检查发现,win7系统下字体比win10系统多了一种BaTang字体,而win7系统将转义字符识别为了这种字体导致导出PDF后字符错位,解决方法为将要写入上下角标的单元格字体格式统一设置为Arial Unicode MS字体,这样导出PDF后字符是正常的。

你可能感兴趣的:(WPF,c#,开发语言,.net,Excel,上下角标)