You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

356 lines
18 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml;
  6. using System.Reflection;
  7. using System.Text.RegularExpressions;
  8. using System.Data;
  9. using System.IO;
  10. namespace NFine.Web
  11. {
  12. /// <summary>
  13. /// 打印类的公共函数
  14. /// </summary>
  15. public class PrintFunction
  16. {
  17. /// <summary>
  18. /// 把List类转化为XML字符串
  19. /// </summary>
  20. /// <typeparam name="T"></typeparam>
  21. /// <param name="DtTable"></param>
  22. /// <returns></returns>
  23. public static string ListToXml<T>(IList<T> DtTable)
  24. {
  25. StringBuilder PrintStr = new StringBuilder();
  26. XmlWriterSettings setting = new XmlWriterSettings();
  27. PropertyInfo[] TableColumns;
  28. Type TableType = DtTable[0].GetType();
  29. int ColRecn, RowRecn;
  30. string ColType;
  31. setting.Indent = true;
  32. setting.OmitXmlDeclaration = true;
  33. TableColumns = TableType.GetProperties();
  34. using (XmlWriter PrintXml = XmlWriter.Create(PrintStr, setting))
  35. {
  36. PrintXml.WriteStartElement("xml");
  37. PrintXml.WriteAttributeString("xmlns", "s", null, "u");
  38. PrintXml.WriteAttributeString("xmlns", "dt", null, "u");
  39. PrintXml.WriteAttributeString("xmlns", "rs", null, "u");
  40. PrintXml.WriteAttributeString("xmlns", "z", null, "#R");
  41. PrintXml.WriteStartElement("s", "Schema", null);
  42. PrintXml.WriteAttributeString("id", "RowsetSchema");
  43. PrintXml.WriteStartElement("s", "ElementType", null);
  44. PrintXml.WriteAttributeString("name", "row");
  45. PrintXml.WriteAttributeString("content", "eltOnly");
  46. PrintXml.WriteAttributeString("rs", "updatable", null, "true");
  47. for (ColRecn = 0; ColRecn < TableColumns.Length; ColRecn++)
  48. {
  49. ColType = TableColumns[ColRecn].PropertyType.ToString();
  50. if (ColType == "System.String" || ColType == "System.DateTime"
  51. || ColType == "System.Int" || ColType == "System.Double"
  52. || ColType == "System.Boolean" || ColType == "System.Int32" || ColType == "System.Int64")
  53. {
  54. PrintXml.WriteStartElement("s", "AttributeType", null);
  55. PrintXml.WriteAttributeString("name", TableColumns[ColRecn].Name);
  56. PrintXml.WriteAttributeString("rs", "number", null, (ColRecn + 1).ToString());
  57. PrintXml.WriteAttributeString("rs", "nullable", null, "true");
  58. PrintXml.WriteAttributeString("rs", "maydefer", null, "true");
  59. PrintXml.WriteAttributeString("rs", "writeunknown", null, "true");
  60. PrintXml.WriteAttributeString("rs", "basetable", null, "");
  61. PrintXml.WriteAttributeString("rs", "basecolumn", null, TableColumns[ColRecn].Name);
  62. if (ColType == "System.String")
  63. { //char
  64. PrintXml.WriteStartElement("s", "datatype", null);
  65. PrintXml.WriteAttributeString("dt", "type", null, "string");
  66. PrintXml.WriteAttributeString("dt", "maxLength", null, "8000");
  67. PrintXml.WriteEndElement();
  68. }
  69. else if (ColType == "System.DateTime")
  70. { //dateTime
  71. PrintXml.WriteStartElement("s", "datatype", null);
  72. PrintXml.WriteAttributeString("dt", "type", null, "dateTime");
  73. PrintXml.WriteAttributeString("dt", "maxLength", null, "16");
  74. PrintXml.WriteEndElement();
  75. }
  76. else if (ColType == "System.Double")
  77. { //Float
  78. PrintXml.WriteStartElement("s", "datatype", null);
  79. PrintXml.WriteAttributeString("dt", "type", null, "float");
  80. PrintXml.WriteAttributeString("dt", "maxLength", null, "8");
  81. PrintXml.WriteEndElement();
  82. }
  83. else if (ColType == "System.Int" || ColType == "System.Int32" || ColType == "System.Int64")
  84. { //int
  85. PrintXml.WriteStartElement("s", "datatype", null);
  86. PrintXml.WriteAttributeString("dt", "type", null, "int");
  87. PrintXml.WriteAttributeString("dt", "maxLength", null, "4");
  88. PrintXml.WriteEndElement();
  89. }
  90. else if (ColType == "System.Boolean")
  91. { //bit
  92. PrintXml.WriteStartElement("s", "datatype", null);
  93. PrintXml.WriteAttributeString("dt", "type", null, "boolean");
  94. PrintXml.WriteAttributeString("dt", "maxLength", null, "2");
  95. PrintXml.WriteEndElement();
  96. }
  97. PrintXml.WriteEndElement();
  98. }
  99. }
  100. PrintXml.WriteEndElement();
  101. PrintXml.WriteEndElement();
  102. PrintXml.WriteStartElement("rs", "data", null);
  103. for (RowRecn = 0; RowRecn < DtTable.Count; RowRecn++)
  104. {
  105. PrintXml.WriteStartElement("z", "row", null);
  106. for (ColRecn = 0; ColRecn < TableColumns.Length; ColRecn++)
  107. {
  108. if (Convert.IsDBNull(TableColumns[ColRecn].GetValue(DtTable[RowRecn], null)) == true)
  109. continue;
  110. ColType = TableColumns[ColRecn].PropertyType.ToString();
  111. if (ColType == "System.String" || ColType == "System.DateTime"
  112. || ColType == "System.Int" || ColType == "System.Double"
  113. || ColType == "System.Boolean" || ColType == "System.Int32" || ColType == "System.Int64")
  114. {
  115. if (ColType == "System.DateTime")
  116. {
  117. DateTime TmpDateTime = System.Convert.ToDateTime(TableColumns[ColRecn].GetValue(DtTable[RowRecn], null));
  118. string TmpStr = TmpDateTime.ToString("yyyy-MM-dd") + "T" + TmpDateTime.ToString("HH:mm:ss");
  119. PrintXml.WriteAttributeString(TableColumns[ColRecn].Name, TmpStr);
  120. }
  121. else
  122. {
  123. string strValue = TableColumns[ColRecn].GetValue(DtTable[RowRecn], null).ToString();
  124. strValue = strValue.Replace("\b", " ");
  125. strValue = Regex.Replace(strValue, "[\x00-\x08\x0b-\x0c\x0e-\x1f]", " ");
  126. PrintXml.WriteAttributeString(TableColumns[ColRecn].Name, strValue);
  127. }
  128. }
  129. }
  130. PrintXml.WriteEndElement();
  131. }
  132. PrintXml.WriteEndElement();
  133. PrintXml.WriteEndElement();
  134. }
  135. return PrintStr.ToString();
  136. }
  137. /// <summary>
  138. /// 把数据表转换为XML字符串
  139. /// </summary>
  140. /// <param name="pTable"></param>
  141. /// <returns></returns>
  142. public static string TableToXml(DataTable pTable)
  143. {
  144. StringBuilder stbPrint = new StringBuilder();
  145. XmlWriterSettings pXMLSet = new XmlWriterSettings();
  146. pXMLSet.Indent = true;
  147. pXMLSet.OmitXmlDeclaration = true;
  148. using (XmlWriter pPrintXML = XmlWriter.Create(stbPrint, pXMLSet))
  149. {
  150. pPrintXML.WriteStartElement("xml");
  151. pPrintXML.WriteAttributeString("xmlns", "s", null, "u");
  152. pPrintXML.WriteAttributeString("xmlns", "dt", null, "u");
  153. pPrintXML.WriteAttributeString("xmlns", "rs", null, "u");
  154. pPrintXML.WriteAttributeString("xmlns", "z", null, "#R");
  155. pPrintXML.WriteStartElement("s", "Schema", null);
  156. pPrintXML.WriteAttributeString("id", "RowsetSchema");
  157. pPrintXML.WriteStartElement("s", "ElementType", null);
  158. pPrintXML.WriteAttributeString("name", "row");
  159. pPrintXML.WriteAttributeString("content", "eltOnly");
  160. pPrintXML.WriteAttributeString("rs", "updatable", null, "true");
  161. foreach (DataColumn pColumn in pTable.Columns)
  162. {
  163. if (pColumn.DataType.Name == "Int64" || pColumn.DataType.Name == "Boolean"
  164. || pColumn.DataType.Name == "String" || pColumn.DataType.Name == "DateTime"
  165. || pColumn.DataType.Name == "Decimal" || pColumn.DataType.Name == "Double"
  166. || pColumn.DataType.Name == "Int32" || pColumn.DataType.Name == "Single"
  167. || pColumn.DataType.Name == "Int16" || pColumn.DataType.Name == "Byte")
  168. {
  169. pPrintXML.WriteStartElement("s", "AttributeType", null);
  170. pPrintXML.WriteAttributeString("name", pColumn.ColumnName);
  171. pPrintXML.WriteAttributeString("rs", "number", null, (pColumn.Ordinal + 1).ToString());
  172. pPrintXML.WriteAttributeString("rs", "nullable", null, "true");
  173. pPrintXML.WriteAttributeString("rs", "maydefer", null, "true");
  174. pPrintXML.WriteAttributeString("rs", "writeunknown", null, "true");
  175. pPrintXML.WriteAttributeString("rs", "basetable", null, pTable.TableName);
  176. pPrintXML.WriteAttributeString("rs", "basecolumn", null, pColumn.ColumnName);
  177. if (pColumn.DataType.Name == "Int64")
  178. { //bigint
  179. pPrintXML.WriteStartElement("s", "datatype", null);
  180. pPrintXML.WriteAttributeString("dt", "type", null, "i8");
  181. pPrintXML.WriteAttributeString("dt", "maxLength", null, "8");
  182. pPrintXML.WriteEndElement();
  183. }
  184. else if (pColumn.DataType.Name == "Boolean")
  185. { //bit
  186. pPrintXML.WriteStartElement("s", "datatype", null);
  187. pPrintXML.WriteAttributeString("dt", "type", null, "boolean");
  188. pPrintXML.WriteAttributeString("dt", "maxLength", null, "2");
  189. pPrintXML.WriteEndElement();
  190. }
  191. else if (pColumn.DataType.Name == "String")
  192. { //char
  193. pPrintXML.WriteStartElement("s", "datatype", null);
  194. pPrintXML.WriteAttributeString("dt", "type", null, "string");
  195. pPrintXML.WriteAttributeString("dt", "maxLength", null, pColumn.MaxLength.ToString());
  196. pPrintXML.WriteEndElement();
  197. }
  198. else if (pColumn.DataType.Name == "DateTime")
  199. { //dateTime
  200. pPrintXML.WriteStartElement("s", "datatype", null);
  201. pPrintXML.WriteAttributeString("dt", "type", null, "dateTime");
  202. pPrintXML.WriteAttributeString("dt", "maxLength", null, "16");
  203. pPrintXML.WriteEndElement();
  204. }
  205. else if (pColumn.DataType.Name == "Decimal")
  206. { //Decimal
  207. pPrintXML.WriteStartElement("s", "datatype", null);
  208. pPrintXML.WriteAttributeString("dt", "type", null, "number");
  209. pPrintXML.WriteAttributeString("dt", "maxLength", null, "19");
  210. pPrintXML.WriteEndElement();
  211. }
  212. else if (pColumn.DataType.Name == "Double")
  213. { //Float
  214. pPrintXML.WriteStartElement("s", "datatype", null);
  215. pPrintXML.WriteAttributeString("dt", "type", null, "float");
  216. pPrintXML.WriteAttributeString("dt", "maxLength", null, "8");
  217. pPrintXML.WriteEndElement();
  218. }
  219. else if (pColumn.DataType.Name == "Int32")
  220. { //int
  221. pPrintXML.WriteStartElement("s", "datatype", null);
  222. pPrintXML.WriteAttributeString("dt", "type", null, "int");
  223. pPrintXML.WriteAttributeString("dt", "maxLength", null, "4");
  224. pPrintXML.WriteEndElement();
  225. }
  226. else if (pColumn.DataType.Name == "Single")
  227. { //Real
  228. pPrintXML.WriteStartElement("s", "datatype", null);
  229. pPrintXML.WriteAttributeString("dt", "type", null, "r4");
  230. pPrintXML.WriteAttributeString("dt", "maxLength", null, "4");
  231. pPrintXML.WriteEndElement();
  232. }
  233. else if (pColumn.DataType.Name == "Int16")
  234. { //smallint
  235. pPrintXML.WriteStartElement("s", "datatype", null);
  236. pPrintXML.WriteAttributeString("dt", "type", null, "i2");
  237. pPrintXML.WriteAttributeString("dt", "maxLength", null, "2");
  238. pPrintXML.WriteEndElement();
  239. }
  240. else if (pColumn.DataType.Name == "Byte")
  241. { //tinyint
  242. pPrintXML.WriteStartElement("s", "datatype", null);
  243. pPrintXML.WriteAttributeString("dt", "type", null, "ui1");
  244. pPrintXML.WriteAttributeString("dt", "maxLength", null, "1");
  245. pPrintXML.WriteEndElement();
  246. }
  247. pPrintXML.WriteEndElement();
  248. }
  249. }
  250. pPrintXML.WriteEndElement();
  251. pPrintXML.WriteEndElement();
  252. pPrintXML.WriteStartElement("rs", "data", null);
  253. foreach (DataRow pRow in pTable.Rows)
  254. {
  255. pPrintXML.WriteStartElement("z", "row", null);
  256. foreach (DataColumn Column in pTable.Columns)
  257. {
  258. if (pRow.IsNull(Column) == true)
  259. continue;
  260. if (Column.DataType.Name == "Int64" || Column.DataType.Name == "Boolean"
  261. || Column.DataType.Name == "String" || Column.DataType.Name == "DateTime"
  262. || Column.DataType.Name == "Decimal" || Column.DataType.Name == "Double"
  263. || Column.DataType.Name == "Int32" || Column.DataType.Name == "Single"
  264. || Column.DataType.Name == "Int16" || Column.DataType.Name == "Byte")
  265. {
  266. if (Column.DataType.Name == "DateTime")
  267. {
  268. DateTime dtValue = System.Convert.ToDateTime(pRow[Column]);
  269. string strValue = dtValue.ToString("yyyy-MM-dd") + "T" + dtValue.ToString("HH:mm:ss");
  270. pPrintXML.WriteAttributeString(Column.ColumnName, strValue);
  271. }
  272. else
  273. {
  274. string strValue = pRow[Column].ToString();
  275. strValue = strValue.Replace("\b", " ");
  276. strValue = Regex.Replace(strValue, "[\x00-\x08\x0b-\x0c\x0e-\x1f]", " ");
  277. pPrintXML.WriteAttributeString(Column.ColumnName, strValue);
  278. }
  279. }
  280. }
  281. pPrintXML.WriteEndElement();
  282. }
  283. pPrintXML.WriteEndElement();
  284. pPrintXML.WriteEndElement();
  285. }
  286. return stbPrint.ToString();
  287. }
  288. /// <summary>
  289. /// 把文件转换为字符串
  290. /// </summary>
  291. /// <param name="strFileName"></param>
  292. /// <returns></returns>
  293. public static string FileToString(string strFileName)
  294. {
  295. string[] aryByte = new string[256];
  296. for (int i = 0; i < 256; i++)
  297. {
  298. if (i == 0)
  299. aryByte[i] = "00";
  300. else if (i < 16)
  301. aryByte[i] = "0" + Convert.ToString(i, 16).ToUpper();
  302. else
  303. aryByte[i] = Convert.ToString(i, 16).ToUpper();
  304. }
  305. if (!File.Exists(strFileName))
  306. {
  307. return "";
  308. }
  309. byte[] aryFileByte = File.ReadAllBytes(strFileName);
  310. StringBuilder stbFileValue = new StringBuilder();
  311. for (int i = 0; i < aryFileByte.Length; i++)
  312. {
  313. stbFileValue.Append(aryByte[aryFileByte[i]]);
  314. }
  315. aryFileByte = null;
  316. return stbFileValue.ToString();
  317. }
  318. /// <summary>
  319. /// 转化为base64编码
  320. /// </summary>
  321. /// <param name="strValue"></param>
  322. /// <returns></returns>
  323. public static string EnBase64(string strValue)
  324. {
  325. byte[] byteData = Encoding.Default.GetBytes(strValue);
  326. string strResult = Convert.ToBase64String(byteData);
  327. return strResult;
  328. }
  329. }
  330. }