[12074] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Globalization;
|
---|
| 4 | using System.Linq;
|
---|
| 5 | using System.Text;
|
---|
| 6 | using System.Text.RegularExpressions;
|
---|
| 7 | using OfficeOpenXml.FormulaParsing.ExpressionGraph;
|
---|
| 8 | using System.IO;
|
---|
| 9 |
|
---|
| 10 | namespace OfficeOpenXml.Utils
|
---|
| 11 | {
|
---|
| 12 | internal static class ConvertUtil
|
---|
| 13 | {
|
---|
| 14 | internal static bool IsNumeric(object candidate)
|
---|
| 15 | {
|
---|
| 16 | if (candidate == null) return false;
|
---|
| 17 | return (candidate.GetType().IsPrimitive || candidate is double || candidate is decimal || candidate is DateTime || candidate is TimeSpan || candidate is long);
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | internal static bool IsNumericString(object candidate)
|
---|
| 21 | {
|
---|
| 22 | if (candidate != null)
|
---|
| 23 | {
|
---|
| 24 | return Regex.IsMatch(candidate.ToString(), @"^[\d]+(\,[\d])?");
|
---|
| 25 | }
|
---|
| 26 | return false;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | /// <summary>
|
---|
| 30 | /// Convert an object value to a double
|
---|
| 31 | /// </summary>
|
---|
| 32 | /// <param name="v"></param>
|
---|
| 33 | /// <param name="ignoreBool"></param>
|
---|
| 34 | /// <returns></returns>
|
---|
| 35 | internal static double GetValueDouble(object v, bool ignoreBool = false)
|
---|
| 36 | {
|
---|
| 37 | double d;
|
---|
| 38 | try
|
---|
| 39 | {
|
---|
| 40 | if (ignoreBool && v is bool)
|
---|
| 41 | {
|
---|
| 42 | return 0;
|
---|
| 43 | }
|
---|
| 44 | if (IsNumeric(v))
|
---|
| 45 | {
|
---|
| 46 | if (v is DateTime)
|
---|
| 47 | {
|
---|
| 48 | d = ((DateTime)v).ToOADate();
|
---|
| 49 | }
|
---|
| 50 | else if (v is TimeSpan)
|
---|
| 51 | {
|
---|
| 52 | d = DateTime.FromOADate(0).Add((TimeSpan)v).ToOADate();
|
---|
| 53 | }
|
---|
| 54 | else
|
---|
| 55 | {
|
---|
| 56 | d = Convert.ToDouble(v, CultureInfo.InvariantCulture);
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | else
|
---|
| 60 | {
|
---|
| 61 | d = 0;
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | catch
|
---|
| 66 | {
|
---|
| 67 | d = 0;
|
---|
| 68 | }
|
---|
| 69 | return d;
|
---|
| 70 | }
|
---|
| 71 | /// <summary>
|
---|
| 72 | /// OOXML requires that "," , and & be escaped, but ' and " should *not* be escaped, nor should
|
---|
| 73 | /// any extended Unicode characters. This function only encodes the required characters.
|
---|
| 74 | /// System.Security.SecurityElement.Escape() escapes ' and " as ' and ", so it cannot
|
---|
| 75 | /// be used reliably. System.Web.HttpUtility.HtmlEncode overreaches as well and uses the numeric
|
---|
| 76 | /// escape equivalent.
|
---|
| 77 | /// </summary>
|
---|
| 78 | /// <param name="s"></param>
|
---|
| 79 | /// <returns></returns>
|
---|
| 80 | internal static string ExcelEscapeString(string s)
|
---|
| 81 | {
|
---|
| 82 | return s.Replace("&", "&").Replace("<", "<").Replace(">", ">");
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | /// <summary>
|
---|
| 86 | /// Return true if preserve space attribute is set.
|
---|
| 87 | /// </summary>
|
---|
| 88 | /// <param name="sw"></param>
|
---|
| 89 | /// <param name="t"></param>
|
---|
| 90 | /// <returns></returns>
|
---|
| 91 | internal static void ExcelEncodeString(StreamWriter sw, string t)
|
---|
| 92 | {
|
---|
| 93 | if (Regex.IsMatch(t, "(_x[0-9A-F]{4,4}_)"))
|
---|
| 94 | {
|
---|
| 95 | var match = Regex.Match(t, "(_x[0-9A-F]{4,4}_)");
|
---|
| 96 | int indexAdd = 0;
|
---|
| 97 | while (match.Success)
|
---|
| 98 | {
|
---|
| 99 | t = t.Insert(match.Index + indexAdd, "_x005F");
|
---|
| 100 | indexAdd += 6;
|
---|
| 101 | match = match.NextMatch();
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | for (int i = 0; i < t.Length; i++)
|
---|
| 105 | {
|
---|
| 106 | if (t[i] <= 0x1f && t[i] != '\t' && t[i] != '\n' && t[i] != '\r') //Not Tab, CR or LF
|
---|
| 107 | {
|
---|
| 108 | sw.Write("_x00{0}_", (t[i] < 0xa ? "0" : "") + ((int)t[i]).ToString("X"));
|
---|
| 109 | }
|
---|
| 110 | else
|
---|
| 111 | {
|
---|
| 112 | sw.Write(t[i]);
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | }
|
---|
| 117 | /// <summary>
|
---|
| 118 | /// Return true if preserve space attribute is set.
|
---|
| 119 | /// </summary>
|
---|
| 120 | /// <param name="sb"></param>
|
---|
| 121 | /// <param name="t"></param>
|
---|
| 122 | /// <returns></returns>
|
---|
| 123 | internal static void ExcelEncodeString(StringBuilder sb, string t)
|
---|
| 124 | {
|
---|
| 125 | if (Regex.IsMatch(t, "(_x[0-9A-F]{4,4}_)"))
|
---|
| 126 | {
|
---|
| 127 | var match = Regex.Match(t, "(_x[0-9A-F]{4,4}_)");
|
---|
| 128 | int indexAdd = 0;
|
---|
| 129 | while (match.Success)
|
---|
| 130 | {
|
---|
| 131 | t = t.Insert(match.Index + indexAdd, "_x005F");
|
---|
| 132 | indexAdd += 6;
|
---|
| 133 | match = match.NextMatch();
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | for (int i = 0; i < t.Length; i++)
|
---|
| 137 | {
|
---|
| 138 | if (t[i] <= 0x1f && t[i] != '\t' && t[i] != '\n' && t[i] != '\r') //Not Tab, CR or LF
|
---|
| 139 | {
|
---|
| 140 | sb.AppendFormat("_x00{0}_", (t[i] < 0xa ? "0" : "") + ((int)t[i]).ToString("X"));
|
---|
| 141 | }
|
---|
| 142 | else
|
---|
| 143 | {
|
---|
| 144 | sb.Append(t[i]);
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | }
|
---|
| 149 | internal static string ExcelDecodeString(string t)
|
---|
| 150 | {
|
---|
| 151 | var match = Regex.Match(t, "(_x005F|_x[0-9A-F]{4,4}_)");
|
---|
| 152 | if (!match.Success) return t;
|
---|
| 153 |
|
---|
| 154 | var useNextValue = false;
|
---|
| 155 | var ret = new StringBuilder();
|
---|
| 156 | var prevIndex = 0;
|
---|
| 157 | while (match.Success)
|
---|
| 158 | {
|
---|
| 159 | if (prevIndex < match.Index) ret.Append(t.Substring(prevIndex, match.Index - prevIndex));
|
---|
| 160 | if (!useNextValue && match.Value == "_x005F")
|
---|
| 161 | {
|
---|
| 162 | useNextValue = true;
|
---|
| 163 | }
|
---|
| 164 | else
|
---|
| 165 | {
|
---|
| 166 | if (useNextValue)
|
---|
| 167 | {
|
---|
| 168 | ret.Append(match.Value);
|
---|
| 169 | useNextValue = false;
|
---|
| 170 | }
|
---|
| 171 | else
|
---|
| 172 | {
|
---|
| 173 | ret.Append((char)int.Parse(match.Value.Substring(2, 4), NumberStyles.AllowHexSpecifier));
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 | prevIndex = match.Index + match.Length;
|
---|
| 177 | match = match.NextMatch();
|
---|
| 178 | }
|
---|
| 179 | ret.Append(t.Substring(prevIndex, t.Length - prevIndex));
|
---|
| 180 | return ret.ToString();
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | }
|
---|