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


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

    一、简介

    使用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 EntityHex Entity
    SUPERSCRIPT ZERO8304\x2070
    SUPERSCRIPT ONE¹185\x00B9
    SUPERSCRIPT TWO²178\x00B2
    SUPERSCRIPT THREE³179\x00B3
    SUPERSCRIPT FOUR8308\x2074
    SUPERSCRIPT FIVE8309\x2075
    SUPERSCRIPT SIX8310\x2076
    SUPERSCRIPT SEVEN8311\x2077
    SUPERSCRIPT EIGHT8312\x2078
    SUPERSCRIPT NINE8313\x2079
    SUPERSCRIPT PLUS SIGN8314\x207A
    SUPERSCRIPT MINUS8315\x207B
    SUPERSCRIPT EQUALS SIGN8316\x207C
    SUPERSCRIPT LEFT PARENTHESIS8317\x207D
    SUPERSCRIPT RIGHT PARENTHESIS8318\x207E
    SUPERSCRIPT LATIN SMALL LETTER I8305\x2071
    SUPERSCRIPT LATIN SMALL LETTER N8319\x207F

    下角标字符

    字符名称字符Num EntityHex Entity
    SUBSCRIPT ZERO8320\x2080
    SUBSCRIPT ONE8321\x2081
    SUBSCRIPT TWO8322\x2082
    SUBSCRIPT THREE8323\x2083
    SUBSCRIPT FOUR8324\x2084
    SUBSCRIPT FIVE8325\x2085
    SUBSCRIPT SIX8326\x2086
    SUBSCRIPT SEVEN8327\x2087
    SUBSCRIPT EIGHT8328\x2088
    SUBSCRIPT NINE8329\x2089
    SUBSCRIPT PLUS SIGN8330\x208A
    SUBSCRIPT MINUS8331\x208B
    SUBSCRIPT EQUALS SIGN8332\x208C
    SUBSCRIPT LEFT PARENTHESIS8333\x208D
    SUBSCRIPT RIGHT PARENTHESIS8334\x208E
    LATIN SUBSCRIPT SMALL LETTER A8336\x2090
    LATIN SUBSCRIPT SMALL LETTER E8337\x2091
    LATIN SUBSCRIPT SMALL LETTER O8338\x2092
    LATIN SUBSCRIPT SMALL LETTER I7522\x1D62
    LATIN SUBSCRIPT SMALL LETTER R7523\x1D63
    LATIN SUBSCRIPT SMALL LETTER U7524\x1D64
    LATIN SUBSCRIPT SMALL LETTER V7525\x1D65
    LATIN SUBSCRIPT SMALL LETTER X8339\x2093
    GREEK SUBSCRIPT SMALL LETTER BETA7526\x1D66
    GREEK SUBSCRIPT SMALL LETTER GAMMA7527\x1D67
    GREEK SUBSCRIPT SMALL LETTER RHO7528\x1D68
    GREEK SUBSCRIPT SMALL LETTER PHI7529\x1D69
    GREEK SUBSCRIPT SMALL LETTER CHI7530\x1D6A

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

    数学符号字符:

    字符名称字符Num EntityHex Entity
    LESS THAN<60\x3C
    GREATER THAN>62\x3E
    LESS THAN OR EQUAL TO8804\x2264
    GREATER THAN OR EQUAL TO8805\x2265
    PLUS OR MINUS±177\xB1
    NOT EQUALS8800\x2260
    DIVISION SIGN÷247\xF7
    TIMES X×215\x00D7
    MINUS8722\x2212
    DIVISION SLASH8725\x2215
    FRACTION SLASH8260\x2044
    SQUARE ROOT RADICAL8730\x221A
    CUBE ROOT8731\x221B
    FOURTH ROOT8732\x221C
    INFINITY8734\x221E
    ALEF INFINITY SYMBOL8501\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 OPERATOR8901\x22C5
    SUPERSCRIPT TWO¹185\xB9
    SUPERSCRIPT TWO²178\xB2
    SUPERSCRIPT THREE³179\xB3
    LEFT ANGLE BRACKET9001\x2329
    RIGHT ANGLE BRACKET9002\x232A
    LEFT CEILING BRACKET8968\x2308
    RIGHT CEILING BRACKET8969\x2309
    LEFT FLOOR BRACKET8970\x230A
    RIGHT FLOOR BRACKET8971\x230B
    CIRCLED PLUS (Direct Sum)8853\x2295
    CIRCLED TIMES (Vector Product)8855\x2297

    其他一些符号字符:

    字符名称字符Num EntityHex Entity
    VULGAR FRACTION 1/4¼188\xBC
    VULGAR FRACTION 1/2½189\xBD
    VULGAR FRACTION 3/4¾190\xBE
    VULGAR FRACTION 1/38531\x2153
    VULGAR FRACTION 2/38532\x2154
    VULGAR FRACTION 1/58533\x2155
    VULGAR FRACTION 2/58354\x2156
    VULGAR FRACTION 3/58535\x2157
    VULGAR FRACTION 4/58536\x2158
    VULGAR FRACTION 1/68537\x2159
    VULGAR FRACTION 5/68538\x215A
    VULGAR FRACTION 1/88539\x215B
    VULGAR FRACTION 3/88540\x215C
    VULGAR FRACTION 5/88541\x215D
    VULGAR FRACTION 7/88342\x215E
    LOWER CASE MU (Mean)μ956\x3BC
    LOWER CASE SIGMA (Standard Deviation)σ963\x3C3
    LOWER CASE CHIχ967\x3C7
    CAPITAL PI N-ARY PRODUCT8721\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
    INTEGRAL8747\x222B
    PARTIAL DIFFERENTIAL8706\2202
    INCREMENT (Difference or capital Delta)Δ8710\x2206
    NABLA (Backward Difference, Grad or upside down triangle)8711\x2207
    DOUBLE INTEGRAL8748\x222C
    TRIPLE INTEGRAL8749\x222D
    QUADRUPLE INTEGRAL10764\x2A0C
    CONTOUR INTEGRAL8750\x222E
    SURFACE INTEGRAL8751\x222F
    VOLUME INTEGRAL8752\x2230
    CLOCKWISE INTEGRAL8753\x2231
    ANTICCLOCKWISE INTEGRAL10769\x2A11
    CLOCKWISE CONTOUR INTEGRAL8754\x2232
    ANTICLOCKWISE CONTOUR INTEGRAL8755\x2233
    INFINITY (Sideways 8)8734\x221E
    ALEF INFINITY SYMBOL8501\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
    XOR8891\x22BB
    NAND8892\x22BC
    NOR8893\x22BD
    INTERSECTION (Cap or Upside Down U)8745\x2229
    UNION (Cup or U Symbol)8746\x222A
    ELEMENT OF8712\x2208
    NOT AN ELEMENT OF8713\x2209
    SMALL ELEMENT OF8714\x220A
    CONTAINS AS MEMBER8715\x220B
    DOES NOT CONTAIN AS MEMBER8716\x220C
    SMALL CONTAINS AS MEMBER8717\x220D
    SET MINUS8726\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 TO8840\x2288
    NEITHER A SUPERSET OF NOR EQUAL TO8841\x2289
    SUBSET OF WITH NOT EQUAL TO8842\x228A
    SUPERSET OF WITH NOT EQUAL TO8843\x228B
    DIAMOND OPERATOR8900\x22C4
    ASYMPTOTICALLY EQUAL TO8771\x2243
    NOT ASYMPTOTICALLY EQUAL TO8772\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 SUBSET8912\x22D0
    DOUBLE SUPERSET8913\x22D1
    DOUBLE INTERSECTION8914\x22D2
    DOUBLE UNION8915\x22D3
    N-ARY LOGICAL AND8896\x22C0
    N-ARY LOGICAL OR8897\x22C1
    N-ARY INTERSECTION&cu8898\x22C2
    N-ARY UNION8899\x22C3
    CURLY LOGICAL OR8910\x22CE
    CURLY LOGICAL AND8911\x22CF
    CIRCLED PLUS (Direct Sum)8853\x2295
    CIRCLED TIMES (Vector Product)8855\x2297
    CIRCLED MINUS8854\x2296
    CIRCLED DIVISION SLASH8856\x2298
    ELEMENT OF WITH LONG HORIZONTAL STROKE8946\x22F2
    ELEMENT OF WITH VERTICAL BAR AT END OF HORIZONTAL STROKE8947\x22F3
    SMALL ELEMENT OF WITH VERTICAL BAR AT END OF HORIZONTAL STROKE8948\x22F4
    ELEMENT OF WITH DOT ABOVE8949\x22F5
    ELEMENT OF WITH OVERBAR8950\x22F6
    SMALL ELEMENT OF WITH OVERBAR8951\x22F7
    ELEMENT OF WITH UNDERBAR8952\x22F8
    ELEMENT OF WITH TWO HORIZONTAL STROKES8953\x22F9
    CONTAINS WITH LONG HORIZONTAL STROKE8954\x22FA
    CONTAINS WITH VERTICAL BAR AT END OF HORIZONTAL STROKE8955\x22FB
    SMALL CONTAINS WITH VERTICAL BAR AT END OF HORIZONTAL STROKE8956\x22FC
    CONTAINS WITH OVERBAR8957\x22FD
    SMALL CONTAINS WITH OVERBAR8958\x22FE
    NOTATION BAG MEMBERSHIP8959\x22FF
    Character NameCharacterNumHex
    RIGHT ANGLE8735\x221F
    ANGLE8736\x2220
    MEASURED ANGLE8737\x2221
    SPHERICAL ANGLE8738\x2222
    DIVIDES8739\x2223
    DOES NOT DIVIDE8740\x2224
    PARALLEL TO8741\x2225
    NOT PARALLEL TO8742\x2226
    RIGHT ANGLE WITH ARC8894\x22BE
    RIGHT TRIANGLE8895\x22BF
    UP TACK (Perpendicular)8869\x22A5
    RIGHT TACK8866\x22A2
    LEFT TACK8867\x22A3
    DOWN TACK8868\x22A4
    THEREFORE (Triangular Dots)8756\x2234
    BECAUSE (Upside down Triangular Dots)8757\x2235
    PROPORTIONAL TO8733\x221D
    END OF PROOF (solid rectangle)8718\x220E
    NOT EQUALS8800\x2260
    APPROXIMATELY EQUAL8773\x2245
    ALMOST EQUAL (ASYMPTOTIC)8776\x2248
    NOT ALMOST EQUAL TO8777\x2249
    TILDE SIMILAR TO8764\x223C
    IDENTICAL TO (three lines)8801\x2261
    NOT IDENTICAL TO8802\x2262
    STRICTLY EQUIVALENT TO8803\x2263
    NOT IDENTICAL TO8802\x2262
    LESS-THAN OVER EQUAL TO8806\x2266
    GREATER-THAN OVER EQUAL TO8807\x2267
    LESS-THAN BUT NOT EQUAL TO8808\x2268
    GREATER-THAN BUT NOT EQUAL TO8809\x2269
    MUCH LESS-THAN8810\x226A
    MUCH GREATER-THAN8811\x226B
    BETWEEN8812\x226C
    NOT EQUIVALENT TO8813\x226D
    NOT LESS-THAN8814\x226E
    NOT GREATER-THAN8815\x226F
    NEITHER LESS-THAN NOR EQUAL TO8816\x2270
    NEITHER GREATER-THAN NOR EQUAL TO8817\x2271
    LESS-THAN OR EQUIVALENT TO8818\x2272
    GREATER-THAN OR EQUIVALENT TO8819\x2273
    NEITHER LESS-THAN NOR EQUIVALENT TO8820\x2274
    NEITHER GREATER-THAN NOR EQUIVALENT TO8821\x2275
    LESS-THAN OR GREATER-THAN8822\x2276
    GREATER-THAN OR LESS-THAN8823\x2277
    NEITHER LESS-THAN NOR GREATERTHAN8824\x2278
    NEITHER GREATER-THAN NOR LESSTHAN8825\x2279
    NOT TILDE8769\x2241
    MINUS TILDE8770\x2242
    ASYMPTOTICALLY EQUAL TO8771\x2243
    NOT ASYMPTOTICALLY EQUAL TO8772\x2244
    APPROXIMATELY BUT NOT ACTUALLY EQUAL TO8774\x2246
    NEITHER APPROXIMATELY NOR ACTUALLY EQUAL TO8775\x2247
    NOT ALMOST EQUAL TO8777\x2249
    ALMOST EQUAL OR EQUAL TO8778\x224A
    TRIPLE TILDE8779\x224B
    ALL EQUAL TO8780\x224C

    三、导出PDF字符错位

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

  • 相关阅读:
    linux文件目录
    避坑手册 | JAVA编码中容易踩坑的十大陷阱
    SpringBoot整合Dubbo
    操作字符串获取文件名字
    理解Lua中“元表和元方法“
    【数论】莫比乌斯反演
    C++左值右值、左值引用&右值引用&&、移动语义move
    QT 知:qmake 手册
    yarn包管理工具
    本机MySQL数据库安装
  • 原文地址:https://blog.csdn.net/weixin_46846685/article/details/126908706